1 diff -urN xc.orig/config/cf/Server.tmpl xc/config/cf/Server.tmpl
2 --- xc.orig/config/cf/Server.tmpl Mon Dec 30 15:52:30 2002
3 +++ xc/config/cf/Server.tmpl Mon Dec 30 17:31:51 2002
5 #ifndef DoThreadedServer
6 #define DoThreadedServer NO
8 +#ifndef XserverNeedsSetUID
9 +#define XserverNeedsSetUID NO
11 +#ifndef UseXserverWrapper
12 +#define UseXserverWrapper XserverNeedsSetUID
14 #ifndef InstallServerSetUID
15 -#define InstallServerSetUID NO
16 +#define InstallServerSetUID (XserverNeedsSetUID && !UseXserverWrapper)
19 #ifdef CrossCompileDir
20 diff -urN xc.orig/config/cf/xf86site.def xc/config/cf/xf86site.def
21 --- xc.orig/config/cf/xf86site.def Mon Dec 30 15:52:31 2002
22 +++ xc/config/cf/xf86site.def Mon Dec 30 17:37:06 2002
27 - * If you only run the X server under xdm the X servers don't need to be
28 - * installed SetUID, and you may comment out the lines below. If you run
29 - * the servers by hand (with xinit or startx), then they do need to be
30 - * installed SetUID on most platforms.
31 + * The X servers need to run as root on most OSs. We're now using a
32 + * wrapper in that case, but we still need to make it known that the
33 + * servers need SetUID. When only using xdm, this (and the wrapper)
34 + * are not required. Disabling this automatically disables use of the
37 - * Consult your system administrator before making the X server setuid.
38 + * If you're only starting the Xservers with xdm set this to NO
40 -#define InstallXserverSetUID NO
41 +#define XserverNeedsSetUID NO
45 diff -urN xc.orig/config/cf/xfree86.cf xc/config/cf/xfree86.cf
46 --- xc.orig/config/cf/xfree86.cf Mon Dec 30 15:52:31 2002
47 +++ xc/config/cf/xfree86.cf Mon Dec 30 17:31:51 2002
48 @@ -1104,12 +1104,15 @@
52 - * The default is to install the X servers setuid-root on most OSs.
53 - * It the servers are only started by xdm, they should not be setuid-root.
54 + * The X servers need to run as root on most OSs. We're now using a
55 + * wrapper in that case, but we still need to make it known that the
56 + * servers need SetUID. When only using xdm, this (and the wrapper)
57 + * are not required. Disabling this automatically disables use of the
60 #if !defined(i386MachArchitecture) && !defined(OS2Architecture)
61 -# ifndef InstallXserverSetUID
62 -# define InstallXserverSetUID YES
63 +# ifndef XserverNeedsSetUID
64 +# define XserverNeedsSetUID YES
68 diff -urN xc.orig/programs/Xserver/Imakefile xc/programs/Xserver/Imakefile
69 --- xc.orig/programs/Xserver/Imakefile Mon Dec 30 15:53:22 2002
70 +++ xc/programs/Xserver/Imakefile Mon Dec 30 17:31:51 2002
73 XCOMM $XFree86: xc/programs/Xserver/Imakefile,v 3.275 2002/12/21 00:19:11 torrey Exp $
75 -#ifndef InstallXserverSetUID
76 -#define InstallXserverSetUID NO
78 -#define InstallServerSetUID InstallXserverSetUID
80 #include <Server.tmpl>
83 @@ -1146,6 +1141,11 @@
84 #endif /* XnestServer */
87 +#if UseXserverWrapper
88 +SetUIDProgramTarget(Xwrapper,os/wrapper.o,NullParameter,$(PAMLIBS),NullParameter)
89 +InstallProgramWithFlags(Xwrapper,$(BINDIR),$(INSTUIDFLAGS))
92 #if defined(XnonServer) && XnonServer
94 XCOMM non server, just compile sources for build test
95 diff -urN xc.orig/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c
96 --- xc.orig/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c Mon Dec 30 15:53:52 2002
97 +++ xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c Mon Dec 30 17:31:51 2002
99 /* check if we're run with euid==0 */
102 - FatalError("xf86OpenConsole: Server must be suid root\n");
103 + FatalError("xf86OpenConsole: Server must be running with root "
105 + "You should be using Xwrapper to start the server or xdm.\n"
106 + "We strongly advise against making the server SUID root!\n");
110 --- xc/programs/Xserver/os/Imakefile.orig 2006-02-19 16:51:31.000000000 +0100
111 +++ xc/programs/Xserver/os/Imakefile 2006-07-19 21:30:56.576028250 +0200
113 -I$(SERVERSRC)/Xext -I$(SERVERSRC)/render \
114 -I$(SERVERSRC)/lbx -I$(SERVERSRC)/Xprint \
115 -I$(TOP)/lib/Xau Krb5Includes
116 + EXTRA_DEFINES = -DUSE_PAM
117 DEPEND_DEFINES = $(DBM_DEFINES) $(XDMCP_DEFINES) \
118 $(TRANS_INCLUDES) $(CONNECTION_FLAGS) DependDefines
119 LINTLIBS = ../dix/llib-ldix.ln
121 SpecialCObjectRule(oscolor,$(ICONFIGFILES),$(DBM_DEFINES))
124 +#if UseXserverWrapper
125 +AllTarget(wrapper.o)
127 + WRAPPER_DEFINES = -DXSERVER_PATH=\"/etc/X11/X\"
129 +SpecialCObjectRule(wrapper,NullParameter,$(WRAPPER_DEFINES))
133 LinkSourceFile(k5encode.c,$(XAUTHSRC))
135 diff -urN xc.orig/programs/Xserver/os/wrapper.c xc/programs/Xserver/os/wrapper.c
136 --- xc.orig/programs/Xserver/os/wrapper.c Thu Jan 1 01:00:00 1970
137 +++ xc/programs/Xserver/os/wrapper.c Mon Dec 30 17:31:52 2002
140 + * X server wrapper.
142 + * This wrapper makes some sanity checks on the command line arguments
143 + * and environment variables when run with euid == 0 && euid != uid.
144 + * If the checks fail, the wrapper exits with a message.
145 + * If they succeed, it exec's the Xserver.
149 + * Copyright (c) 1998 by The XFree86 Project, Inc. All Rights Reserved.
151 + * Permission is hereby granted, free of charge, to any person obtaining
152 + * a copy of this software and associated documentation files (the
153 + * "Software"), to deal in the Software without restriction, including
154 + * without limitation the rights to use, copy, modify, merge, publish,
155 + * distribute, sublicense, and/or sell copies of the Software, and to
156 + * permit persons to whom the Software is furnished to do so, subject
157 + * to the following conditions:
159 + * The above copyright notice and this permission notice shall be included
160 + * in all copies or substantial portions of the Software.
162 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
163 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
164 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
165 + * IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES
166 + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
167 + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
168 + * OR OTHER DEALINGS IN THE SOFTWARE.
170 + * Except as contained in this notice, the name of the XFree86 Project
171 + * shall not be used in advertising or otherwise to promote the sale,
172 + * use or other dealings in this Software without prior written
173 + * authorization from the XFree86 Project.
176 +/* $XFree86: xc/programs/Xserver/os/wrapper.c,v 1.1.2.5 1998/02/27 15:28:59 dawes Exp $ */
178 +/* This is normally set in the Imakefile */
179 +#ifndef XSERVER_PATH
180 +#define XSERVER_PATH "/etc/X11/X"
188 +#include <sys/types.h>
190 +#include <security/pam_appl.h>
191 +#include <security/pam_misc.h>
193 +#endif /* USE_PAM */
195 +/* Neither of these should be required for XFree86 3.3.2 */
196 +#ifndef REJECT_CONFIG
197 +#define REJECT_CONFIG 0
199 +#ifndef REJECT_XKBDIR
200 +#define REJECT_XKBDIR 0
203 +/* Consider LD* variables insecure ? */
204 +#ifndef REMOVE_ENV_LD
205 +#define REMOVE_ENV_LD 1
208 +/* Remove long environment variables? */
209 +#ifndef REMOVE_LONG_ENV
210 +#define REMOVE_LONG_ENV 1
213 +/* Check args and env only if running setuid (euid == 0 && euid != uid) ? */
215 +#define CHECK_EUID 1
219 + * Maybe the locale can be faked to make isprint(3) report that everything
220 + * is printable? Avoid it by default.
223 +#define USE_ISPRINT 0
226 +#define MAX_ARG_LENGTH 128
227 +#define MAX_ENV_LENGTH 256
228 +#define MAX_ENV_PATH_LENGTH 2048
232 +#define checkPrintable(c) isprint(c)
234 +#define checkPrintable(c) (((c) & 0x7f) >= 0x20 && ((c) & 0x7f) != 0x7f)
247 +#endif /* USE_PAM */
251 + "\nIf the arguments used are valid, and have been rejected incorrectly\n" \
252 + "please send details of the arguments and why they are valid to\n" \
253 + "XFree86@XFree86.org. In the meantime, you can start the Xserver as\n" \
254 + "the \"super user\" (root).\n"
257 + "\nIf the environment is valid, and have been rejected incorrectly\n" \
258 + "please send details of the environment and why it is valid to\n" \
259 + "XFree86@XFree86.org. In the meantime, you can start the Xserver as\n" \
260 + "the \"super user\" (root).\n"
263 +static struct pam_conv conv = {
267 +#endif /* USE_PAM */
271 +main(int argc, char **argv, char **envp)
273 + enum BadCode bad = NotBad;
277 + pam_handle_t *pamh = NULL;
281 + pw = getpwuid(getuid());
283 + bad = InternalError;
287 + retval = pam_start("xserver", pw->pw_name, &conv, &pamh);
288 + if (retval != PAM_SUCCESS)
293 + retval = pam_authenticate(pamh, 0);
294 + if (retval != PAM_SUCCESS) {
295 + pam_end(pamh, retval);
296 + bad = PamAuthFailed;
301 + retval = pam_acct_mgmt(pamh, 0);
302 + if (retval != PAM_SUCCESS) {
303 + pam_end(pamh, retval);
304 + bad = PamAuthFailed;
308 + /* this is not a session, so do not do session management */
310 + if (!bad) pam_end(pamh, PAM_SUCCESS);
311 +#endif /* USE_PAM */
314 + if (!bad && geteuid() == 0 && getuid() != geteuid()) {
318 + /* Check each argv[] */
319 + for (i = 1; i < argc; i++) {
321 + /* Check for known bad arguments */
323 + if (strcmp(argv[i], "-config") == 0) {
329 + if (strcmp(argv[i], "-xkbdir") == 0) {
334 + if (strlen(argv[i]) > MAX_ARG_LENGTH) {
340 + if (checkPrintable(*a) == 0) {
341 + bad = UnprintableArg;
349 + /* Check each envp[] */
351 + for (i = 0; envp[i]; i++) {
353 + /* Check for bad environment variables and values */
355 + while (envp[i] && (strncmp(envp[i], "LD", 2) == 0)) {
356 + for (j = i; envp[j]; j++) {
357 + envp[j] = envp[j+1];
361 + if (envp[i] && (strlen(envp[i]) > MAX_ENV_LENGTH)) {
363 + for (j = i; envp[j]; j++) {
364 + envp[j] = envp[j+1];
371 + eq = strchr(envp[i], '=');
374 + len = eq - envp[i];
375 + e = malloc(len + 1);
377 + bad = InternalError;
380 + strncpy(e, envp[i], len);
383 + (strcmp(e + len - 4, "PATH") == 0 ||
384 + strcmp(e, "TERMCAP") == 0)) {
385 + if (strlen(envp[i]) > MAX_ENV_PATH_LENGTH) {
401 + execve(XSERVER_PATH, argv, envp);
402 + fprintf(stderr, "execve failed for %s (errno %d)\n", XSERVER_PATH,
406 + fprintf(stderr, "Command line argument number %d is unsafe\n", i);
407 + fprintf(stderr, ARGMSG);
410 + fprintf(stderr, "Command line argument number %d is too long\n", i);
411 + fprintf(stderr, ARGMSG);
413 + case UnprintableArg:
414 + fprintf(stderr, "Command line argument number %d contains unprintable"
415 + " characters\n", i);
416 + fprintf(stderr, ARGMSG);
419 + fprintf(stderr, "Environment variable `%s' is too long\n", e);
420 + fprintf(stderr, ENVMSG);
422 + case InternalError:
423 + fprintf(stderr, "Internal Error\n");
427 + fprintf(stderr, "Authentication System Failure, "
428 + "missing or mangled PAM configuration file or module?\n");
430 + case PamAuthFailed:
431 + fprintf(stderr, "PAM authentication failed\n");
435 + fprintf(stderr, "Unknown error\n");
436 + fprintf(stderr, ARGMSG);
437 + fprintf(stderr, ENVMSG);
443 diff -urN xc.orig/programs/xinit/startx.cpp xc/programs/xinit/startx.cpp
444 --- xc.orig/programs/xinit/startx.cpp Mon Dec 30 15:54:10 2002
445 +++ xc/programs/xinit/startx.cpp Mon Dec 30 17:31:52 2002
447 sysclientrc=XINITDIR/xinitrc
448 sysserverrc=XINITDIR/xserverrc
449 defaultclient=BINDIR/xterm
450 -defaultserver=BINDIR/X
451 +defaultserver=BINDIR/Xwrapper
455 diff -urN xc.orig/programs/xinit/xinit.c xc/programs/xinit/xinit.c
456 --- xc.orig/programs/xinit/xinit.c Mon Dec 30 15:54:10 2002
457 +++ xc/programs/xinit/xinit.c Mon Dec 30 17:31:52 2002
462 +char *default_wrapper = BINDIR "/Xwrapper";
463 char *default_server = "X";
464 char *default_display = ":0"; /* choose most efficient */
465 char *default_client[] = {"xterm", "-geometry", "+1+1", "-n", "login", NULL};
469 (**argv != '/' && **argv != '.')) {
470 - *sptr++ = default_server;
471 + if (access(default_wrapper, X_OK) == 0)
472 + *sptr++ = default_wrapper;
474 + *sptr++ = default_server;
476 (**argv != '/' && **argv != '\\' && **argv != '.' &&
477 !(isalpha(**argv) && (*argv)[1]==':'))) {