]> mj.ucw.cz Git - suidgw.git/blob - suidgw.c
Handle saved uid/gid properly
[suidgw.git] / suidgw.c
1 /*
2  *  A simple gateway for set-uid scripts
3  *
4  *  (c) 2013 Martin Mares <mj@ucw.cz>
5  */
6
7 #undef DEBUG
8
9 #define _GNU_SOURCE
10
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <pwd.h>
15 #include <stdarg.h>
16 #include <stdbool.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <syslog.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
24 #define NONRET __attribute__((noreturn))
25 #define UNUSED __attribute__((unused))
26
27 #ifdef DEBUG
28 #define DBG(...) printf(__VA_ARGS__)
29 #else
30 #define DBG(...) do { } while (0)
31 #endif
32
33 static char program_name[PATH_MAX];
34 static char script_path[PATH_MAX];
35 static uid_t use_uid;
36 static gid_t use_gid;
37
38 static const char * const script_dirs[] = {
39   "/usr/local/lib/suidgw",
40   "/usr/lib/suidgw",
41 #ifdef DEBUG
42   "./scripts",
43 #endif
44   NULL
45 };
46
47 static char *sanitized_env[] = {
48   "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
49   NULL
50 };
51
52 static void NONRET die(const char *fmt, ...)
53 {
54   va_list args;
55   va_start(args, fmt);
56   fprintf(stderr, "suidgw: ");
57   vfprintf(stderr, fmt, args);
58   fputc('\n', stderr);
59   exit(1);
60 }
61
62 static void sanitize_fds(void)
63 {
64   // Make sure that nobody is playing dirty games with our file descriptors
65   int fd;
66   do
67     {
68       fd = open("/dev/null", O_RDWR);
69       if (fd < 0)
70         die("Cannot open /dev/null: %m");
71     }
72   while (fd <= 2);
73   close(fd);
74 }
75
76 static bool get_program_name(const char *arg0)
77 {
78   // If arg0 is a path, extract the last component
79   const char *p = strrchr(arg0, '/');
80   if (p)
81     p++;
82   else
83     p = arg0;
84
85   // Reject empty and oversized names
86   if (!p[0] || strlen(p) >= PATH_MAX)
87     return 0;
88
89   // Reject invalid characters
90   for (const char *q = p; *q; q++)
91     {
92       int c = *q;
93       if (! (c >= 'a' && c <= 'z' ||
94              c >= 'A' && c <= 'Z' ||
95              c >= '0' && c <= '9' ||
96              c == '-' ||
97              c == '_'))
98         return 0;
99     }
100
101   DBG("Program name: <%s>\n", p);
102   strcpy(program_name, p);
103   return 1;
104 }
105
106 static void find_script(void)
107 {
108   for (int i = 0; script_dirs[i]; i++)
109     {
110       int len = snprintf(script_path, PATH_MAX, "%s/%s", script_dirs[i], program_name);
111       if (len >= PATH_MAX)
112         continue;
113       DBG("Trying <%s>\n", script_path);
114       if (access(script_path, X_OK) >= 0)
115         {
116           DBG("Found <%s>\n", script_path);
117           return;
118         }
119       if (errno != ENOENT)
120         die("Cannot run %s: %m", script_path);
121     }
122
123   die("Unable to find script %s", program_name);
124 }
125
126 static void check_stat(void)
127 {
128   struct stat st;
129   if (stat(script_path, &st) < 0)
130     die("Cannot stat %s: %m", script_path);
131
132   if (!S_ISREG(st.st_mode))
133     die("Not a regular file: %s", script_path);
134   if (!(st.st_mode & (S_ISUID | S_ISGID)))
135     die("Missing suid/sgid: %s", script_path);
136
137   use_uid = (st.st_mode & S_ISUID) ? st.st_uid : getuid();
138   use_gid = (st.st_mode & S_ISGID) ? st.st_gid : getgid();
139   DBG("Will use uid=%d gid=%d\n", (int) use_uid, (int) use_gid);
140 }
141
142 static void switch_ugid(void)
143 {
144   if (setresgid(getgid(), use_gid, use_gid) < 0)
145     die("Failed to set group id: %m");
146
147   if (setresuid(getuid(), use_uid, use_uid) < 0)
148     die("Failed to set user id: %m");
149 }
150
151 int main(int argc UNUSED, char **argv)
152 {
153   sanitize_fds();
154
155   if (geteuid())
156     die("Must be run setuid");
157
158   struct passwd *pwd = getpwuid(getuid());
159   if (!pwd)
160     die("You don't exist");
161
162   openlog("suidgw", LOG_NDELAY | LOG_PID, LOG_AUTH);
163
164   if (!get_program_name(argv[0]))
165     die("Unable to parse program name %s", argv[0]);
166
167   find_script();
168   check_stat();
169   switch_ugid();
170
171   syslog(LOG_INFO, "User %s executes %s with uid=%d, gid=%d",
172     pwd->pw_name, script_path, (int) use_uid, (int) use_gid);
173
174   if (execve(script_path, argv, sanitized_env) < 0)
175     die("Cannot execute %s: %m", script_path);
176
177   die("This must never happen");
178 }