Blame SOURCES/0001-Fix-local-privilege-escalation-to-root-and-sandbox-b.patch

3635cf
From d47f6aec436e0e9df6554436e391471097686ecc Mon Sep 17 00:00:00 2001
3635cf
From: Michael R Sweet <michael.r.sweet@gmail.com>
3635cf
Date: Tue, 8 May 2018 15:24:21 -0700
3635cf
Subject: [PATCH] Fix local privilege escalation to root and sandbox bypasses
3635cf
 in scheduler (rdar://37836779, rdar://37836995, rdar://37837252,
3635cf
 rdar://37837581)
3635cf
3635cf
---
3635cf
 man/cups-files.conf.man.in |  10 ++
3635cf
 man/cupsd.conf.man.in      |   8 --
3635cf
 scheduler/conf.c           | 201 +++++++++++++++++++++++--------------
3635cf
 scheduler/job.c            |  12 +++
3635cf
 scheduler/process.c        |  16 +--
3635cf
 scheduler/server.c         |  20 +++-
3635cf
 test/run-stp-tests.sh      |  11 +-
3635cf
 7 files changed, 179 insertions(+), 99 deletions(-)
3635cf
3635cf
diff --git a/man/cups-files.conf.man.in b/man/cups-files.conf.man.in
3635cf
index 7b96d687d..baf3cb6af 100644
3635cf
--- a/man/cups-files.conf.man.in
3635cf
+++ b/man/cups-files.conf.man.in
3635cf
@@ -153,6 +153,11 @@ The server name may be included in filenames using the string "%s", for example:
3635cf
 
3635cf
 .fi
3635cf
 The default is "/var/log/cups/page_log".
3635cf
+.\"#PassEnv
3635cf
+.TP 5
3635cf
+\fBPassEnv \fIvariable \fR[ ... \fIvariable \fR]
3635cf
+Passes the specified environment variable(s) to child processes.
3635cf
+Note: the standard CUPS filter and backend environment variables cannot be overridden using this directive.
3635cf
 .\"#RemoteRoot
3635cf
 .TP 5
3635cf
 \fBRemoteRoot \fIusername\fR
3635cf
@@ -187,6 +192,11 @@ macOS uses its keychain database to store certificates and keys while other plat
3635cf
 \fBServerRoot \fIdirectory\fR
3635cf
 Specifies the directory containing the server configuration files.
3635cf
 The default is "/etc/cups".
3635cf
+.\"#SetEnv
3635cf
+.TP 5
3635cf
+\fBSetEnv \fIvariable value\fR
3635cf
+Set the specified environment variable to be passed to child processes.
3635cf
+Note: the standard CUPS filter and backend environment variables cannot be overridden using this directive.
3635cf
 .\"#StateDir
3635cf
 .TP 5
3635cf
 \fBStateDir \fIdirectory\fR
3635cf
diff --git a/man/cupsd.conf.man.in b/man/cupsd.conf.man.in
3635cf
index 3ffc80e42..36c849398 100644
3635cf
--- a/man/cupsd.conf.man.in
3635cf
+++ b/man/cupsd.conf.man.in
3635cf
@@ -349,10 +349,6 @@ The default is "1048576" (1MB).
3635cf
 \fBMultipleOperationTimeout \fIseconds\fR
3635cf
 Specifies the maximum amount of time to allow between files in a multiple file print job.
3635cf
 The default is "300" (5 minutes).
3635cf
-.\"#PassEnv
3635cf
-.TP 5
3635cf
-\fBPassEnv \fIvariable \fR[ ... \fIvariable \fR]
3635cf
-Passes the specified environment variable(s) to child processes.
3635cf
 .\"#Policy
3635cf
 .TP 5
3635cf
 \fB<Policy \fIname\fB> \fR... \fB</Policy>\fR
3635cf
@@ -433,10 +429,6 @@ Specifies what information is included in the Server header of HTTP responses.
3635cf
 command.
3635cf
 "Full" reports "CUPS 2.0.0 (UNAME) IPP/2.0".
3635cf
 The default is "Minimal".
3635cf
-.\"#SetEnv
3635cf
-.TP 5
3635cf
-\fBSetEnv \fIvariable value\fR
3635cf
-Set the specified environment variable to be passed to child processes.
3635cf
 .\"#SSLListen
3635cf
 .TP 5
3635cf
 \fBSSLListen \fIipv4-address\fB:\fIport\fR
3635cf
diff --git a/scheduler/conf.c b/scheduler/conf.c
3635cf
index 67a91e7a6..b51c6060c 100644
3635cf
--- a/scheduler/conf.c
3635cf
+++ b/scheduler/conf.c
3635cf
@@ -2929,13 +2929,10 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
 					/* Line from file */
3635cf
 			temp[HTTP_MAX_BUFFER],
3635cf
 					/* Temporary buffer for value */
3635cf
-			*value,		/* Pointer to value */
3635cf
-			*valueptr;	/* Pointer into value */
3635cf
+			*value;		/* Pointer to value */
3635cf
   int			valuelen;	/* Length of value */
3635cf
   http_addrlist_t	*addrlist,	/* Address list */
3635cf
 			*addr;		/* Current address */
3635cf
-  cups_file_t		*incfile;	/* Include file */
3635cf
-  char			incname[1024];	/* Include filename */
3635cf
 
3635cf
 
3635cf
  /*
3635cf
@@ -2950,28 +2947,7 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
     * Decode the directive...
3635cf
     */
3635cf
 
3635cf
-    if (!_cups_strcasecmp(line, "Include") && value)
3635cf
-    {
3635cf
-     /*
3635cf
-      * Include filename
3635cf
-      */
3635cf
-
3635cf
-      if (value[0] == '/')
3635cf
-        strlcpy(incname, value, sizeof(incname));
3635cf
-      else
3635cf
-        snprintf(incname, sizeof(incname), "%s/%s", ServerRoot, value);
3635cf
-
3635cf
-      if ((incfile = cupsFileOpen(incname, "rb")) == NULL)
3635cf
-        cupsdLogMessage(CUPSD_LOG_ERROR,
3635cf
-	                "Unable to include config file \"%s\" - %s",
3635cf
-	                incname, strerror(errno));
3635cf
-      else
3635cf
-      {
3635cf
-        read_cupsd_conf(incfile);
3635cf
-	cupsFileClose(incfile);
3635cf
-      }
3635cf
-    }
3635cf
-    else if (!_cups_strcasecmp(line, "
3635cf
+    if (!_cups_strcasecmp(line, "
3635cf
     {
3635cf
      /*
3635cf
       * <Location path>
3635cf
@@ -3367,31 +3343,6 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
 	cupsdLogMessage(CUPSD_LOG_WARN, "Unknown ServerTokens %s on line %d of %s.",
3635cf
                         value, linenum, ConfigurationFile);
3635cf
     }
3635cf
-    else if (!_cups_strcasecmp(line, "PassEnv") && value)
3635cf
-    {
3635cf
-     /*
3635cf
-      * PassEnv variable [... variable]
3635cf
-      */
3635cf
-
3635cf
-      for (; *value;)
3635cf
-      {
3635cf
-        for (valuelen = 0; value[valuelen]; valuelen ++)
3635cf
-	  if (_cups_isspace(value[valuelen]) || value[valuelen] == ',')
3635cf
-	    break;
3635cf
-
3635cf
-        if (value[valuelen])
3635cf
-        {
3635cf
-	  value[valuelen] = '\0';
3635cf
-	  valuelen ++;
3635cf
-	}
3635cf
-
3635cf
-        cupsdSetEnv(value, NULL);
3635cf
-
3635cf
-        for (value += valuelen; *value; value ++)
3635cf
-	  if (!_cups_isspace(*value) || *value != ',')
3635cf
-	    break;
3635cf
-      }
3635cf
-    }
3635cf
     else if (!_cups_strcasecmp(line, "ServerAlias") && value)
3635cf
     {
3635cf
      /*
3635cf
@@ -3420,30 +3371,6 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
 	    break;
3635cf
       }
3635cf
     }
3635cf
-    else if (!_cups_strcasecmp(line, "SetEnv") && value)
3635cf
-    {
3635cf
-     /*
3635cf
-      * SetEnv variable value
3635cf
-      */
3635cf
-
3635cf
-      for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
3635cf
-
3635cf
-      if (*valueptr)
3635cf
-      {
3635cf
-       /*
3635cf
-        * Found a value...
3635cf
-	*/
3635cf
-
3635cf
-        while (isspace(*valueptr & 255))
3635cf
-	  *valueptr++ = '\0';
3635cf
-
3635cf
-        cupsdSetEnv(value, valueptr);
3635cf
-      }
3635cf
-      else
3635cf
-        cupsdLogMessage(CUPSD_LOG_ERROR,
3635cf
-	                "Missing value for SetEnv directive on line %d of %s.",
3635cf
-	                linenum, ConfigurationFile);
3635cf
-    }
3635cf
     else if (!_cups_strcasecmp(line, "AccessLog") ||
3635cf
              !_cups_strcasecmp(line, "CacheDir") ||
3635cf
              !_cups_strcasecmp(line, "ConfigFilePerm") ||
3635cf
@@ -3457,6 +3384,7 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
              !_cups_strcasecmp(line, "LogFilePerm") ||
3635cf
              !_cups_strcasecmp(line, "LPDConfigFile") ||
3635cf
              !_cups_strcasecmp(line, "PageLog") ||
3635cf
+             !_cups_strcasecmp(line, "PassEnv") ||
3635cf
              !_cups_strcasecmp(line, "Printcap") ||
3635cf
              !_cups_strcasecmp(line, "PrintcapFormat") ||
3635cf
              !_cups_strcasecmp(line, "RemoteRoot") ||
3635cf
@@ -3466,6 +3394,7 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
              !_cups_strcasecmp(line, "ServerKey") ||
3635cf
              !_cups_strcasecmp(line, "ServerKeychain") ||
3635cf
              !_cups_strcasecmp(line, "ServerRoot") ||
3635cf
+             !_cups_strcasecmp(line, "SetEnv") ||
3635cf
              !_cups_strcasecmp(line, "SMBConfigFile") ||
3635cf
              !_cups_strcasecmp(line, "StateDir") ||
3635cf
              !_cups_strcasecmp(line, "SystemGroup") ||
3635cf
@@ -3495,10 +3424,49 @@ read_cupsd_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
 static int				/* O - 1 on success, 0 on failure */
3635cf
 read_cups_files_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
 {
3635cf
-  int		linenum;		/* Current line number */
3635cf
+  int		i,			/* Looping var */
3635cf
+		linenum;		/* Current line number */
3635cf
   char		line[HTTP_MAX_BUFFER],	/* Line from file */
3635cf
 		*value;			/* Value from line */
3635cf
   struct group	*group;			/* Group */
3635cf
+  static const char * const prohibited_env[] =
3635cf
+  {					/* Prohibited environment variables */
3635cf
+    "APPLE_LANGUAGE",
3635cf
+    "AUTH_DOMAIN",
3635cf
+    "AUTH_INFO_REQUIRED",
3635cf
+    "AUTH_NEGOTIATE",
3635cf
+    "AUTH_PASSWORD",
3635cf
+    "AUTH_UID",
3635cf
+    "AUTH_USERNAME",
3635cf
+    "CHARSET",
3635cf
+    "CLASS",
3635cf
+    "CLASSIFICATION",
3635cf
+    "CONTENT_TYPE",
3635cf
+    "CUPS_CACHEDIR",
3635cf
+    "CUPS_DATADIR",
3635cf
+    "CUPS_DOCROOT",
3635cf
+    "CUPS_FILETYPE",
3635cf
+    "CUPS_FONTPATH",
3635cf
+    "CUPS_MAX_MESSAGE",
3635cf
+    "CUPS_REQUESTROOT",
3635cf
+    "CUPS_SERVERBIN",
3635cf
+    "CUPS_SERVERROOT",
3635cf
+    "CUPS_STATEDIR",
3635cf
+    "DEVICE_URI",
3635cf
+    "FINAL_CONTENT_TYPE",
3635cf
+    "HOME",
3635cf
+    "LANG",
3635cf
+    "PPD",
3635cf
+    "PRINTER",
3635cf
+    "PRINTER_INFO",
3635cf
+    "PRINTER_LOCATION",
3635cf
+    "PRINTER_STATE_REASONS",
3635cf
+    "RIP_CACHE",
3635cf
+    "SERVER_ADMIN",
3635cf
+    "SOFTWARE",
3635cf
+    "TMPDIR",
3635cf
+    "USER"
3635cf
+  };
3635cf
 
3635cf
 
3635cf
  /*
3635cf
@@ -3536,6 +3504,47 @@ read_cups_files_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
 	}
3635cf
       }
3635cf
     }
3635cf
+    else if (!_cups_strcasecmp(line, "PassEnv") && value)
3635cf
+    {
3635cf
+     /*
3635cf
+      * PassEnv variable [... variable]
3635cf
+      */
3635cf
+
3635cf
+      int valuelen;			/* Length of variable name */
3635cf
+
3635cf
+      for (; *value;)
3635cf
+      {
3635cf
+        for (valuelen = 0; value[valuelen]; valuelen ++)
3635cf
+	  if (_cups_isspace(value[valuelen]) || value[valuelen] == ',')
3635cf
+	    break;
3635cf
+
3635cf
+        if (value[valuelen])
3635cf
+        {
3635cf
+	  value[valuelen] = '\0';
3635cf
+	  valuelen ++;
3635cf
+	}
3635cf
+
3635cf
+        for (i = 0; i < (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0])); i ++)
3635cf
+        {
3635cf
+          if (!strcmp(value, prohibited_env[i]))
3635cf
+          {
3635cf
+	    cupsdLogMessage(CUPSD_LOG_ERROR, "Environment variable \"%s\" cannot be passed through on line %d of %s.", value, linenum, CupsFilesFile);
3635cf
+
3635cf
+	    if (FatalErrors & CUPSD_FATAL_CONFIG)
3635cf
+	      return (0);
3635cf
+	    else
3635cf
+	      break;
3635cf
+          }
3635cf
+	}
3635cf
+
3635cf
+        if (i >= (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0])))
3635cf
+          cupsdSetEnv(value, NULL);
3635cf
+
3635cf
+        for (value += valuelen; *value; value ++)
3635cf
+	  if (!_cups_isspace(*value) || *value != ',')
3635cf
+	    break;
3635cf
+      }
3635cf
+    }
3635cf
     else if (!_cups_strcasecmp(line, "PrintcapFormat") && value)
3635cf
     {
3635cf
      /*
3635cf
@@ -3581,6 +3590,46 @@ read_cups_files_conf(cups_file_t *fp)	/* I - File to read from */
3635cf
           return (0);
3635cf
       }
3635cf
     }
3635cf
+    else if (!_cups_strcasecmp(line, "SetEnv") && value)
3635cf
+    {
3635cf
+     /*
3635cf
+      * SetEnv variable value
3635cf
+      */
3635cf
+
3635cf
+      char *valueptr;			/* Pointer to environment variable value */
3635cf
+
3635cf
+      for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
3635cf
+
3635cf
+      if (*valueptr)
3635cf
+      {
3635cf
+       /*
3635cf
+        * Found a value...
3635cf
+	*/
3635cf
+
3635cf
+        while (isspace(*valueptr & 255))
3635cf
+	  *valueptr++ = '\0';
3635cf
+
3635cf
+        for (i = 0; i < (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0])); i ++)
3635cf
+        {
3635cf
+          if (!strcmp(value, prohibited_env[i]))
3635cf
+          {
3635cf
+	    cupsdLogMessage(CUPSD_LOG_ERROR, "Environment variable \"%s\" cannot be set  on line %d of %s.", value, linenum, CupsFilesFile);
3635cf
+
3635cf
+	    if (FatalErrors & CUPSD_FATAL_CONFIG)
3635cf
+	      return (0);
3635cf
+	    else
3635cf
+	      break;
3635cf
+          }
3635cf
+	}
3635cf
+
3635cf
+        if (i >= (int)(sizeof(prohibited_env) / sizeof(prohibited_env[0])))
3635cf
+	  cupsdSetEnv(value, valueptr);
3635cf
+      }
3635cf
+      else
3635cf
+        cupsdLogMessage(CUPSD_LOG_ERROR,
3635cf
+	                "Missing value for SetEnv directive on line %d of %s.",
3635cf
+	                linenum, ConfigurationFile);
3635cf
+    }
3635cf
     else if (!_cups_strcasecmp(line, "SystemGroup") && value)
3635cf
     {
3635cf
      /*
3635cf
diff --git a/scheduler/job.c b/scheduler/job.c
3635cf
index 61cda44e2..5ced0b9d1 100644
3635cf
--- a/scheduler/job.c
3635cf
+++ b/scheduler/job.c
3635cf
@@ -4779,6 +4779,18 @@ start_job(cupsd_job_t     *job,		/* I - Job ID */
3635cf
   job->profile  = cupsdCreateProfile(job->id, 0);
3635cf
   job->bprofile = cupsdCreateProfile(job->id, 1);
3635cf
 
3635cf
+#ifdef HAVE_SANDBOX_H
3635cf
+  if ((!job->profile || !job->bprofile) && UseSandboxing && Sandboxing != CUPSD_SANDBOXING_OFF)
3635cf
+  {
3635cf
+   /*
3635cf
+    * Failure to create the sandbox profile means something really bad has
3635cf
+    * happened and we need to shutdown immediately.
3635cf
+    */
3635cf
+
3635cf
+    return;
3635cf
+  }
3635cf
+#endif /* HAVE_SANDBOX_H */
3635cf
+
3635cf
  /*
3635cf
   * Create the status pipes and buffer...
3635cf
   */
3635cf
diff --git a/scheduler/process.c b/scheduler/process.c
3635cf
index b8d49d8f0..3c1c6ba4f 100644
3635cf
--- a/scheduler/process.c
3635cf
+++ b/scheduler/process.c
3635cf
@@ -98,9 +98,13 @@ cupsdCreateProfile(int job_id,		/* I - Job ID or 0 for none */
3635cf
 
3635cf
   if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL)
3635cf
   {
3635cf
+   /*
3635cf
+    * This should never happen, and is fatal when sandboxing is enabled.
3635cf
+    */
3635cf
+
3635cf
     cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
3635cf
-    cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s",
3635cf
-                    strerror(errno));
3635cf
+    cupsdLogMessage(CUPSD_LOG_EMERG, "Unable to create security profile: %s", strerror(errno));
3635cf
+    kill(getpid(), SIGTERM);
3635cf
     return (NULL);
3635cf
   }
3635cf
 
3635cf
@@ -197,10 +201,8 @@ cupsdCreateProfile(int job_id,		/* I - Job ID or 0 for none */
3635cf
 		 " #\"^%s/\""		/* TempDir/... */
3635cf
 		 " #\"^%s$\""		/* CacheDir */
3635cf
 		 " #\"^%s/\""		/* CacheDir/... */
3635cf
-		 " #\"^%s$\""		/* StateDir */
3635cf
-		 " #\"^%s/\""		/* StateDir/... */
3635cf
 		 "))\n",
3635cf
-		 temp, temp, cache, cache, state, state);
3635cf
+		 temp, temp, cache, cache);
3635cf
   /* Read common folders */
3635cf
   cupsFilePrintf(fp,
3635cf
                  "(allow file-read-data file-read-metadata\n"
3635cf
@@ -242,8 +244,10 @@ cupsdCreateProfile(int job_id,		/* I - Job ID or 0 for none */
3635cf
 		 " #\"^%s/\""		/* ServerBin/... */
3635cf
 		 " #\"^%s$\""		/* ServerRoot */
3635cf
 		 " #\"^%s/\""		/* ServerRoot/... */
3635cf
+		 " #\"^%s$\""		/* StateDir */
3635cf
+		 " #\"^%s/\""		/* StateDir/... */
3635cf
 		 "))\n",
3635cf
-		 request, request, bin, bin, root, root);
3635cf
+		 request, request, bin, bin, root, root, state, state);
3635cf
   if (Sandboxing == CUPSD_SANDBOXING_RELAXED)
3635cf
   {
3635cf
     /* Limited write access to /Library/Printers/... */
3635cf
diff --git a/scheduler/server.c b/scheduler/server.c
3635cf
index cecbabe67..a4033791b 100644
3635cf
--- a/scheduler/server.c
3635cf
+++ b/scheduler/server.c
3635cf
@@ -34,16 +34,28 @@ void
3635cf
 cupsdStartServer(void)
3635cf
 {
3635cf
  /*
3635cf
-  * Start color management (as needed)...
3635cf
+  * Create the default security profile...
3635cf
   */
3635cf
 
3635cf
-  cupsdStartColor();
3635cf
+  DefaultProfile = cupsdCreateProfile(0, 1);
3635cf
+
3635cf
+#ifdef HAVE_SANDBOX_H
3635cf
+  if (!DefaultProfile && UseSandboxing && Sandboxing != CUPSD_SANDBOXING_OFF)
3635cf
+  {
3635cf
+   /*
3635cf
+    * Failure to create the sandbox profile means something really bad has
3635cf
+    * happened and we need to shutdown immediately.
3635cf
+    */
3635cf
+
3635cf
+    return;
3635cf
+  }
3635cf
+#endif /* HAVE_SANDBOX_H */
3635cf
 
3635cf
  /*
3635cf
-  * Create the default security profile...
3635cf
+  * Start color management (as needed)...
3635cf
   */
3635cf
 
3635cf
-  DefaultProfile = cupsdCreateProfile(0, 1);
3635cf
+  cupsdStartColor();
3635cf
 
3635cf
  /*
3635cf
   * Startup all the networking stuff...
3635cf
diff --git a/test/run-stp-tests.sh b/test/run-stp-tests.sh
3635cf
index 7eb269a67..f83bd5d91 100755
3635cf
--- a/test/run-stp-tests.sh
3635cf
+++ b/test/run-stp-tests.sh
3635cf
@@ -489,11 +489,6 @@ StrictConformance Yes
3635cf
 Browsing Off
3635cf
 Listen localhost:$port
3635cf
 Listen $BASE/sock
3635cf
-PassEnv DYLD_LIBRARY_PATH
3635cf
-PassEnv LD_LIBRARY_PATH
3635cf
-PassEnv LD_PRELOAD
3635cf
-PassEnv LOCALEDIR
3635cf
-PassEnv SHLIB_PATH
3635cf
 MaxSubscriptions 3
3635cf
 MaxLogSize 0
3635cf
 AccessLogLevel actions
3635cf
@@ -529,6 +524,12 @@ TempDir $BASE/spool/temp
3635cf
 AccessLog $BASE/log/access_log
3635cf
 ErrorLog $BASE/log/error_log
3635cf
 PageLog $BASE/log/page_log
3635cf
+
3635cf
+PassEnv DYLD_LIBRARY_PATH
3635cf
+PassEnv LD_LIBRARY_PATH
3635cf
+PassEnv LD_PRELOAD
3635cf
+PassEnv LOCALEDIR
3635cf
+PassEnv SHLIB_PATH
3635cf
 EOF
3635cf
 
3635cf
 if test $ssltype != 0 -a `uname` = Darwin; then
3635cf
-- 
3635cf
2.17.1
3635cf