2 * A simple gateway for set-uid scripts
4 * (c) 2013 Martin Mares <mj@ucw.cz>
24 #define NONRET __attribute__((noreturn))
25 #define UNUSED __attribute__((unused))
28 #define DBG(...) printf(__VA_ARGS__)
30 #define DBG(...) do { } while (0)
33 static char program_name[PATH_MAX];
34 static char script_path[PATH_MAX];
38 static const char * const script_dirs[] = {
39 "/usr/local/lib/suidgw",
47 static char env_orig_uid[32], env_orig_gid[32];
49 static char *sanitized_env[] = {
50 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
56 static void NONRET die(const char *fmt, ...)
60 fprintf(stderr, "suidgw: ");
61 vfprintf(stderr, fmt, args);
66 static void sanitize_fds(void)
68 // Make sure that nobody is playing dirty games with our file descriptors
72 fd = open("/dev/null", O_RDWR);
74 die("Cannot open /dev/null: %m");
80 static void sanitize_env(void)
82 snprintf(env_orig_uid, sizeof(env_orig_uid), "ORIG_UID=%d", (int) getuid());
83 snprintf(env_orig_gid, sizeof(env_orig_gid), "ORIG_GID=%d", (int) getgid());
86 static void get_script_name(const char *arg0)
88 // If arg0 is a path, extract the last component
89 const char *p = strrchr(arg0, '/');
95 // Reject empty and oversized names
97 die("Script name is empty");
98 if (strlen(p) >= PATH_MAX)
99 die("Script name too long");
101 // Reject invalid characters
102 for (const char *q = p; *q; q++)
105 if (! (c >= 'a' && c <= 'z' ||
106 c >= 'A' && c <= 'Z' ||
107 c >= '0' && c <= '9' ||
111 die("Script name contains an invalid character 0x%02x", c);
114 // Reject "." and ".."
115 if (!strcmp(p, ".") || !strcmp(p, ".."))
116 die("Script name must not be `.' or `..'");
118 DBG("Program name: <%s>\n", p);
119 strcpy(program_name, p);
122 static void find_script(void)
124 for (int i = 0; script_dirs[i]; i++)
126 int len = snprintf(script_path, PATH_MAX, "%s/%s", script_dirs[i], program_name);
129 DBG("Trying <%s>\n", script_path);
130 if (access(script_path, X_OK) >= 0)
132 DBG("Found <%s>\n", script_path);
136 die("Cannot run %s: %m", script_path);
139 die("Unable to find script %s", program_name);
142 static void check_stat(void)
145 if (stat(script_path, &st) < 0)
146 die("Cannot stat %s: %m", script_path);
148 if (!S_ISREG(st.st_mode))
149 die("Not a regular file: %s", script_path);
150 if (!(st.st_mode & (S_ISUID | S_ISGID)))
151 die("Missing suid/sgid: %s", script_path);
153 use_uid = (st.st_mode & S_ISUID) ? st.st_uid : getuid();
154 use_gid = (st.st_mode & S_ISGID) ? st.st_gid : getgid();
155 DBG("Will use uid=%d gid=%d\n", (int) use_uid, (int) use_gid);
158 static void switch_ugid(void)
160 if (setresgid(use_gid, use_gid, use_gid) < 0)
161 die("Failed to set group id: %m");
163 if (setresuid(use_uid, use_uid, use_uid) < 0)
164 die("Failed to set user id: %m");
167 int main(int argc UNUSED, char **argv)
173 die("Must be run setuid");
175 struct passwd *pwd = getpwuid(getuid());
177 die("You don't exist");
179 openlog("suidgw", LOG_NDELAY | LOG_PID, LOG_AUTH);
181 get_script_name(argv[0]);
186 syslog(LOG_INFO, "User %s executes %s with uid=%d, gid=%d",
187 pwd->pw_name, script_path, (int) use_uid, (int) use_gid);
189 if (execve(script_path, argv, sanitized_env) < 0)
190 die("Cannot execute %s: %m", script_path);
192 die("This must never happen");