/[path-sandbox]/trunk/sandbox_futils.c
Gentoo

Contents of /trunk/sandbox_futils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (hide annotations) (download) (as text)
Thu May 12 00:07:21 2005 UTC (13 years, 3 months ago) by azarah
File MIME type: text/x-csrc
File size: 11876 byte(s)
More path limit fixes.  Declare SB_BUF_LEN global and use it where needed.

1 ferringb 2 /*
2     * Copyright (C) 2002 Brad House <brad@mainstreetsoftworks.com>
3     * Distributed under the terms of the GNU General Public License, v2 or later
4     * Author: Brad House <brad@mainstreetsoftworks.com>
5     *
6     * $Header$
7     *
8     */
9    
10     #include <errno.h>
11     #include <fcntl.h>
12     #include <signal.h>
13     #include <stdio.h>
14     #include <stdlib.h>
15     #include <limits.h>
16     #include <string.h>
17     #include <stdarg.h>
18     #include <sys/file.h>
19     #include <sys/stat.h>
20     #include <sys/time.h>
21     #include <sys/types.h>
22     #include <sys/resource.h>
23     #include <sys/wait.h>
24     #include <unistd.h>
25     #include <fcntl.h>
26    
27     #include <grp.h>
28     #include <pwd.h>
29    
30     #include "sandbox.h"
31 ferringb 13 #include "config.h"
32 ferringb 2
33     /* BEGIN Prototypes */
34 azarah 43 SB_STATIC int file_security_check(char *filename);
35 ferringb 2 /* END Prototypes */
36    
37     /* glibc modified getcwd() functions */
38 azarah 43 SB_STATIC char *egetcwd(char *, size_t);
39 ferringb 2
40 azarah 43 SB_STATIC char *get_sandbox_path(char *argv0)
41 ferringb 2 {
42 azarah 92 char path[SB_PATH_MAX];
43 ferringb 2
44     memset(path, 0, sizeof(path));
45     /* ARGV[0] specifies full path */
46     if (argv0[0] == '/') {
47 azarah 30 strncpy(path, argv0, sizeof(path) - 1);
48 ferringb 2
49     /* ARGV[0] specifies relative path */
50     } else {
51 azarah 92 if (-1 != readlink("/proc/self/exe", path, sizeof(path)))
52     path[sizeof(path) - 1] = '\0';
53     else
54     path[0] = '\0';
55 ferringb 2 }
56    
57     /* Return just directory */
58     return (sb_dirname(path));
59     }
60    
61 azarah 43 SB_STATIC char *get_sandbox_lib(char *sb_path)
62 ferringb 2 {
63 azarah 92 char path[SB_PATH_MAX];
64 ferringb 2
65     #ifdef SB_HAVE_64BIT_ARCH
66 azarah 30 snprintf(path, sizeof(path), "%s", LIB_NAME);
67 ferringb 2 #else
68 azarah 30 snprintf(path, sizeof(path), "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
69 ferringb 2 if (file_exist(path, 0) <= 0) {
70     snprintf(path, sizeof(path), "%s%s", sb_path, LIB_NAME);
71     }
72     #endif
73     return (strdup(path));
74     }
75    
76 azarah 43 SB_STATIC char *get_sandbox_pids_file(void)
77 ferringb 2 {
78     if (0 < getenv("SANDBOX_PIDS_FILE")) {
79     return (strdup(getenv("SANDBOX_PIDS_FILE")));
80     }
81     return (strdup(PIDS_FILE));
82     }
83    
84 azarah 43 SB_STATIC char *get_sandbox_rc(char *sb_path)
85 ferringb 2 {
86 azarah 92 char path[SB_PATH_MAX];
87 ferringb 2
88 ferringb 13 snprintf(path, sizeof(path), "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
89 ferringb 2 if (file_exist(path, 0) <= 0) {
90     snprintf(path, sizeof(path), "%s%s", sb_path, BASHRC_NAME);
91     }
92     return (strdup(path));
93     }
94    
95 azarah 43 SB_STATIC char *get_sandbox_log()
96 ferringb 2 {
97 azarah 92 char path[SB_PATH_MAX];
98 ferringb 2 char *sandbox_log_env = NULL;
99    
100     /* THIS CHUNK BREAK THINGS BY DOING THIS:
101     * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
102     */
103    
104     sandbox_log_env = getenv(ENV_SANDBOX_LOG);
105 azarah 30 snprintf(path, sizeof(path) - 1, "%s%s%s%d%s", LOG_FILE_PREFIX,
106     (sandbox_log_env == NULL ? "" : sandbox_log_env),
107     (sandbox_log_env == NULL ? "" : "-"), getpid(), LOG_FILE_EXT);
108 ferringb 2 return (strdup(path));
109     }
110    
111     /* Obtain base directory name. Do not allow trailing / */
112 azarah 43 SB_STATIC char *sb_dirname(const char *path)
113 ferringb 2 {
114     char *ret = NULL;
115     char *ptr = NULL;
116     int loc = 0, i;
117     int cut_len = -1;
118    
119     /* don't think NULL will ever be passed, but just in case */
120     if (NULL == path)
121     return (strdup("."));
122    
123     /* Grab pointer to last slash */
124     ptr = strrchr(path, '/');
125     if (NULL == ptr) {
126     return (strdup("."));
127     }
128    
129     /* decimal location of pointer */
130     loc = ptr - path;
131    
132     /* Remove any trailing slash */
133     for (i = loc - 1; i >= 0; i--) {
134     if (path[i] != '/') {
135 azarah 30 cut_len = i + 1; /* make cut_len the length of the string to keep */
136 ferringb 2 break;
137     }
138     }
139    
140     /* It could have been just a plain /, return a 1byte 0 filled string */
141     if (-1 == cut_len)
142     return (strdup(""));
143    
144     /* Allocate memory, and return the directory */
145 azarah 30 ret = (char *)malloc((cut_len + 1) * sizeof(char));
146 ferringb 2 memcpy(ret, path, cut_len);
147     ret[cut_len] = 0;
148    
149     return (ret);
150     }
151    
152     /*
153 azarah 43 SB_STATIC char* dirname(const char* path)
154 ferringb 2 {
155     char* base = NULL;
156     unsigned int length = 0;
157    
158     base = strrchr(path, '/');
159     if (NULL == base)
160     {
161     return strdup(".");
162     }
163     while (base > path && *base == '/')
164     {
165     base--;
166     }
167     length = (unsigned int) 1 + base - path;
168    
169     base = malloc(sizeof(char)*(length+1));
170     memmove(base, path, length);
171     base[length] = 0;
172    
173     return base;
174     }*/
175    
176     /* Convert text (string) modes to integer values */
177 azarah 43 SB_STATIC int file_getmode(char *mode)
178 ferringb 2 {
179     int mde = 0;
180     if (0 == strcasecmp(mode, "r+")) {
181     mde = O_RDWR | O_CREAT;
182     } else if (0 == strcasecmp(mode, "w+")) {
183     mde = O_RDWR | O_CREAT | O_TRUNC;
184     } else if (0 == strcasecmp(mode, "a+")) {
185     mde = O_RDWR | O_CREAT | O_APPEND;
186     } else if (0 == strcasecmp(mode, "r")) {
187     mde = O_RDONLY;
188     } else if (0 == strcasecmp(mode, "w")) {
189     mde = O_WRONLY | O_CREAT | O_TRUNC;
190     } else if (0 == strcasecmp(mode, "a")) {
191     mde = O_WRONLY | O_APPEND | O_CREAT;
192     } else {
193     mde = O_RDONLY;
194     }
195     return (mde);
196     }
197    
198     /* Get current position in file */
199 azarah 43 SB_STATIC long file_tell(int fp)
200 ferringb 2 {
201     return (lseek(fp, 0L, SEEK_CUR));
202     }
203    
204     /* lock the file, preferrably the POSIX way */
205 azarah 43 SB_STATIC int file_lock(int fd, int lock, char *filename)
206 ferringb 2 {
207     int err;
208     #ifdef USE_FLOCK
209     if (flock(fd, lock) < 0) {
210     err = errno;
211     fprintf(stderr, ">>> %s flock file lock: %s\n", filename, strerror(err));
212     return 0;
213     }
214     #else
215     struct flock fl;
216     fl.l_type = lock;
217     fl.l_whence = SEEK_SET;
218     fl.l_start = 0L;
219     fl.l_len = 0L;
220     fl.l_pid = getpid();
221     if (fcntl(fd, F_SETLKW, &fl) < 0) {
222     err = errno;
223     fprintf(stderr, ">>> %s fcntl file lock: %s\n", filename, strerror(err));
224     return 0;
225     }
226     #endif
227     return 1;
228     }
229    
230     /* unlock the file, preferrably the POSIX way */
231 azarah 43 SB_STATIC int file_unlock(int fd)
232 ferringb 2 {
233     #ifdef USE_FLOCK
234     if (flock(fd, LOCK_UN) < 0) {
235     perror(">>> flock file unlock");
236     return 0;
237     }
238     #else
239     struct flock fl;
240     fl.l_type = F_UNLCK;
241     fl.l_whence = SEEK_SET;
242     fl.l_start = 0L;
243     fl.l_len = 0L;
244     fl.l_pid = getpid();
245     if (fcntl(fd, F_SETLKW, &fl) < 0) {
246     perror(">>> fcntl file unlock");
247     return 0;
248     }
249     #endif
250     return 1;
251     }
252    
253     /* Auto-determine from how the file was opened, what kind of lock to lock
254     * the file with
255     */
256 azarah 43 SB_STATIC int file_locktype(char *mode)
257 ferringb 2 {
258     #ifdef USE_FLOCK
259     if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+'))
260 azarah 30 || (NULL != strchr(mode, 'a')))
261 ferringb 2 return (LOCK_EX);
262     return (LOCK_SH);
263     #else
264     if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+'))
265 azarah 30 || (NULL != strchr(mode, 'a')))
266 ferringb 2 return (F_WRLCK);
267     return (F_RDLCK);
268     #endif
269     }
270    
271     /* Use standard fopen style modes to open the specified file. Also auto-determines and
272     * locks the file either in shared or exclusive mode depending on opening mode
273     */
274 azarah 43 SB_STATIC int file_open(char *filename, char *mode, int perm_specified, ...)
275 ferringb 2 {
276     int fd;
277 azarah 93 char error[SB_BUF_LEN];
278 ferringb 2 va_list ap;
279     int perm;
280     char *group = NULL;
281     struct group *group_struct;
282 azarah 30
283 ferringb 2 file_security_check(filename);
284    
285     if (perm_specified) {
286     va_start(ap, perm_specified);
287     perm = va_arg(ap, int);
288     group = va_arg(ap, char *);
289     va_end(ap);
290     }
291     fd = open(filename, file_getmode(mode));
292     file_security_check(filename);
293     if (-1 == fd) {
294     snprintf(error, sizeof(error), ">>> %s file mode: %s open", filename, mode);
295     perror(error);
296     return (fd);
297     }
298     if (perm_specified) {
299     if (fchmod(fd, 0664) && (0 == getuid())) {
300     snprintf(error, sizeof(error), ">>> Could not set mode: %s", filename);
301     perror(error);
302     }
303     }
304     if (NULL != group) {
305     group_struct = getgrnam(group);
306 azarah 82 if (NULL == group_struct) {
307 ferringb 2 snprintf(error, sizeof(error), ">>> Could not get grp number: %s", group);
308     perror(error);
309     } else {
310     if (fchown(fd, -1, group_struct->gr_gid) && (0 == getuid())) {
311     snprintf(error, sizeof(error), ">>> Could not set group: %s", filename);
312     perror(error);
313     }
314     }
315     }
316     /* Only lock the file if opening succeeded */
317     if (-1 != fd) {
318 azarah 30 if (file_security_check(filename) != 0) {
319     /* Security violation occured between the last check and the */
320 ferringb 2 /* creation of the file. As SpanKY pointed out there is a race */
321     /* condition here, so if there is a problem here we'll mesg and */
322     /* bail out to avoid it until we can work and test a better fix. */
323     fprintf(stderr, "\n\nSECURITY RACE CONDITION: Problem recurred after creation!\nBAILING OUT\n\n");
324     exit(127);
325     }
326    
327     if (0 == file_lock(fd, file_locktype(mode), filename)) {
328     close(fd);
329     return -1;
330     }
331     } else {
332     snprintf(error, sizeof(error), ">>> %s file mode:%s open", filename, mode);
333     perror(error);
334     }
335     return (fd);
336     }
337    
338     /* Close and unlock file */
339 azarah 43 SB_STATIC void file_close(int fd)
340 ferringb 2 {
341     if (-1 != fd) {
342     file_unlock(fd);
343     close(fd);
344     }
345     }
346    
347     /* Return length of file */
348 azarah 43 SB_STATIC long file_length(int fd)
349 ferringb 2 {
350     long pos, len;
351     pos = file_tell(fd);
352     len = lseek(fd, 0L, SEEK_END);
353     lseek(fd, pos, SEEK_SET);
354     return (len);
355     }
356    
357     /* Zero out file */
358 azarah 43 SB_STATIC int file_truncate(int fd)
359 ferringb 2 {
360     lseek(fd, 0L, SEEK_SET);
361     if (ftruncate(fd, 0) < 0) {
362     perror(">>> file truncate");
363     return 0;
364     }
365     return 1;
366     }
367    
368     /* Check to see if a file exists Return: 1 success, 0 file not found, -1 error */
369 azarah 43 SB_STATIC int file_exist(char *filename, int checkmode)
370 ferringb 2 {
371     struct stat mystat;
372    
373     /* Verify file exists and is regular file (not sym link) */
374     if (checkmode) {
375     if (-1 == lstat(filename, &mystat)) {
376     /* file doesn't exist */
377     if (ENOENT == errno) {
378     return 0;
379 azarah 30 } else { /* permission denied or other error */
380 ferringb 2 perror(">>> stat file");
381     return -1;
382     }
383     }
384     if (!S_ISREG(mystat.st_mode))
385     return -1;
386    
387     /* Just plain verify the file exists */
388     } else {
389     if (-1 == stat(filename, &mystat)) {
390     /* file does not exist */
391     if (ENOENT == errno) {
392     return 0;
393 azarah 30 } else { /* permission denied or other error */
394 ferringb 2 perror(">>> stat file");
395     return -1;
396     }
397     }
398     }
399    
400     return 1;
401     }
402    
403 azarah 43 SB_STATIC int file_security_check(char *filename)
404 azarah 30 { /* 0 == fine, >0 == problem */
405 ferringb 2 struct stat stat_buf;
406     struct group *group_buf;
407     struct passwd *passwd_buf;
408 azarah 30
409 ferringb 2 passwd_buf = getpwnam("portage");
410     group_buf = getgrnam("portage");
411    
412 azarah 30 if ((lstat(filename, &stat_buf) == -1) && (errno == ENOENT)) {
413 ferringb 2 /* Doesn't exist. */
414     return 0;
415 azarah 30 } else {
416     if ((stat_buf.st_nlink) > 1) { /* Security: We are handlinked... */
417     if (unlink(filename)) {
418     fprintf(stderr, "Unable to delete file in security violation (hardlinked): %s\n", filename);
419 ferringb 2 exit(127);
420     }
421 azarah 30 fprintf(stderr, "File in security violation (hardlinked): %s\n", filename);
422 ferringb 2 return 1;
423 azarah 30 } else if (S_ISLNK(stat_buf.st_mode)) { /* Security: We are a symlink? */
424     fprintf(stderr, "File in security violation (symlink): %s\n", filename);
425 ferringb 2 exit(127);
426 azarah 30 } else if (0 == S_ISREG(stat_buf.st_mode)) { /* Security: special file */
427     fprintf(stderr, "File in security violation (not regular): %s\n", filename);
428 ferringb 2 exit(127);
429 azarah 30 } else if (stat_buf.st_mode & S_IWOTH) { /* Security: We are o+w? */
430     if (unlink(filename)) {
431     fprintf(stderr, "Unable to delete file in security violation (world write): %s\n", filename);
432 ferringb 2 exit(127);
433     }
434 azarah 30 fprintf(stderr, "File in security violation (world write): %s\n", filename);
435 ferringb 2 return 1;
436 azarah 30 } else
437     if (!((stat_buf.st_uid == 0) || (stat_buf.st_uid == getuid()) ||
438     ((passwd_buf != NULL) && (stat_buf.st_uid == passwd_buf->pw_uid))) ||
439     !((stat_buf.st_gid == 0) || (stat_buf.st_gid == getgid()) ||
440     ((group_buf != NULL) && (stat_buf.st_gid == group_buf->gr_gid)))) {
441     /* Security: Owner/Group isn't right. */
442    
443 ferringb 2 /* uid = 0 or myuid or portage */
444     /* gid = 0 or mygid or portage */
445 azarah 30
446     if (0) {
447 ferringb 2 fprintf(stderr, "--1: %d,%d,%d,%d\n--2: %d,%d,%d,%d\n",
448     (stat_buf.st_uid == 0),
449     (stat_buf.st_uid == getuid()),
450 azarah 30 (passwd_buf != NULL),
451     (passwd_buf != NULL) ? (stat_buf.st_uid == passwd_buf->pw_uid) : -1,
452     (stat_buf.st_gid == 0),
453 ferringb 2 (stat_buf.st_gid == getgid()),
454 azarah 30 (group_buf != NULL),
455     (group_buf != NULL) ? (stat_buf.st_gid == group_buf->gr_gid) : -1);
456 ferringb 2 }
457 azarah 30
458 ferringb 2 /* manpage: "The return value may point to static area" */
459     /* DO NOT ACTUALLY FREE THIS... It'll segfault. */
460     /* if(passwd_buf != NULL) { free(passwd_buf); } */
461     /* if(group_buf != NULL) { free(group_buf); } */
462 azarah 30
463     if (unlink(filename)) {
464     fprintf(stderr, "Unable to delete file in security violation (bad owner/group): %s\n", filename);
465 ferringb 2 exit(127);
466     }
467 azarah 30 fprintf(stderr, "File in security violation (bad owner/group): %s\n", filename);
468 ferringb 2 return 1;
469     }
470 azarah 30 } /* Stat */
471 ferringb 2 return 0;
472     }
473    
474 azarah 30 // vim:noexpandtab noai:cindent ai

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20