Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 21766 | Differences between
and this patch

Collapse All | Expand All

(-)ChangeLog (+16 lines)
Lines 2-7 Link Here
2
# Copyright 2002 Gentoo Technologies, Inc.; Distributed under the GPL v2
2
# Copyright 2002 Gentoo Technologies, Inc.; Distributed under the GPL v2
3
# $Header: /home/cvsroot/gentoo-src/portage/src/sandbox-1.1/ChangeLog,v 1.17 2003/06/29 16:20:19 azarah Exp $
3
# $Header: /home/cvsroot/gentoo-src/portage/src/sandbox-1.1/ChangeLog,v 1.17 2003/06/29 16:20:19 azarah Exp $
4
4
5
  27 Jul 2003; Martin Schlemmer <azarah@gentoo.org> getcwd.c, libsandbox.c,
6
  sandbox_futils.c, canonicalize.c :
7
  Once again coreutils fails, as my systems had 2.5 kernel, the getcwd system
8
  call handled strings larger than PATH_MAX (bug #21766).  It however does not
9
  work the same on 2.4 kernels.
10
11
  To fix, I added the posix implementation of getcwd() (from glibc cvs) that
12
  do not need the system call.  We use the default getcwd() function via a
13
  wrapper (egetcwd), and then lstat the returned path.  If lstat fails, it
14
  means the current directory was removed, OR that the the system call for
15
  getcwd failed (curious is that it do not fail and return NULL or set
16
  errno, but rather just truncate the retured directory - usually from the
17
  start), and if so, we use the generic getcwd() function (__egetcwd).  Note
18
  that we do not use the generic version all the time, as it calls lstat()
19
  a great number of times, and performance degrade much.
20
5
  29 Jun 2003; Martin Schlemmer <azarah@gentoo.org> create-localdecls,
21
  29 Jun 2003; Martin Schlemmer <azarah@gentoo.org> create-localdecls,
6
  libsandbox.c :
22
  libsandbox.c :
7
  Make sure SB_PATH_MAX will not wrap.  Fix two possible memory leaks.
23
  Make sure SB_PATH_MAX will not wrap.  Fix two possible memory leaks.
(-)Makefile (-6 / +3 lines)
Lines 22-28 Link Here
22
24
23
all:	$(TARGETS)
25
all:	$(TARGETS)
24
26
25
sandbox: sandbox.o sandbox_futils.o
27
sandbox: sandbox.o sandbox_futils.o getcwd.c
26
	$(CC) $^ -ldl -lc -o $@
28
	$(CC) $^ -ldl -lc -o $@
27
29
28
sandbox.o: sandbox.c sandbox.h
30
sandbox.o: sandbox.c sandbox.h
Lines 31-44 Link Here
31
sandbox_futils.o: sandbox_futils.c sandbox.h
33
sandbox_futils.o: sandbox_futils.c sandbox.h
32
	$(CC) $(CFLAGS) -Wall -c $(OBJ_DEFINES) sandbox_futils.c
34
	$(CC) $(CFLAGS) -Wall -c $(OBJ_DEFINES) sandbox_futils.c
33
35
34
libsandbox.so: libsandbox.o sandbox_futils.o canonicalize.o
36
libsandbox.so: libsandbox.o sandbox_futils.o
35
	$(CC) $^ -shared -fPIC -ldl -lc -o $@ -nostdlib -lgcc
37
	$(CC) $^ -shared -fPIC -ldl -lc -o $@ -nostdlib -lgcc
36
38
37
libsandbox.o: libsandbox.c localdecls.h
39
libsandbox.o: libsandbox.c localdecls.h canonicalize.c getcwd.c
38
	$(CC) $(CFLAGS) -Wall -c $(OBJ_DEFINES) libsandbox.c
40
	$(CC) $(CFLAGS) -Wall -c $(OBJ_DEFINES) libsandbox.c
39
40
canonicalize.o: canonicalize.c localdecls.h
41
	$(CC) $(CFLAGS) -Wall -c $(OBJ_DEFINES) canonicalize.c
42
41
43
localdecls.h: create-localdecls libctest.c
42
localdecls.h: create-localdecls libctest.c
44
	./create-localdecls
43
	./create-localdecls
(-)canonicalize.c (-63 / +38 lines)
Lines 1-5 Link Here
1
/* Return the canonical absolute name of a given file.
1
/* Return the canonical absolute name of a given file.
2
   Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2
   Copyright (C) 1996-2001, 2002 Free Software Foundation, Inc.
3
   This file is part of the GNU C Library.
3
   This file is part of the GNU C Library.
4
4
5
   The GNU C Library is free software; you can redistribute it and/or
5
   The GNU C Library is free software; you can redistribute it and/or
Lines 17-26 Link Here
17
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18
   02111-1307 USA.  */
18
   02111-1307 USA.  */
19
19
20
/*
21
 * $Header: /home/cvsroot/gentoo-src/portage/src/sandbox-1.1/canonicalize.c,v 1.3 2003/06/22 20:01:20 azarah Exp $
22
 */
23
24
#include <stdlib.h>
20
#include <stdlib.h>
25
#include <string.h>
21
#include <string.h>
26
#include <unistd.h>
22
#include <unistd.h>
Lines 30-36 Link Here
30
#include <errno.h>
26
#include <errno.h>
31
#include <stddef.h>
27
#include <stddef.h>
32
28
33
#include "localdecls.h"
29
//#include <shlib-compat.h>
34
                     
30
                     
35
#ifndef __set_errno
31
#ifndef __set_errno
36
# define __set_errno(val) errno = (val)
32
# define __set_errno(val) errno = (val)
Lines 55-85 Link Here
55
 *  
51
 *  
56
 */
52
 */
57
53
58
static char *
54
char *
59
ecanonicalize (const char *name, char *resolved)
55
erealpath(const char *name, char *resolved)
60
{
56
{
61
  char *rpath, *dest;
57
  char *rpath, *dest;
62
  const char *start, *end, *rpath_limit;
58
  const char *start, *end, *rpath_limit;
63
  long int path_max;
59
  long int path_max;
64
60
65
  if (name == NULL)
61
	if (name == NULL) {
66
    {
67
      /* As per Single Unix Specification V2 we must return an error if
62
      /* As per Single Unix Specification V2 we must return an error if
68
         either parameter is a null pointer.  We extend this to allow
63
         either parameter is a null pointer.  We extend this to allow
69
         the RESOLVED parameter to be NULL in case the we are expected to
64
         the RESOLVED parameter to be NULL in case the we are expected to
70
         allocate the room for the return value.  */
65
         allocate the room for the return value.  */
71
      __set_errno (EINVAL);
66
		__set_errno(EINVAL);
72
      return NULL;
67
      return NULL;
73
    }
68
    }
74
69
75
  if (name[0] == '\0')
70
	if (name[0] == '\0') {
76
    {
77
      /* As per Single Unix Specification V2 we must return an error if
71
      /* As per Single Unix Specification V2 we must return an error if
78
         the name argument points to an empty string.  */
72
         the name argument points to an empty string.  */
79
      __set_errno (ENOENT);
73
		__set_errno(ENOENT);
80
      return NULL;
74
      return NULL;
81
    }
75
    }
82
83
#ifdef SB_PATH_MAX
76
#ifdef SB_PATH_MAX
84
  path_max = SB_PATH_MAX;
77
  path_max = SB_PATH_MAX;
85
#else
78
#else
Lines 83-145 Link Here
83
#ifdef SB_PATH_MAX
76
#ifdef SB_PATH_MAX
84
  path_max = SB_PATH_MAX;
77
  path_max = SB_PATH_MAX;
85
#else
78
#else
86
  path_max = pathconf (name, _PC_PATH_MAX);
79
	path_max = pathconf(name, _PC_SB_PATH_MAX);
87
  if (path_max <= 0)
80
  if (path_max <= 0)
88
    path_max = 1024;
81
    path_max = 1024;
89
#endif
82
#endif
90
83
91
  rpath = resolved ? alloca (path_max) : malloc (path_max);
84
	if (resolved == NULL) {
85
		rpath = malloc(path_max);
86
		if (rpath == NULL)
87
			return NULL;
88
	} else
89
		rpath = resolved;
92
  rpath_limit = rpath + path_max;
90
  rpath_limit = rpath + path_max;
93
91
94
  if (name[0] != '/')
92
	if (name[0] != '/') {
95
    {
93
		if (!egetcwd(rpath, path_max)) {
96
      if (!getcwd (rpath, path_max))
97
        {
98
          rpath[0] = '\0';
94
          rpath[0] = '\0';
99
          goto error;
95
          goto error;
100
        }
96
        }
101
      dest = strchr (rpath, '\0');
97
		dest = strchr(rpath, '\0');
102
    }
98
	} else {
103
  else
104
    {
105
      rpath[0] = '/';
99
      rpath[0] = '/';
106
      dest = rpath + 1;
100
      dest = rpath + 1;
107
    }
101
    }
108
102
109
  for (start = end = name; *start; start = end)
103
	for (start = end = name; *start; start = end) {
110
    {
111
      /* Skip sequence of multiple path-separators.  */
104
      /* Skip sequence of multiple path-separators.  */
112
      while (*start == '/')
105
      while (*start == '/')
113
        ++start;
106
        ++start;
114
107
115
      /* Find end of path component.  */
108
      /* Find end of path component.  */
116
      for (end = start; *end && *end != '/'; ++end)
109
      for (end = start; *end && *end != '/'; ++end)
117
        /* Nothing.  */;
110
			/* Nothing.  */ ;
118
111
119
      if (end - start == 0)
112
      if (end - start == 0)
120
        break;
113
        break;
121
      else if (end - start == 1 && start[0] == '.')
114
      else if (end - start == 1 && start[0] == '.')
122
        /* nothing */;
115
			/* nothing */ ;
123
      else if (end - start == 2 && start[0] == '.' && start[1] == '.')
116
		else if (end - start == 2 && start[0] == '.' && start[1] == '.') {
124
        {
125
          /* Back up to previous component, ignore if at root already.  */
117
          /* Back up to previous component, ignore if at root already.  */
126
          if (dest > rpath + 1)
118
          if (dest > rpath + 1)
127
            while ((--dest)[-1] != '/');
119
				while ((--dest)[-1] != '/') ;
128
        }
120
		} else {
129
      else
130
        {
131
          size_t new_size;
121
          size_t new_size;
132
122
133
          if (dest[-1] != '/')
123
          if (dest[-1] != '/')
134
            *dest++ = '/';
124
            *dest++ = '/';
135
125
136
          if (dest + (end - start) >= rpath_limit)
126
			if (dest + (end - start) >= rpath_limit) {
137
            {
138
              ptrdiff_t dest_offset = dest - rpath;
127
              ptrdiff_t dest_offset = dest - rpath;
128
				char *new_rpath;
139
129
140
              if (resolved)
130
				if (resolved) {
141
                {
131
					__set_errno(ENAMETOOLONG);
142
                  __set_errno (ENAMETOOLONG);
143
                  if (dest > rpath + 1)
132
                  if (dest > rpath + 1)
144
                    dest--;
133
                    dest--;
145
                  *dest = '\0';
134
                  *dest = '\0';
Lines 150-196 Link Here
150
                new_size += end - start + 1;
139
                new_size += end - start + 1;
151
              else
140
              else
152
                new_size += path_max;
141
                new_size += path_max;
153
              rpath = realloc (rpath, new_size);
142
				new_rpath = (char *) realloc(rpath, new_size);
143
				if (new_rpath == NULL)
144
					goto error;
145
				rpath = new_rpath;
154
              rpath_limit = rpath + new_size;
146
              rpath_limit = rpath + new_size;
155
              if (rpath == NULL)
156
                return NULL;
157
147
158
              dest = rpath + dest_offset;
148
              dest = rpath + dest_offset;
159
            }
149
            }
160
150
161
          dest = __mempcpy (dest, start, end - start);
151
			dest = __mempcpy(dest, start, end - start);
162
          *dest = '\0';
152
          *dest = '\0';
163
164
          }
153
          }
165
    }
154
    }
166
#if 0
155
#if 1
167
  if (dest > rpath + 1 && dest[-1] == '/')
156
  if (dest > rpath + 1 && dest[-1] == '/')
168
    --dest;
157
    --dest;
169
#endif
158
#endif
170
  *dest = '\0';
159
  *dest = '\0';
171
160
172
  return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath;
161
	return resolved ? memcpy(resolved, rpath, dest - rpath + 1) : rpath;
173
162
174
error:
163
error:
175
  if (resolved)
164
  if (resolved)
176
    strcpy (resolved, rpath);
165
		strcpy(resolved, rpath);
177
  else
166
  else
178
    free (rpath);
167
		free(rpath);
179
  return NULL;
180
}
181
182
183
char *
184
erealpath (const char *name, char *resolved)
185
{
186
  if (resolved == NULL)
187
    {
188
      __set_errno (EINVAL);
189
      return NULL;
168
      return NULL;
190
    }
191
192
  return ecanonicalize (name, resolved);
193
}
169
}
194
195
170
196
// vim:expandtab noai:cindent ai
171
// vim:expandtab noai:cindent ai
(-)libctest.c (-2 / +3 lines)
Lines 1-6 Link Here
1
/* Dummy program to check your libc version */
1
/* Dummy program to check your libc version */
2
2
3
int main(void) {
3
int
4
main(void)
5
{
4
	return 0;
6
	return 0;
5
}
7
}
6
(-)libsandbox.c (-175 / +253 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
S *  Path sandbox for the gentoo linux portage package system, initially
2
 *  Path sandbox for the gentoo linux portage package system, initially
3
 *  based on the ROCK Linux Wrapper for getting a list of created files
3
 *  based on the ROCK Linux Wrapper for getting a list of created files
4
 *
4
 *
5
 *  to integrate with bash, bash should have been built like this
5
 *  to integrate with bash, bash should have been built like this
Lines 121-140 Link Here
121
119
122
typedef struct {
120
typedef struct {
123
  int show_access_violation;
121
  int show_access_violation;
124
  char** deny_prefixes;
122
	char **deny_prefixes;
125
  int num_deny_prefixes;
123
  int num_deny_prefixes;
126
  char** read_prefixes;
124
	char **read_prefixes;
127
  int num_read_prefixes;
125
  int num_read_prefixes;
128
  char** write_prefixes;
126
	char **write_prefixes;
129
  int num_write_prefixes;
127
  int num_write_prefixes;
130
  char** predict_prefixes;
128
	char **predict_prefixes;
131
  int num_predict_prefixes;
129
  int num_predict_prefixes;
132
  char** write_denied_prefixes;
130
	char **write_denied_prefixes;
133
  int num_write_denied_prefixes;
131
  int num_write_denied_prefixes;
134
} sbcontext_t;
132
} sbcontext_t;
135
133
136
/* glibc modified realpath() functions */
134
/* glibc modified realpath() functions */
137
char *erealpath (const char *name, char *resolved);
135
char *erealpath(const char *name, char *resolved);
136
/* glibc modified getcwd() functions */
137
char *egetcwd(char *, size_t);
138
138
139
static void init_wrappers(void);
139
static void init_wrappers(void);
140
static void *get_dlsym(const char *);
140
static void *get_dlsym(const char *);
Lines 147-212 Link Here
147
static void clean_env_entries(char ***, int *);
147
static void clean_env_entries(char ***, int *);
148
static void init_context(sbcontext_t *);
148
static void init_context(sbcontext_t *);
149
static void init_env_entries(char ***, int *, char *, int);
149
static void init_env_entries(char ***, int *, char *, int);
150
static char* filter_path(const char*);
150
static char *filter_path(const char *);
151
static int is_sandbox_on();
151
static int is_sandbox_on();
152
static int is_sandbox_pid();
152
static int is_sandbox_pid();
153
153
154
/* Wrapped functions */
154
/* Wrapped functions */
155
155
156
extern int chmod(const char *, mode_t);
156
extern int chmod(const char *, mode_t);
157
static int(*true_chmod)(const char *, mode_t);
157
static int (*true_chmod) (const char *, mode_t);
158
extern int chown(const char *, uid_t, gid_t);
158
extern int chown(const char *, uid_t, gid_t);
159
static int(*true_chown)(const char *, uid_t, gid_t);
159
static int (*true_chown) (const char *, uid_t, gid_t);
160
extern int creat(const char *, mode_t);
160
extern int creat(const char *, mode_t);
161
static int(*true_creat)(const char *, mode_t);
161
static int (*true_creat) (const char *, mode_t);
162
extern FILE *fopen(const char *,const char*);
162
extern FILE *fopen(const char *, const char *);
163
static FILE *(*true_fopen)(const char *,const char*);
163
static FILE *(*true_fopen) (const char *, const char *);
164
extern int lchown(const char *, uid_t, gid_t);
164
extern int lchown(const char *, uid_t, gid_t);
165
static int(*true_lchown)(const char *, uid_t, gid_t);
165
static int (*true_lchown) (const char *, uid_t, gid_t);
166
extern int link(const char *, const char *);
166
extern int link(const char *, const char *);
167
static int(*true_link)(const char *, const char *);
167
static int (*true_link) (const char *, const char *);
168
extern int mkdir(const char *, mode_t);
168
extern int mkdir(const char *, mode_t);
169
static int(*true_mkdir)(const char *, mode_t);
169
static int (*true_mkdir) (const char *, mode_t);
170
extern DIR *opendir(const char *);
170
extern DIR *opendir(const char *);
171
static DIR *(*true_opendir)(const char *);
171
static DIR *(*true_opendir) (const char *);
172
#ifdef WRAP_MKNOD
172
#ifdef WRAP_MKNOD
173
extern int __xmknod(const char *, mode_t, dev_t);
173
extern int __xmknod(const char *, mode_t, dev_t);
174
static int(*true___xmknod)(const char *, mode_t, dev_t);
174
static int (*true___xmknod) (const char *, mode_t, dev_t);
175
#endif
175
#endif
176
extern int open(const char *, int, ...);
176
extern int open(const char *, int, ...);
177
static int(*true_open)(const char *, int, ...);
177
static int (*true_open) (const char *, int, ...);
178
extern int rename(const char *, const char *);
178
extern int rename(const char *, const char *);
179
static int(*true_rename)(const char *, const char *);
179
static int (*true_rename) (const char *, const char *);
180
extern int rmdir(const char *);
180
extern int rmdir(const char *);
181
static int(*true_rmdir)(const char *);
181
static int (*true_rmdir) (const char *);
182
extern int symlink(const char *, const char *);
182
extern int symlink(const char *, const char *);
183
static int(*true_symlink)(const char *, const char *);
183
static int (*true_symlink) (const char *, const char *);
184
extern int truncate(const char *, TRUNCATE_T);
184
extern int truncate(const char *, TRUNCATE_T);
185
static int(*true_truncate)(const char *, TRUNCATE_T);
185
static int (*true_truncate) (const char *, TRUNCATE_T);
186
extern int unlink(const char *);
186
extern int unlink(const char *);
187
static int(*true_unlink)(const char *);
187
static int (*true_unlink) (const char *);
188
188
189
#if (GLIBC_MINOR >= 1)
189
#if (GLIBC_MINOR >= 1)
190
190
191
extern int creat64(const char *, __mode_t);
191
extern int creat64(const char *, __mode_t);
192
static int(*true_creat64)(const char *, __mode_t);
192
static int (*true_creat64) (const char *, __mode_t);
193
extern FILE *fopen64(const char *,const char *);
193
extern FILE *fopen64(const char *, const char *);
194
static FILE *(*true_fopen64)(const char *,const char *);
194
static FILE *(*true_fopen64) (const char *, const char *);
195
extern int open64(const char *, int, ...);
195
extern int open64(const char *, int, ...);
196
static int(*true_open64)(const char *, int, ...);
196
static int (*true_open64) (const char *, int, ...);
197
extern int truncate64(const char *, __off64_t);
197
extern int truncate64(const char *, __off64_t);
198
static int(*true_truncate64)(const char *, __off64_t);
198
static int (*true_truncate64) (const char *, __off64_t);
199
199
200
#endif
200
#endif
201
201
202
extern int execve(const char *filename, char *const argv [], char *const envp[]);
202
extern int execve(const char *filename, char *const argv[], char *const envp[]);
203
static int (*true_execve)(const char *, char *const [], char *const []);
203
static int (*true_execve) (const char *, char *const[], char *const[]);
204
204
205
/*
205
/*
206
 * Initialize the shabang
206
 * Initialize the shabang
207
 */
207
 */
208
208
209
static void init_wrappers(void)
209
static void
210
init_wrappers(void)
210
{
211
{
211
  void *libc_handle = NULL;
212
  void *libc_handle = NULL;
212
213
Lines 246-252 Link Here
246
  true_execve = dlsym(libc_handle, "execve");
247
  true_execve = dlsym(libc_handle, "execve");
247
}
248
}
248
  
249
  
249
void _init(void)
250
void
251
_init(void)
250
{
252
{
251
  int old_errno = errno;
253
  int old_errno = errno;
252
  char *tmp_string = NULL;
254
  char *tmp_string = NULL;
Lines 261-273 Link Here
261
  tmp_string = get_sandbox_lib("/");
263
  tmp_string = get_sandbox_lib("/");
262
  strncpy(sandbox_lib, tmp_string, 254);
264
  strncpy(sandbox_lib, tmp_string, 254);
263
  
265
  
264
  if (tmp_string) free(tmp_string);
266
	if (tmp_string)
267
		free(tmp_string);
265
  tmp_string = NULL;
268
  tmp_string = NULL;
266
269
267
  errno = old_errno;
270
  errno = old_errno;
268
}
271
}
269
272
270
static int canonicalize(const char *path, char *resolved_path)
273
static int
274
canonicalize(const char *path, char *resolved_path)
271
{
275
{
272
  int old_errno = errno;
276
  int old_errno = errno;
273
  char *retval;
277
  char *retval;
Lines 280-286 Link Here
280
284
281
  retval = erealpath(path, resolved_path);
285
  retval = erealpath(path, resolved_path);
282
  
286
  
283
  if((!retval) && (path[0] != '/')) {
287
	if ((!retval) && (path[0] != '/')) {
284
    /* The path could not be canonicalized, append it
288
    /* The path could not be canonicalized, append it
285
     * to the current working directory if it was not
289
     * to the current working directory if it was not
286
     * an absolute path
290
     * an absolute path
Lines 288-294 Link Here
288
    if (errno == ENAMETOOLONG)
292
    if (errno == ENAMETOOLONG)
289
      return -1;
293
      return -1;
290
    
294
    
291
    getcwd(resolved_path, SB_PATH_MAX - 2);
295
		egetcwd(resolved_path, SB_PATH_MAX - 2);
292
    strcat(resolved_path, "/");
296
    strcat(resolved_path, "/");
293
    strncat(resolved_path, path, SB_PATH_MAX - 1);
297
    strncat(resolved_path, path, SB_PATH_MAX - 1);
294
    
298
    
Lines 313-319 Link Here
313
  return 0;
317
  return 0;
314
}
318
}
315
319
316
static void *get_dlsym(const char *symname)
320
static void *
321
get_dlsym(const char *symname)
317
{
322
{
318
  void *libc_handle = NULL;
323
  void *libc_handle = NULL;
319
  void *symaddr = NULL;
324
  void *symaddr = NULL;
Lines 341-354 Link Here
341
 * Wrapper Functions
346
 * Wrapper Functions
342
 */
347
 */
343
348
344
int chmod(const char *path, mode_t mode)
349
int
350
chmod(const char *path, mode_t mode)
345
{
351
{
346
  int result = -1;
352
  int result = -1;
347
  char canonic[SB_PATH_MAX];
353
  char canonic[SB_PATH_MAX];
348
354
349
  canonicalize_int(path, canonic);
355
  canonicalize_int(path, canonic);
350
356
351
  if FUNCTION_SANDBOX_SAFE("chmod", canonic) {
357
	if FUNCTION_SANDBOX_SAFE
358
		("chmod", canonic) {
352
    check_dlsym(chmod);
359
    check_dlsym(chmod);
353
    result = true_chmod(path, mode);
360
    result = true_chmod(path, mode);
354
  }
361
  }
Lines 356-369 Link Here
356
  return result;
363
  return result;
357
}
364
}
358
365
359
int chown(const char *path, uid_t owner, gid_t group)
366
int
367
chown(const char *path, uid_t owner, gid_t group)
360
{
368
{
361
  int result = -1;
369
  int result = -1;
362
  char canonic[SB_PATH_MAX];
370
  char canonic[SB_PATH_MAX];
363
371
364
  canonicalize_int(path, canonic);
372
  canonicalize_int(path, canonic);
365
373
366
  if FUNCTION_SANDBOX_SAFE("chown", canonic) {
374
	if FUNCTION_SANDBOX_SAFE
375
		("chown", canonic) {
367
    check_dlsym(chown);
376
    check_dlsym(chown);
368
    result = true_chown(path, owner, group);
377
    result = true_chown(path, owner, group);
369
  }
378
  }
Lines 371-377 Link Here
371
  return result;
380
  return result;
372
}
381
}
373
382
374
int creat(const char *pathname, mode_t mode)
383
int
384
creat(const char *pathname, mode_t mode)
375
{
385
{
376
/* Is it a system call? */
386
/* Is it a system call? */
377
  int result = -1;
387
  int result = -1;
Lines 379-385 Link Here
379
389
380
  canonicalize_int(pathname, canonic);
390
  canonicalize_int(pathname, canonic);
381
391
382
  if FUNCTION_SANDBOX_SAFE("creat", canonic) {
392
	if FUNCTION_SANDBOX_SAFE
393
		("creat", canonic) {
383
    check_dlsym(open);
394
    check_dlsym(open);
384
    result = true_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
395
    result = true_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
385
  }
396
  }
Lines 387-408 Link Here
387
  return result;
398
  return result;
388
}
399
}
389
400
390
FILE *fopen(const char *pathname, const char *mode)
401
FILE *
402
fopen(const char *pathname, const char *mode)
391
{
403
{
392
  FILE *result = NULL;
404
  FILE *result = NULL;
393
  char canonic[SB_PATH_MAX];
405
  char canonic[SB_PATH_MAX];
394
406
395
  canonicalize_ptr(pathname, canonic);
407
  canonicalize_ptr(pathname, canonic);
396
408
397
  if FUNCTION_SANDBOX_SAFE_CHAR("fopen", canonic, mode) {
409
	if FUNCTION_SANDBOX_SAFE_CHAR
410
		("fopen", canonic, mode) {
398
    check_dlsym(fopen);
411
    check_dlsym(fopen);
399
    result = true_fopen(pathname,mode);
412
		result = true_fopen(pathname, mode);
400
  }
413
  }
401
   
414
   
402
  return result;
415
  return result;
403
}
416
}
404
417
405
int lchown(const char *path, uid_t owner, gid_t group)
418
int
419
lchown(const char *path, uid_t owner, gid_t group)
406
{
420
{
407
/* Linux specific? */
421
/* Linux specific? */
408
  int result = -1;
422
  int result = -1;
Lines 410-416 Link Here
410
424
411
  canonicalize_int(path, canonic);
425
  canonicalize_int(path, canonic);
412
426
413
  if FUNCTION_SANDBOX_SAFE("lchown", canonic) {
427
	if FUNCTION_SANDBOX_SAFE
428
		("lchown", canonic) {
414
    check_dlsym(chown);
429
    check_dlsym(chown);
415
    result = true_chown(path, owner, group);
430
    result = true_chown(path, owner, group);
416
  }
431
  }
Lines 418-424 Link Here
418
  return result;
433
  return result;
419
}
434
}
420
435
421
int link(const char *oldpath, const char *newpath)
436
int
437
link(const char *oldpath, const char *newpath)
422
{
438
{
423
  int result = -1;
439
  int result = -1;
424
  char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
440
  char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
Lines 426-432 Link Here
426
  canonicalize_int(oldpath, old_canonic);
442
  canonicalize_int(oldpath, old_canonic);
427
  canonicalize_int(newpath, new_canonic);
443
  canonicalize_int(newpath, new_canonic);
428
444
429
  if FUNCTION_SANDBOX_SAFE("link", new_canonic) {
445
	if FUNCTION_SANDBOX_SAFE
446
		("link", new_canonic) {
430
    check_dlsym(link);
447
    check_dlsym(link);
431
    result = true_link(oldpath, newpath);
448
    result = true_link(oldpath, newpath);
432
  }
449
  }
Lines 434-447 Link Here
434
  return result;
451
  return result;
435
}
452
}
436
453
437
int mkdir(const char *pathname, mode_t mode)
454
int
455
mkdir(const char *pathname, mode_t mode)
438
{
456
{
439
  int result = -1;
457
  int result = -1;
440
  char canonic[SB_PATH_MAX];
458
  char canonic[SB_PATH_MAX];
441
459
442
  canonicalize_int(pathname, canonic);
460
  canonicalize_int(pathname, canonic);
443
461
444
  if FUNCTION_SANDBOX_SAFE("mkdir", canonic) {
462
	if FUNCTION_SANDBOX_SAFE
463
		("mkdir", canonic) {
445
    check_dlsym(mkdir);
464
    check_dlsym(mkdir);
446
    result = true_mkdir(pathname, mode);
465
    result = true_mkdir(pathname, mode);
447
  }
466
  }
Lines 449-462 Link Here
449
  return result;
468
  return result;
450
}
469
}
451
470
452
DIR *opendir(const char *name)
471
DIR *
472
opendir(const char *name)
453
{
473
{
454
  DIR *result = NULL;
474
  DIR *result = NULL;
455
  char canonic[SB_PATH_MAX];
475
  char canonic[SB_PATH_MAX];
456
476
457
  canonicalize_ptr(name, canonic);
477
  canonicalize_ptr(name, canonic);
458
478
459
  if FUNCTION_SANDBOX_SAFE("opendir", canonic) {
479
	if FUNCTION_SANDBOX_SAFE
480
		("opendir", canonic) {
460
    check_dlsym(opendir);
481
    check_dlsym(opendir);
461
    result = true_opendir(name);
482
    result = true_opendir(name);
462
  }
483
  }
Lines 466-479 Link Here
466
487
467
#ifdef WRAP_MKNOD
488
#ifdef WRAP_MKNOD
468
489
469
int __xmknod(const char *pathname, mode_t mode, dev_t dev)
490
int
491
__xmknod(const char *pathname, mode_t mode, dev_t dev)
470
{
492
{
471
  int result = -1;
493
  int result = -1;
472
  char canonic[SB_PATH_MAX];
494
  char canonic[SB_PATH_MAX];
473
495
474
  canonicalize_int(pathname, canonic);
496
  canonicalize_int(pathname, canonic);
475
497
476
  if FUNCTION_SANDBOX_SAFE("__xmknod", canonic) {
498
	if FUNCTION_SANDBOX_SAFE
499
		("__xmknod", canonic) {
477
    check_dlsym(__xmknod);
500
    check_dlsym(__xmknod);
478
    result = true___xmknod(pathname, mode, dev);
501
    result = true___xmknod(pathname, mode, dev);
479
  }
502
  }
Lines 483-489 Link Here
483
506
484
#endif
507
#endif
485
508
486
int open(const char *pathname, int flags, ...)
509
int
510
open(const char *pathname, int flags, ...)
487
{
511
{
488
/* Eventually, there is a third parameter: it's mode_t mode */
512
/* Eventually, there is a third parameter: it's mode_t mode */
489
  va_list ap;
513
  va_list ap;
Lines 499-516 Link Here
499
523
500
  canonicalize_int(pathname, canonic);
524
  canonicalize_int(pathname, canonic);
501
525
502
  if FUNCTION_SANDBOX_SAFE_INT("open", canonic, flags) {
526
	if FUNCTION_SANDBOX_SAFE_INT
527
		("open", canonic, flags) {
503
    /* We need to resolve open() realtime in some cases,
528
    /* We need to resolve open() realtime in some cases,
504
    * else we get a segfault when running /bin/ps, etc
529
    * else we get a segfault when running /bin/ps, etc
505
    * in a sandbox */
530
    * in a sandbox */
506
    check_dlsym(open);
531
    check_dlsym(open);
507
    result=true_open(pathname, flags, mode);
532
		result = true_open(pathname, flags, mode);
508
  }
533
  }
509
534
510
  return result;
535
  return result;
511
}
536
}
512
537
513
int rename(const char *oldpath, const char *newpath)
538
int
539
rename(const char *oldpath, const char *newpath)
514
{
540
{
515
  int result = -1;
541
  int result = -1;
516
  char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
542
  char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
Lines 518-524 Link Here
518
  canonicalize_int(oldpath, old_canonic);
544
  canonicalize_int(oldpath, old_canonic);
519
  canonicalize_int(newpath, new_canonic);
545
  canonicalize_int(newpath, new_canonic);
520
546
521
  if FUNCTION_SANDBOX_SAFE("rename", new_canonic) {
547
	if FUNCTION_SANDBOX_SAFE
548
		("rename", new_canonic) {
522
    check_dlsym(rename);
549
    check_dlsym(rename);
523
    result = true_rename(oldpath, newpath);
550
    result = true_rename(oldpath, newpath);
524
  }
551
  }
Lines 526-539 Link Here
526
  return result;
553
  return result;
527
}
554
}
528
555
529
int rmdir(const char *pathname)
556
int
557
rmdir(const char *pathname)
530
{
558
{
531
  int result = -1;
559
  int result = -1;
532
  char canonic[SB_PATH_MAX];
560
  char canonic[SB_PATH_MAX];
533
561
534
  canonicalize_int(pathname, canonic);
562
  canonicalize_int(pathname, canonic);
535
563
536
  if FUNCTION_SANDBOX_SAFE("rmdir", canonic) {
564
	if FUNCTION_SANDBOX_SAFE
565
		("rmdir", canonic) {
537
    check_dlsym(rmdir);
566
    check_dlsym(rmdir);
538
    result = true_rmdir(pathname);
567
    result = true_rmdir(pathname);
539
  }
568
  }
Lines 541-547 Link Here
541
  return result;
570
  return result;
542
}
571
}
543
572
544
int symlink(const char *oldpath, const char *newpath)
573
int
574
symlink(const char *oldpath, const char *newpath)
545
{
575
{
546
  int result = -1;
576
  int result = -1;
547
  char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
577
  char old_canonic[SB_PATH_MAX], new_canonic[SB_PATH_MAX];
Lines 549-555 Link Here
549
  canonicalize_int(oldpath, old_canonic);
579
  canonicalize_int(oldpath, old_canonic);
550
  canonicalize_int(newpath, new_canonic);
580
  canonicalize_int(newpath, new_canonic);
551
581
552
  if FUNCTION_SANDBOX_SAFE("symlink", new_canonic) {
582
	if FUNCTION_SANDBOX_SAFE
583
		("symlink", new_canonic) {
553
    check_dlsym(symlink);
584
    check_dlsym(symlink);
554
    result = true_symlink(oldpath, newpath);
585
    result = true_symlink(oldpath, newpath);
555
  }
586
  }
Lines 557-570 Link Here
557
  return result;
588
  return result;
558
}
589
}
559
590
560
int truncate(const char *path, TRUNCATE_T length)
591
int
592
truncate(const char *path, TRUNCATE_T length)
561
{
593
{
562
  int result = -1;
594
  int result = -1;
563
  char canonic[SB_PATH_MAX];
595
  char canonic[SB_PATH_MAX];
564
596
565
  canonicalize_int(path, canonic);
597
  canonicalize_int(path, canonic);
566
598
567
  if FUNCTION_SANDBOX_SAFE("truncate", canonic) {
599
	if FUNCTION_SANDBOX_SAFE
600
		("truncate", canonic) {
568
    check_dlsym(truncate);
601
    check_dlsym(truncate);
569
    result = true_truncate(path, length);
602
    result = true_truncate(path, length);
570
  }
603
  }
Lines 572-585 Link Here
572
  return result;
605
  return result;
573
}
606
}
574
607
575
int unlink(const char *pathname)
608
int
609
unlink(const char *pathname)
576
{
610
{
577
  int result = -1;
611
  int result = -1;
578
  char canonic[SB_PATH_MAX];
612
  char canonic[SB_PATH_MAX];
579
613
580
  canonicalize_int(pathname, canonic);
614
  canonicalize_int(pathname, canonic);
581
615
582
  if FUNCTION_SANDBOX_SAFE("unlink", canonic) {
616
	if FUNCTION_SANDBOX_SAFE
617
		("unlink", canonic) {
583
    check_dlsym(unlink);
618
    check_dlsym(unlink);
584
    result = true_unlink(pathname);
619
    result = true_unlink(pathname);
585
  }
620
  }
Lines 589-595 Link Here
589
624
590
#if (GLIBC_MINOR >= 1)
625
#if (GLIBC_MINOR >= 1)
591
626
592
int creat64(const char *pathname, __mode_t mode)
627
int
628
creat64(const char *pathname, __mode_t mode)
593
{
629
{
594
/* Is it a system call? */
630
/* Is it a system call? */
595
  int result = -1;
631
  int result = -1;
Lines 597-603 Link Here
597
633
598
  canonicalize_int(pathname, canonic);
634
  canonicalize_int(pathname, canonic);
599
635
600
  if FUNCTION_SANDBOX_SAFE("creat64", canonic) {
636
	if FUNCTION_SANDBOX_SAFE
637
		("creat64", canonic) {
601
    check_dlsym(open64);
638
    check_dlsym(open64);
602
    result = true_open64(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
639
    result = true_open64(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
603
  }
640
  }
Lines 605-626 Link Here
605
  return result;
642
  return result;
606
}
643
}
607
644
608
FILE *fopen64(const char *pathname, const char *mode)
645
FILE *
646
fopen64(const char *pathname, const char *mode)
609
{
647
{
610
  FILE *result = NULL;
648
  FILE *result = NULL;
611
  char canonic[SB_PATH_MAX];
649
  char canonic[SB_PATH_MAX];
612
650
613
  canonicalize_ptr(pathname, canonic);
651
  canonicalize_ptr(pathname, canonic);
614
652
615
  if FUNCTION_SANDBOX_SAFE_CHAR("fopen64", canonic, mode) {
653
	if FUNCTION_SANDBOX_SAFE_CHAR
654
		("fopen64", canonic, mode) {
616
    check_dlsym(fopen64);
655
    check_dlsym(fopen64);
617
    result = true_fopen(pathname,mode);
656
		result = true_fopen(pathname, mode);
618
  }
657
  }
619
   
658
   
620
  return result;
659
  return result;
621
}
660
}
622
661
623
int open64(const char *pathname, int flags, ...)
662
int
663
open64(const char *pathname, int flags, ...)
624
{
664
{
625
/* Eventually, there is a third parameter: it's mode_t mode */
665
/* Eventually, there is a third parameter: it's mode_t mode */
626
  va_list ap;
666
  va_list ap;
Lines 636-657 Link Here
636
676
637
  canonicalize_int(pathname, canonic);
677
  canonicalize_int(pathname, canonic);
638
678
639
  if FUNCTION_SANDBOX_SAFE_INT("open64", canonic, flags) {
679
	if FUNCTION_SANDBOX_SAFE_INT
680
		("open64", canonic, flags) {
640
    check_dlsym(open64);
681
    check_dlsym(open64);
641
    result=true_open64(pathname, flags, mode);
682
		result = true_open64(pathname, flags, mode);
642
  }
683
  }
643
684
644
  return result;
685
  return result;
645
}
686
}
646
687
647
int truncate64(const char *path, __off64_t length)
688
int
689
truncate64(const char *path, __off64_t length)
648
{
690
{
649
  int result = -1;
691
  int result = -1;
650
  char canonic[SB_PATH_MAX];
692
  char canonic[SB_PATH_MAX];
651
693
652
  canonicalize_int(path, canonic);
694
  canonicalize_int(path, canonic);
653
695
654
  if FUNCTION_SANDBOX_SAFE("truncate64", canonic) {
696
	if FUNCTION_SANDBOX_SAFE
697
		("truncate64", canonic) {
655
    check_dlsym(truncate64);
698
    check_dlsym(truncate64);
656
    result = true_truncate64(path, length);
699
    result = true_truncate64(path, length);
657
  }
700
  }
Lines 665-671 Link Here
665
 * Exec Wrappers
708
 * Exec Wrappers
666
 */
709
 */
667
710
668
int execve(const char *filename, char *const argv [], char *const envp[])
711
int
712
execve(const char *filename, char *const argv[], char *const envp[])
669
{
713
{
670
  int old_errno = errno;
714
  int old_errno = errno;
671
  int result = -1;
715
  int result = -1;
Lines 676-688 Link Here
676
720
677
  canonicalize_int(filename, canonic);
721
  canonicalize_int(filename, canonic);
678
722
679
  if FUNCTION_SANDBOX_SAFE("execve", canonic) {
723
	if FUNCTION_SANDBOX_SAFE
724
		("execve", canonic) {
680
    while (envp[count] != NULL) {
725
    while (envp[count] != NULL) {
681
      if (strstr(envp[count], "LD_PRELOAD=") == envp[count]) {
726
      if (strstr(envp[count], "LD_PRELOAD=") == envp[count]) {
682
        if (NULL != strstr(envp[count], sandbox_lib)) {
727
        if (NULL != strstr(envp[count], sandbox_lib)) {
683
          break;
728
          break;
684
        } else {
729
        } else {
685
          const int max_envp_len = strlen(envp[count]) + strlen(sandbox_lib) + 1;
730
					const int max_envp_len =
731
							strlen(envp[count]) + strlen(sandbox_lib) + 1;
686
          
732
          
687
          /* Backup envp[count], and set it to our own one which
733
          /* Backup envp[count], and set it to our own one which
688
           * contains sandbox_lib */
734
           * contains sandbox_lib */
Lines 697-703 Link Here
697
            strncpy(new_envp + strlen(old_envp) + 1, sandbox_lib,
743
            strncpy(new_envp + strlen(old_envp) + 1, sandbox_lib,
698
                    max_envp_len - strlen(new_envp));
744
                    max_envp_len - strlen(new_envp));
699
          } else {
745
          } else {
700
            strncpy(new_envp + strlen(old_envp), sandbox_lib,
746
						strncpy(new_envp +
747
										strlen(old_envp), sandbox_lib,
701
                    max_envp_len - strlen(new_envp));
748
                    max_envp_len - strlen(new_envp));
702
          }
749
          }
703
750
Lines 707-713 Link Here
707
          /* envp[count] = new_envp;
754
          /* envp[count] = new_envp;
708
           *
755
           *
709
           * Get rid of the "read-only" warnings */
756
           * Get rid of the "read-only" warnings */
710
          memcpy((void *)&envp[count], &new_envp, sizeof(new_envp));
757
					memcpy((void *) &envp[count], &new_envp, sizeof (new_envp));
711
758
712
          break;
759
          break;
713
        }
760
        }
Lines 724-730 Link Here
724
      /* Restore envp[count] again.
771
      /* Restore envp[count] again.
725
       * 
772
       * 
726
       * envp[count] = old_envp; */
773
       * envp[count] = old_envp; */
727
      memcpy((void *)&envp[count], &old_envp, sizeof(old_envp));
774
			memcpy((void *) &envp[count], &old_envp, sizeof (old_envp));
728
      old_envp = NULL;
775
      old_envp = NULL;
729
    }
776
    }
730
  }
777
  }
Lines 743-752 Link Here
743
/* This hack is needed for glibc 2.1.1 (and others?)
790
/* This hack is needed for glibc 2.1.1 (and others?)
744
 * (not really needed, but good example) */
791
 * (not really needed, but good example) */
745
extern int fclose(FILE *);
792
extern int fclose(FILE *);
746
static int (*true_fclose)(FILE *) = NULL;
793
static int (*true_fclose) (FILE *) = NULL;
747
int fclose(FILE *file)
794
int
795
fclose(FILE * file)
748
{
796
{
749
  int result = - 1;
797
	int result = -1;
750
798
751
  check_dlsym(fclose);
799
  check_dlsym(fclose);
752
  result = true_fclose(file);
800
  result = true_fclose(file);
Lines 756-762 Link Here
756
804
757
#endif /* GLIBC_MINOR == 1 */
805
#endif /* GLIBC_MINOR == 1 */
758
806
759
static void init_context(sbcontext_t* context)
807
static void
808
init_context(sbcontext_t * context)
760
{
809
{
761
  context->show_access_violation = 1;
810
  context->show_access_violation = 1;
762
  context->deny_prefixes = NULL;
811
  context->deny_prefixes = NULL;
Lines 771-781 Link Here
771
  context->num_write_denied_prefixes = 0;
820
  context->num_write_denied_prefixes = 0;
772
}
821
}
773
822
774
static int is_sandbox_pid()
823
static int
824
is_sandbox_pid()
775
{
825
{
776
  int old_errno = errno;
826
  int old_errno = errno;
777
  int result = 0;
827
  int result = 0;
778
  FILE* pids_stream = NULL;
828
	FILE *pids_stream = NULL;
779
  int pids_file = -1;
829
  int pids_file = -1;
780
  int current_pid = 0;
830
  int current_pid = 0;
781
  int tmp_pid = 0;
831
  int tmp_pid = 0;
Lines 786-794 Link Here
786
836
787
  if (NULL == pids_stream) {
837
  if (NULL == pids_stream) {
788
    perror(">>> pids file fopen");
838
    perror(">>> pids file fopen");
789
  }
839
	} else {
790
  else
791
  {
792
    pids_file = fileno(pids_stream);
840
    pids_file = fileno(pids_stream);
793
841
794
    if (pids_file < 0) {
842
    if (pids_file < 0) {
Lines 815-821 Link Here
815
  return result;
863
  return result;
816
}
864
}
817
865
818
static void clean_env_entries(char*** prefixes_array, int* prefixes_num)
866
static void
867
clean_env_entries(char ***prefixes_array, int *prefixes_num)
819
{
868
{
820
  int old_errno = errno;
869
  int old_errno = errno;
821
  int i = 0;
870
  int i = 0;
Lines 827-833 Link Here
827
        (*prefixes_array)[i] = NULL;
876
        (*prefixes_array)[i] = NULL;
828
      }
877
      }
829
    }
878
    }
830
    if (*prefixes_array) free(*prefixes_array);
879
		if (*prefixes_array)
880
			free(*prefixes_array);
831
    *prefixes_array = NULL;
881
    *prefixes_array = NULL;
832
    *prefixes_num = 0;
882
    *prefixes_num = 0;
833
  }
883
  }
Lines 835-856 Link Here
835
  errno = old_errno;
885
  errno = old_errno;
836
}
886
}
837
887
838
static void init_env_entries(char*** prefixes_array, int* prefixes_num, char* env, int warn)
888
static void
889
init_env_entries(char ***prefixes_array, int *prefixes_num, char *env, int warn)
839
{
890
{
840
  int old_errno = errno;
891
  int old_errno = errno;
841
  char* prefixes_env = getenv(env);
892
	char *prefixes_env = getenv(env);
842
893
843
  if (NULL == prefixes_env) {
894
  if (NULL == prefixes_env) {
844
    fprintf(stderr,
895
    fprintf(stderr,
845
            "Sandbox error : the %s environmental variable should be defined.\n",
896
            "Sandbox error : the %s environmental variable should be defined.\n",
846
            env);
897
            env);
847
  } else {
898
  } else {
848
    char* buffer = NULL;
899
		char *buffer = NULL;
849
    int prefixes_env_length = strlen(prefixes_env);
900
    int prefixes_env_length = strlen(prefixes_env);
850
    int i = 0;
901
    int i = 0;
851
    int num_delimiters = 0;
902
    int num_delimiters = 0;
852
    char* token = NULL;
903
		char *token = NULL;
853
    char* prefix = NULL;
904
		char *prefix = NULL;
854
905
855
    for (i = 0; i < prefixes_env_length; i++) {
906
    for (i = 0; i < prefixes_env_length; i++) {
856
      if (':' == prefixes_env[i]) {
907
      if (':' == prefixes_env[i]) {
Lines 859-865 Link Here
859
    }
910
    }
860
911
861
    if (num_delimiters > 0) {
912
    if (num_delimiters > 0) {
862
      *prefixes_array = (char **)malloc((num_delimiters + 1) * sizeof(char *));
913
			*prefixes_array =
914
					(char **) malloc((num_delimiters + 1) * sizeof (char *));
863
      buffer = strndupa(prefixes_env, prefixes_env_length);
915
      buffer = strndupa(prefixes_env, prefixes_env_length);
864
      
916
      
865
#ifdef REENTRANT_STRTOK
917
#ifdef REENTRANT_STRTOK
Lines 878-889 Link Here
878
        token = strtok(NULL, ":");
930
        token = strtok(NULL, ":");
879
#endif
931
#endif
880
932
881
        if (prefix) free(prefix);
933
				if (prefix)
934
					free(prefix);
882
        prefix = NULL;
935
        prefix = NULL;
883
      }
936
      }
884
    }
937
		} else if (prefixes_env_length > 0) {
885
    else if (prefixes_env_length > 0) {
938
			(*prefixes_array) = (char **) malloc(sizeof (char *));
886
      (*prefixes_array) = (char **)malloc(sizeof(char *));
887
         
939
         
888
      (*prefixes_array)[(*prefixes_num)++] = filter_path(prefixes_env);
940
      (*prefixes_array)[(*prefixes_num)++] = filter_path(prefixes_env);
889
    }
941
    }
Lines 892-901 Link Here
892
  errno = old_errno;
944
  errno = old_errno;
893
}
945
}
894
946
895
static char* filter_path(const char* path)
947
static char *
948
filter_path(const char *path)
896
{
949
{
897
  int old_errno = errno;
950
  int old_errno = errno;
898
  char* filtered_path = (char *)malloc(SB_PATH_MAX * sizeof(char));
951
	char *filtered_path = (char *) malloc(SB_PATH_MAX * sizeof (char));
899
952
900
  canonicalize_ptr(path, filtered_path);
953
  canonicalize_ptr(path, filtered_path);
901
954
Lines 904-926 Link Here
904
  return filtered_path;
957
  return filtered_path;
905
}
958
}
906
959
907
static int check_access(sbcontext_t* sbcontext, const char* func, const char* path)
960
static int
961
check_access(sbcontext_t * sbcontext, const char *func, const char *path)
908
{
962
{
909
  int old_errno = errno;
963
  int old_errno = errno;
910
  int result = -1;
964
  int result = -1;
911
  int i = 0;
965
  int i = 0;
912
  char* filtered_path = filter_path(path);
966
	char *filtered_path = filter_path(path);
913
967
914
  if ('/' != filtered_path[0]) {
968
  if ('/' != filtered_path[0]) {
915
    errno = old_errno;
969
    errno = old_errno;
916
970
917
    if (filtered_path) free(filtered_path);
971
		if (filtered_path)
972
			free(filtered_path);
918
    filtered_path = NULL;
973
    filtered_path = NULL;
919
974
920
    return 0;
975
    return 0;
921
  }
976
  }
922
977
923
  if ((0 == strncmp(filtered_path, "/etc/ld.so.preload", 18)) && (is_sandbox_pid())) {
978
	if ((0 == strncmp(filtered_path, "/etc/ld.so.preload", 18))
979
			&& (is_sandbox_pid())) {
924
    result = 1;
980
    result = 1;
925
  }
981
  }
926
   
982
   
Lines 929-935 Link Here
929
      for (i = 0; i < sbcontext->num_deny_prefixes; i++) {
985
      for (i = 0; i < sbcontext->num_deny_prefixes; i++) {
930
        if (NULL != sbcontext->deny_prefixes[i]) {
986
        if (NULL != sbcontext->deny_prefixes[i]) {
931
          if (0 == strncmp(filtered_path,
987
          if (0 == strncmp(filtered_path,
932
                           sbcontext->deny_prefixes[i],
988
													 sbcontext->
989
													 deny_prefixes[i],
933
                           strlen(sbcontext->deny_prefixes[i]))) {
990
                           strlen(sbcontext->deny_prefixes[i]))) {
934
            result = 0;
991
            result = 0;
935
            break;
992
            break;
Lines 948-969 Link Here
948
           (0 == strncmp(func, "execlp", 6)) ||
1005
           (0 == strncmp(func, "execlp", 6)) ||
949
           (0 == strncmp(func, "execle", 6)) ||
1006
           (0 == strncmp(func, "execle", 6)) ||
950
           (0 == strncmp(func, "execv", 5)) ||
1007
           (0 == strncmp(func, "execv", 5)) ||
951
           (0 == strncmp(func, "execvp", 6)) ||
1008
					 (0 == strncmp(func, "execvp", 6))
952
           (0 == strncmp(func, "execve", 6))
1009
					 || (0 == strncmp(func, "execve", 6))
953
          )
1010
          )
954
         ) {
1011
         ) {
955
        for (i = 0; i < sbcontext->num_read_prefixes; i++) {
1012
        for (i = 0; i < sbcontext->num_read_prefixes; i++) {
956
          if (NULL != sbcontext->read_prefixes[i]) {
1013
          if (NULL != sbcontext->read_prefixes[i]) {
957
            if (0 == strncmp(filtered_path,
1014
            if (0 == strncmp(filtered_path,
958
                             sbcontext->read_prefixes[i],
1015
														 sbcontext->
1016
														 read_prefixes[i],
959
                             strlen(sbcontext->read_prefixes[i]))) {
1017
                             strlen(sbcontext->read_prefixes[i]))) {
960
              result = 1;
1018
              result = 1;
961
              break;
1019
              break;
962
            }
1020
            }
963
          }
1021
          }
964
        }
1022
        }
965
      }
1023
			} else if ((NULL != sbcontext->write_prefixes) &&
966
      else if ((NULL != sbcontext->write_prefixes) &&
967
               ((0 == strncmp(func, "open_wr", 7)) ||
1024
               ((0 == strncmp(func, "open_wr", 7)) ||
968
                (0 == strncmp(func, "creat", 5)) ||
1025
                (0 == strncmp(func, "creat", 5)) ||
969
                (0 == strncmp(func, "creat64", 7)) ||
1026
                (0 == strncmp(func, "creat64", 7)) ||
Lines 990-998 Link Here
990
1047
991
        for (i = 0; i < sbcontext->num_write_denied_prefixes; i++) {
1048
        for (i = 0; i < sbcontext->num_write_denied_prefixes; i++) {
992
          if (NULL != sbcontext->write_denied_prefixes[i]) {
1049
          if (NULL != sbcontext->write_denied_prefixes[i]) {
993
            if (0 == strncmp(filtered_path,
1050
						if (0 ==
994
                             sbcontext->write_denied_prefixes[i],
1051
								strncmp(filtered_path,
995
                             strlen(sbcontext->write_denied_prefixes[i]))) {
1052
												sbcontext->
1053
												write_denied_prefixes
1054
												[i], strlen(sbcontext->write_denied_prefixes[i]))) {
996
              result = 0;
1055
              result = 0;
997
              break;
1056
              break;
998
            }
1057
            }
Lines 1002-1008 Link Here
1002
        if (-1 == result) {
1061
        if (-1 == result) {
1003
          for (i = 0; i < sbcontext->num_write_prefixes; i++) {
1062
          for (i = 0; i < sbcontext->num_write_prefixes; i++) {
1004
            if (NULL != sbcontext->write_prefixes[i]) {
1063
            if (NULL != sbcontext->write_prefixes[i]) {
1005
              if (0 == strncmp(filtered_path,
1064
							if (0 ==
1065
									strncmp
1066
									(filtered_path,
1006
                               sbcontext->write_prefixes[i],
1067
                               sbcontext->write_prefixes[i],
1007
                               strlen(sbcontext->write_prefixes[i]))) {
1068
                               strlen(sbcontext->write_prefixes[i]))) {
1008
                result = 1;
1069
                result = 1;
Lines 1023-1030 Link Here
1023
            if (-1 == result) {
1084
            if (-1 == result) {
1024
              for (i = 0; i < sbcontext->num_predict_prefixes; i++) {
1085
              for (i = 0; i < sbcontext->num_predict_prefixes; i++) {
1025
                if (NULL != sbcontext->predict_prefixes[i]) {
1086
                if (NULL != sbcontext->predict_prefixes[i]) {
1026
                  if (0 == strncmp(filtered_path,
1087
									if (0 ==
1027
                                   sbcontext->predict_prefixes[i],
1088
											strncmp
1089
											(filtered_path,
1090
											 sbcontext->
1091
											 predict_prefixes[i],
1028
                                   strlen(sbcontext->predict_prefixes[i]))) {
1092
                                   strlen(sbcontext->predict_prefixes[i]))) {
1029
                    sbcontext->show_access_violation = 0;
1093
                    sbcontext->show_access_violation = 0;
1030
                    result = 0;
1094
                    result = 0;
Lines 1043-1049 Link Here
1043
    result = 0;
1107
    result = 0;
1044
  }
1108
  }
1045
1109
1046
  if (filtered_path) free(filtered_path);
1110
	if (filtered_path)
1111
		free(filtered_path);
1047
  filtered_path = NULL;
1112
  filtered_path = NULL;
1048
1113
1049
  errno = old_errno;
1114
  errno = old_errno;
Lines 1051-1082 Link Here
1051
  return result;
1116
  return result;
1052
}
1117
}
1053
1118
1054
static int check_syscall(sbcontext_t* sbcontext, const char* func, const char* file)
1119
static int
1120
check_syscall(sbcontext_t * sbcontext, const char *func, const char *file)
1055
{
1121
{
1056
  int old_errno = errno;
1122
  int old_errno = errno;
1057
  int result = 1;
1123
  int result = 1;
1058
  struct stat log_stat;
1124
  struct stat log_stat;
1059
  char* log_path = NULL;
1125
	char *log_path = NULL;
1060
  char* absolute_path = NULL;
1126
	char *absolute_path = NULL;
1061
  char* tmp_buffer = NULL;
1127
	char *tmp_buffer = NULL;
1062
  int log_file = 0;
1128
  int log_file = 0;
1063
  struct stat debug_log_stat;
1129
  struct stat debug_log_stat;
1064
  char* debug_log_env = NULL;
1130
	char *debug_log_env = NULL;
1065
  char* debug_log_path = NULL;
1131
	char *debug_log_path = NULL;
1066
  int debug_log_file = 0;
1132
  int debug_log_file = 0;
1067
  char buffer[512];
1133
  char buffer[512];
1068
1134
1069
  init_wrappers();
1135
  init_wrappers();
1070
1136
1071
  if ('/' == file[0]) {
1137
  if ('/' == file[0]) {
1072
    absolute_path = (char *)malloc((strlen(file) + 1) * sizeof(char));
1138
		absolute_path = (char *) malloc((strlen(file) + 1) * sizeof (char));
1073
    sprintf(absolute_path, "%s", file);
1139
    sprintf(absolute_path, "%s", file);
1074
  } else {
1140
  } else {
1075
    tmp_buffer = get_current_dir_name();
1141
		tmp_buffer = (char *) malloc(SB_PATH_MAX * sizeof (char));
1076
    absolute_path = (char *)malloc((strlen(tmp_buffer) + 1 + strlen(file) + 1) * sizeof(char));
1142
		egetcwd(tmp_buffer, SB_PATH_MAX - 1);
1077
    sprintf(absolute_path,"%s/%s", tmp_buffer, file);
1143
		absolute_path = (char *) malloc((strlen(tmp_buffer) + 1 + strlen(file) + 1)
1144
																		* sizeof (char));
1145
		sprintf(absolute_path, "%s/%s", tmp_buffer, file);
1078
    
1146
    
1079
    if (tmp_buffer) free(tmp_buffer);
1147
		if (tmp_buffer)
1148
			free(tmp_buffer);
1080
    tmp_buffer = NULL;
1149
    tmp_buffer = NULL;
1081
  }
1150
  }
1082
  
1151
  
Lines 1088-1114 Link Here
1088
       (0 != strncmp(absolute_path, log_path, strlen(log_path)))) &&
1157
       (0 != strncmp(absolute_path, log_path, strlen(log_path)))) &&
1089
      ((NULL == debug_log_env) ||
1158
      ((NULL == debug_log_env) ||
1090
       (NULL == debug_log_path) ||
1159
       (NULL == debug_log_path) ||
1091
       (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path)))) &&
1160
			 (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path))))
1092
      (0 == check_access(sbcontext, func, absolute_path))
1161
			&& (0 == check_access(sbcontext, func, absolute_path))
1093
     ) {
1162
     ) {
1094
    if (1 == sbcontext->show_access_violation) {
1163
    if (1 == sbcontext->show_access_violation) {
1095
      fprintf(stderr, "\e[31;01mACCESS DENIED\033[0m  %s:%*s%s\n",
1164
			fprintf(stderr,
1096
              func, (int)(10 - strlen(func)), "", absolute_path);
1165
							"\e[31;01mACCESS DENIED\033[0m  %s:%*s%s\n",
1166
							func, (int) (10 - strlen(func)), "", absolute_path);
1097
         
1167
         
1098
      if (NULL != log_path) {
1168
      if (NULL != log_path) {
1099
        sprintf(buffer, "%s:%*s%s\n", func, (int)(10 - strlen(func)), "", absolute_path);
1169
				sprintf(buffer, "%s:%*s%s\n", func, (int) (10 - strlen(func)), "",
1170
								absolute_path);
1100
      
1171
      
1101
        if ((0 == lstat(log_path, &log_stat)) &&
1172
				if ((0 == lstat(log_path, &log_stat))
1102
            (0 == S_ISREG(log_stat.st_mode))
1173
						&& (0 == S_ISREG(log_stat.st_mode))
1103
           ) {
1174
           ) {
1104
          fprintf(stderr,
1175
          fprintf(stderr,
1105
                  "\e[31;01mSECURITY BREACH\033[0m  %s already exists and is not a regular file.\n",
1176
                  "\e[31;01mSECURITY BREACH\033[0m  %s already exists and is not a regular file.\n",
1106
                  log_path);
1177
                  log_path);
1107
        } else {
1178
        } else {
1108
          log_file = true_open(log_path,
1179
          log_file = true_open(log_path,
1109
                               O_APPEND | O_WRONLY | O_CREAT,
1180
															 O_APPEND | O_WRONLY
1181
															 | O_CREAT,
1110
                               S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1182
                               S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1111
          if(log_file >= 0) {
1183
					if (log_file >= 0) {
1112
            write(log_file, buffer, strlen(buffer));
1184
            write(log_file, buffer, strlen(buffer));
1113
            close(log_file);
1185
            close(log_file);
1114
          }
1186
          }
Lines 1117-1151 Link Here
1117
    }
1189
    }
1118
1190
1119
    result = 0;
1191
    result = 0;
1120
  }
1192
	} else if (NULL != debug_log_env) {
1121
  else if (NULL != debug_log_env) {
1122
    if (NULL != debug_log_path) {
1193
    if (NULL != debug_log_path) {
1123
      if (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path))) {
1194
      if (0 != strncmp(absolute_path, debug_log_path, strlen(debug_log_path))) {
1124
        sprintf(buffer, "%s:%*s%s\n", func, (int)(10 - strlen(func)), "", absolute_path);
1195
				sprintf(buffer, "%s:%*s%s\n", func, (int) (10 - strlen(func)), "",
1196
								absolute_path);
1125
        
1197
        
1126
        if ((0 == lstat(debug_log_path, &debug_log_stat)) &&
1198
				if ((0 == lstat(debug_log_path, &debug_log_stat))
1127
            (0 == S_ISREG(debug_log_stat.st_mode))
1199
						&& (0 == S_ISREG(debug_log_stat.st_mode))
1128
           ) {
1200
           ) {
1129
          fprintf(stderr,
1201
          fprintf(stderr,
1130
                  "\e[31;01mSECURITY BREACH\033[0m  %s already exists and is not a regular file.\n",
1202
                  "\e[31;01mSECURITY BREACH\033[0m  %s already exists and is not a regular file.\n",
1131
                  log_path);
1203
                  log_path);
1132
        } else {
1204
        } else {
1133
          debug_log_file = true_open(debug_log_path,
1205
					debug_log_file =
1134
                                     O_APPEND | O_WRONLY | O_CREAT,
1206
							true_open(debug_log_path,
1135
                                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1207
												O_APPEND | O_WRONLY |
1136
          if(debug_log_file >= 0) {
1208
												O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1209
					if (debug_log_file >= 0) {
1137
            write(debug_log_file, buffer, strlen(buffer));
1210
            write(debug_log_file, buffer, strlen(buffer));
1138
            close(debug_log_file);
1211
            close(debug_log_file);
1139
          }
1212
          }
1140
        }
1213
        }
1141
      }
1214
      }
1142
    } else {
1215
    } else {
1143
      fprintf(stderr, "\e[32;01mACCESS ALLOWED\033[0m %s:%*s%s\n",
1216
			fprintf(stderr,
1144
              func, (int)(10 - strlen(func)), "", absolute_path);
1217
							"\e[32;01mACCESS ALLOWED\033[0m %s:%*s%s\n",
1218
							func, (int) (10 - strlen(func)), "", absolute_path);
1145
    }
1219
    }
1146
  }
1220
  }
1147
1221
1148
  if (absolute_path) free(absolute_path);
1222
	if (absolute_path)
1223
		free(absolute_path);
1149
  absolute_path = NULL;
1224
  absolute_path = NULL;
1150
1225
1151
  errno = old_errno;
1226
  errno = old_errno;
Lines 1153-1159 Link Here
1153
  return result;
1228
  return result;
1154
}
1229
}
1155
1230
1156
static int is_sandbox_on()
1231
static int
1232
is_sandbox_on()
1157
{
1233
{
1158
  int old_errno = errno;
1234
  int old_errno = errno;
1159
  
1235
  
Lines 1180-1186 Link Here
1180
  }
1256
  }
1181
}
1257
}
1182
1258
1183
static int before_syscall(const char* func, const char* file)
1259
static int
1260
before_syscall(const char *func, const char *file)
1184
{
1261
{
1185
  int old_errno = errno;
1262
  int old_errno = errno;
1186
  int result = 1;
1263
  int result = 1;
Lines 1189-1212 Link Here
1189
  init_context(&sbcontext);
1266
  init_context(&sbcontext);
1190
1267
1191
  init_env_entries(&(sbcontext.deny_prefixes),
1268
  init_env_entries(&(sbcontext.deny_prefixes),
1192
                   &(sbcontext.num_deny_prefixes),
1269
									 &(sbcontext.num_deny_prefixes), "SANDBOX_DENY", 1);
1193
                   "SANDBOX_DENY", 1);
1194
  init_env_entries(&(sbcontext.read_prefixes),
1270
  init_env_entries(&(sbcontext.read_prefixes),
1195
                   &(sbcontext.num_read_prefixes),
1271
									 &(sbcontext.num_read_prefixes), "SANDBOX_READ", 1);
1196
                   "SANDBOX_READ", 1);
1197
  init_env_entries(&(sbcontext.write_prefixes),
1272
  init_env_entries(&(sbcontext.write_prefixes),
1198
                   &(sbcontext.num_write_prefixes),
1273
									 &(sbcontext.num_write_prefixes), "SANDBOX_WRITE", 1);
1199
                   "SANDBOX_WRITE", 1);
1200
  init_env_entries(&(sbcontext.predict_prefixes),
1274
  init_env_entries(&(sbcontext.predict_prefixes),
1201
                   &(sbcontext.num_predict_prefixes),
1275
									 &(sbcontext.num_predict_prefixes), "SANDBOX_PREDICT", 1);
1202
                   "SANDBOX_PREDICT", 1);
1203
1276
1204
  result = check_syscall(&sbcontext, func, file);
1277
  result = check_syscall(&sbcontext, func, file);
1205
1278
1206
  clean_env_entries(&(sbcontext.deny_prefixes),
1279
	clean_env_entries(&(sbcontext.deny_prefixes), &(sbcontext.num_deny_prefixes));
1207
                    &(sbcontext.num_deny_prefixes));
1280
	clean_env_entries(&(sbcontext.read_prefixes), &(sbcontext.num_read_prefixes));
1208
  clean_env_entries(&(sbcontext.read_prefixes),
1209
                    &(sbcontext.num_read_prefixes));
1210
  clean_env_entries(&(sbcontext.write_prefixes),
1281
  clean_env_entries(&(sbcontext.write_prefixes),
1211
                    &(sbcontext.num_write_prefixes));
1282
                    &(sbcontext.num_write_prefixes));
1212
  clean_env_entries(&(sbcontext.predict_prefixes),
1283
  clean_env_entries(&(sbcontext.predict_prefixes),
Lines 1221-1227 Link Here
1221
  return result;
1292
  return result;
1222
}
1293
}
1223
1294
1224
static int before_syscall_open_int(const char* func, const char* file, int flags)
1295
static int
1296
before_syscall_open_int(const char *func, const char *file, int flags)
1225
{
1297
{
1226
  if ((flags & O_WRONLY) || (flags & O_RDWR)) {
1298
  if ((flags & O_WRONLY) || (flags & O_RDWR)) {
1227
    return before_syscall("open_wr", file);
1299
    return before_syscall("open_wr", file);
Lines 1230-1243 Link Here
1230
  }
1302
  }
1231
}
1303
}
1232
1304
1233
static int before_syscall_open_char(const char* func, const char* file, const char* mode)
1305
static int
1306
before_syscall_open_char(const char *func, const char *file, const char *mode)
1234
{
1307
{
1235
  if ((strcmp(mode, "r") == 0) || (strcmp(mode, "rb") == 0) || (strcmp(mode, "rm") == 0)) {
1308
	if ((strcmp(mode, "r") == 0) || (strcmp(mode, "rb") == 0)
1309
			|| (strcmp(mode, "rm") == 0)) {
1236
    return before_syscall("open_rd", file);
1310
    return before_syscall("open_rd", file);
1237
  } else {
1311
  } else {
1238
    return before_syscall("open_wr", file);
1312
    return before_syscall("open_wr", file);
1239
  }
1313
  }
1240
}
1314
}
1241
1315
1316
#include "getcwd.c"
1317
#include "canonicalize.c"
1242
1318
1243
// vim:expandtab noai:cindent ai
1319
// vim:expandtab noai:cindent ai
(-)sandbox.c (-48 / +79 lines)
Lines 38-44 Link Here
38
int print_debug = 0;
38
int print_debug = 0;
39
39
40
/* Read pids file, and load active pids into an array.  Return number of pids in array */
40
/* Read pids file, and load active pids into an array.  Return number of pids in array */
41
int load_active_pids(int fd, int **pids)
41
int
42
load_active_pids(int fd, int **pids)
42
{
43
{
43
  char *data = NULL;
44
  char *data = NULL;
44
  char *ptr = NULL, *ptr2 = NULL;
45
  char *ptr = NULL, *ptr2 = NULL;
Lines 51-57 Link Here
51
  len = file_length(fd);
52
  len = file_length(fd);
52
53
53
  /* Allocate and zero datablock to read pids file */
54
  /* Allocate and zero datablock to read pids file */
54
  data = (char *)malloc((len + 1)*sizeof(char));
55
	data = (char *) malloc((len + 1) * sizeof (char));
55
  memset(data, 0, len + 1);
56
  memset(data, 0, len + 1);
56
57
57
  /* Start at beginning of file */
58
  /* Start at beginning of file */
Lines 76-82 Link Here
76
77
77
    /* If the PID is still alive, add it to our array */
78
    /* If the PID is still alive, add it to our array */
78
    if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
79
    if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
79
      pids[0] = (int *)realloc(pids[0], (num_pids + 1)*sizeof(int));
80
			pids[0] = (int *) realloc(pids[0], (num_pids + 1) * sizeof (int));
80
      pids[0][num_pids] = my_pid;
81
      pids[0][num_pids] = my_pid;
81
      num_pids++;
82
      num_pids++;
82
    }
83
    }
Lines 93-99 Link Here
93
}
94
}
94
95
95
/* Read ld.so.preload file, and loads dirs into an array.  Return number of entries in array */
96
/* Read ld.so.preload file, and loads dirs into an array.  Return number of entries in array */
96
int load_preload_libs(int fd, char ***preloads)
97
int
98
load_preload_libs(int fd, char ***preloads)
97
{
99
{
98
  char *data = NULL;
100
  char *data = NULL;
99
  char *ptr = NULL, *ptr2 = NULL;
101
  char *ptr = NULL, *ptr2 = NULL;
Lines 105-111 Link Here
105
  len = file_length(fd);
107
  len = file_length(fd);
106
108
107
  /* Allocate and zero datablock to read pids file */
109
  /* Allocate and zero datablock to read pids file */
108
  data = (char *)malloc((len + 1)*sizeof(char));
110
	data = (char *) malloc((len + 1) * sizeof (char));
109
  memset(data, 0, len + 1);
111
  memset(data, 0, len + 1);
110
112
111
  /* Start at beginning of file */
113
  /* Start at beginning of file */
Lines 130-136 Link Here
130
132
131
    /* If listing does not match our libname, add it to the array */
133
    /* If listing does not match our libname, add it to the array */
132
    if ((strlen(ptr)) && (NULL == strstr(ptr, LIB_NAME))) {
134
    if ((strlen(ptr)) && (NULL == strstr(ptr, LIB_NAME))) {
133
      preloads[0] = (char **)realloc(preloads[0], (num_entries + 1)*sizeof(char **));
135
			preloads[0] =
136
					(char **) realloc(preloads[0], (num_entries + 1) * sizeof (char **));
134
      preloads[0][num_entries] = strdup(ptr);
137
      preloads[0][num_entries] = strdup(ptr);
135
      num_entries++;
138
      num_entries++;
136
    }
139
    }
Lines 149-156 Link Here
149
  return num_entries;
152
  return num_entries;
150
}
153
}
151
154
152
155
void
153
void cleanup()
156
cleanup()
154
{
157
{
155
  int i = 0;
158
  int i = 0;
156
  int success = 1;
159
  int success = 1;
Lines 211-220 Link Here
211
        file_truncate(preload_file);
213
        file_truncate(preload_file);
212
214
213
        /* store the other preload libraries back into the /etc/ld.so.preload file */
215
        /* store the other preload libraries back into the /etc/ld.so.preload file */
214
        if(num_of_preloads > 0) {
216
				if (num_of_preloads > 0) {
215
          for (i = 0; i < num_of_preloads; i++) {
217
          for (i = 0; i < num_of_preloads; i++) {
216
            sprintf(preload_entry, "%s\n", preload_array[i]);
218
            sprintf(preload_entry, "%s\n", preload_array[i]);
217
            if (write(preload_file, preload_entry, strlen(preload_entry)) != strlen(preload_entry)) {
219
						if (write
220
								(preload_file,
221
								 preload_entry,
222
								 strlen(preload_entry)) != strlen(preload_entry)) {
218
              perror(">>> /etc/ld.so.preload file write");
223
              perror(">>> /etc/ld.so.preload file write");
219
              success = 0;
224
              success = 0;
220
              break;
225
              break;
Lines 224-230 Link Here
224
229
225
    /* Free memory used to store preload array */
230
    /* Free memory used to store preload array */
226
        for (i = 0; i < num_of_preloads; i++) {
231
        for (i = 0; i < num_of_preloads; i++) {
227
          if (preload_array[i]) free(preload_array[i]);
232
					if (preload_array[i])
233
						free(preload_array[i]);
228
          preload_array[i] = NULL;
234
          preload_array[i] = NULL;
229
        }
235
        }
230
        if (preload_array)
236
        if (preload_array)
Lines 240-251 Link Here
240
    file_truncate(pids_file);
246
    file_truncate(pids_file);
241
247
242
    /* if pids are still running, write only the running pids back to the file */
248
    /* if pids are still running, write only the running pids back to the file */
243
    if(num_of_pids > 1) {
249
		if (num_of_pids > 1) {
244
      for (i = 0; i < num_of_pids; i++) {
250
      for (i = 0; i < num_of_pids; i++) {
245
        if (pids_array[i] != getpid()) {
251
        if (pids_array[i] != getpid()) {
246
          sprintf(pid_string, "%d\n", pids_array[i]);
252
          sprintf(pid_string, "%d\n", pids_array[i]);
247
253
248
          if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
254
					if (write(pids_file, pid_string, strlen(pid_string)) !=
255
							strlen(pid_string)) {
249
            perror(">>> pids file write");
256
            perror(">>> pids file write");
250
            success = 0;
257
            success = 0;
251
            break;
258
            break;
Lines 273-292 Link Here
273
    return;
280
    return;
274
}
281
}
275
282
276
void stop(int signum)
283
void
284
stop(int signum)
277
{
285
{
278
  printf("Caught signal %d\r\n", signum);
286
  printf("Caught signal %d\r\n", signum);
279
  cleanup();
287
  cleanup();
280
}
288
}
281
289
282
void setenv_sandbox_write(char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir)
290
void
291
setenv_sandbox_write(char *home_dir, char *portage_tmp_dir, char *var_tmp_dir,
292
										 char *tmp_dir)
283
{
293
{
284
  char sandbox_write_var[1024];
294
  char sandbox_write_var[1024];
285
295
286
  if (!getenv(ENV_SANDBOX_WRITE)) {
296
  if (!getenv(ENV_SANDBOX_WRITE)) {
287
    /* these should go into make.globals later on */
297
    /* these should go into make.globals later on */
288
    strcpy(sandbox_write_var, "");
298
    strcpy(sandbox_write_var, "");
289
    strcat(sandbox_write_var, "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:/dev/vc/:/dev/tty:/tmp/");
299
		strcat(sandbox_write_var,
300
					 "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:/dev/vc/:/dev/tty:/tmp/");
290
    strcat(sandbox_write_var, ":");
301
    strcat(sandbox_write_var, ":");
291
    /* NGPT support */
302
    /* NGPT support */
292
    strcat(sandbox_write_var, "/dev/shm/ngpt");
303
    strcat(sandbox_write_var, "/dev/shm/ngpt");
Lines 349-356 Link Here
349
  }
360
  }
350
}
361
}
351
362
352
363
void
353
void setenv_sandbox_predict(char *home_dir)
364
setenv_sandbox_predict(char *home_dir)
354
{
365
{
355
  char sandbox_predict_var[1024];
366
  char sandbox_predict_var[1024];
356
367
Lines 369-375 Link Here
369
  }
380
  }
370
}
381
}
371
382
372
int print_sandbox_log(char *sandbox_log)
383
int
384
print_sandbox_log(char *sandbox_log)
373
{
385
{
374
  int sandbox_log_file = -1;
386
  int sandbox_log_file = -1;
375
  char *beep_count_env = NULL;
387
  char *beep_count_env = NULL;
Lines 377-400 Link Here
377
  long len = 0;
389
  long len = 0;
378
  char *buffer = NULL;
390
  char *buffer = NULL;
379
391
380
  sandbox_log_file=file_open(sandbox_log, "r", 0);
392
	sandbox_log_file = file_open(sandbox_log, "r", 0);
381
  if (-1 == sandbox_log_file)
393
  if (-1 == sandbox_log_file)
382
    return 0;
394
    return 0;
383
395
384
  len = file_length(sandbox_log_file);
396
  len = file_length(sandbox_log_file);
385
  buffer = (char *)malloc((len + 1)*sizeof(char));
397
	buffer = (char *) malloc((len + 1) * sizeof (char));
386
  memset(buffer, 0, len + 1);
398
  memset(buffer, 0, len + 1);
387
  read(sandbox_log_file, buffer, len);
399
  read(sandbox_log_file, buffer, len);
388
  file_close(sandbox_log_file);
400
  file_close(sandbox_log_file);
389
401
390
  printf("\e[31;01m--------------------------- ACCESS VIOLATION SUMMARY ---------------------------\033[0m\n");
402
	printf
403
			("\e[31;01m--------------------------- ACCESS VIOLATION SUMMARY ---------------------------\033[0m\n");
391
  printf("\e[31;01mLOG FILE = \"%s\"\033[0m\n", sandbox_log);
404
  printf("\e[31;01mLOG FILE = \"%s\"\033[0m\n", sandbox_log);
392
  printf("\n");
405
  printf("\n");
393
  printf("%s", buffer);
406
  printf("%s", buffer);
394
  if (buffer)
407
  if (buffer)
395
    free(buffer);
408
    free(buffer);
396
  buffer = NULL;
409
  buffer = NULL;
397
  printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
410
	printf
411
			("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
398
412
399
  beep_count_env = getenv(ENV_SANDBOX_BEEP);
413
  beep_count_env = getenv(ENV_SANDBOX_BEEP);
400
  if (beep_count_env)
414
  if (beep_count_env)
Lines 404-416 Link Here
404
418
405
  for (i = 0; i < beep_count; i++) {
419
  for (i = 0; i < beep_count; i++) {
406
    fputc('\a', stderr);
420
    fputc('\a', stderr);
407
    if (i < beep_count -1)
421
		if (i < beep_count - 1)
408
      sleep(1);
422
      sleep(1);
409
  }
423
  }
410
  return 1;
424
  return 1;
411
}
425
}
412
426
413
int spawn_shell(char *argv_bash[]) 
427
int
428
spawn_shell(char *argv_bash[])
414
{
429
{
415
#ifdef USE_SYSTEM_SHELL
430
#ifdef USE_SYSTEM_SHELL
416
  int i = 0;
431
  int i = 0;
Lines 424-430 Link Here
424
      break;
439
      break;
425
    if (NULL != sh)
440
    if (NULL != sh)
426
      len = strlen(sh);
441
      len = strlen(sh);
427
    sh = (char *)realloc(sh, len+strlen(argv_bash[i]) + 5);
442
		sh = (char *) realloc(sh, len + strlen(argv_bash[i]) + 5);
428
    if (first) {
443
    if (first) {
429
      sh[0] = 0;
444
      sh[0] = 0;
430
      first = 0;
445
      first = 0;
Lines 471-477 Link Here
471
#endif
486
#endif
472
}
487
}
473
488
474
int main(int argc, char** argv)
489
int
490
main(int argc, char **argv)
475
{
491
{
476
  int i = 0, success = 1;
492
  int i = 0, success = 1;
477
#ifdef USE_LD_SO_PRELOAD
493
#ifdef USE_LD_SO_PRELOAD
Lines 510-520 Link Here
510
    print_debug = 1;
526
    print_debug = 1;
511
527
512
  if (print_debug)
528
  if (print_debug)
513
    printf("========================== Gentoo linux path sandbox ===========================\n");
529
		printf
530
				("========================== Gentoo linux path sandbox ===========================\n");
514
531
515
  /* check if a sandbox is already running */
532
  /* check if a sandbox is already running */
516
  if (NULL != getenv(ENV_SANDBOX_ON)) {
533
  if (NULL != getenv(ENV_SANDBOX_ON)) {
517
    fprintf(stderr, "Not launching a new sandbox instance\nAnother one is already running in this process hierarchy.\n");
534
		fprintf(stderr,
535
						"Not launching a new sandbox instance\nAnother one is already running in this process hierarchy.\n");
518
    exit(1);
536
    exit(1);
519
  } else {
537
  } else {
520
538
Lines 549-561 Link Here
549
      printf("Verification of the required files.\n");
567
      printf("Verification of the required files.\n");
550
568
551
    if (file_exist(sandbox_lib, 0) <= 0) {
569
    if (file_exist(sandbox_lib, 0) <= 0) {
552
      fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
570
			fprintf(stderr, "Could not open the sandbox library at '%s'.\n",
571
							sandbox_lib);
553
      return -1;
572
      return -1;
554
    } else if (file_exist(sandbox_rc, 0) <= 0) {
573
    } else if (file_exist(sandbox_rc, 0) <= 0) {
555
      fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
574
			fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n",
575
							sandbox_rc);
556
      return -1;
576
      return -1;
557
    }
577
    }
558
559
#ifdef USE_LD_SO_PRELOAD
578
#ifdef USE_LD_SO_PRELOAD
560
    /* ensure that the /etc/ld.so.preload file contains an entry for the sandbox lib */
579
    /* ensure that the /etc/ld.so.preload file contains an entry for the sandbox lib */
561
    if (print_debug)
580
    if (print_debug)
Lines 569-575 Link Here
569
588
570
    if (getuid() == 0) {
589
    if (getuid() == 0) {
571
      /* Our r+ also will create the file if it doesn't exist */
590
      /* Our r+ also will create the file if it doesn't exist */
572
      preload_file=file_open("/etc/ld.so.preload", "r+", 1, 0644);
591
			preload_file = file_open("/etc/ld.so.preload", "r+", 1, 0644);
573
      if (-1 == preload_file) {
592
      if (-1 == preload_file) {
574
        preload_adaptable = 0;
593
        preload_adaptable = 0;
575
/*      exit(1);*/
594
/*      exit(1);*/
Lines 591-604 Link Here
591
      for (i = 0; i < num_of_preloads + 1; i++) {
610
      for (i = 0; i < num_of_preloads + 1; i++) {
592
        /* First entry should be our sandbox library */
611
        /* First entry should be our sandbox library */
593
        if (0 == i) {
612
        if (0 == i) {
594
          if (write(preload_file, sandbox_lib, strlen(sandbox_lib)) != strlen(sandbox_lib)) {
613
					if (write
614
							(preload_file, sandbox_lib,
615
							 strlen(sandbox_lib)) != strlen(sandbox_lib)) {
595
            perror(">>> /etc/ld.so.preload file write");
616
            perror(">>> /etc/ld.so.preload file write");
596
            success = 0;
617
            success = 0;
597
            break;
618
            break;
598
          }
619
          }
599
        } else {
620
        } else {
600
          /* Output all other preload entries */
621
          /* Output all other preload entries */
601
          if (write(preload_file, preload_array[i - 1], strlen(preload_array[i - 1])) != strlen(preload_array[i - 1])) {
622
					if (write
623
							(preload_file, preload_array[i - 1],
624
							 strlen(preload_array[i - 1])) != strlen(preload_array[i - 1])) {
602
            perror(">>> /etc/ld.so.preload file write");
625
            perror(">>> /etc/ld.so.preload file write");
603
            success = 0;
626
            success = 0;
604
            break;
627
            break;
Lines 634-640 Link Here
634
      printf("Setting up the required environment variables.\n");
657
      printf("Setting up the required environment variables.\n");
635
658
636
    /* Generate sandbox log full path */
659
    /* Generate sandbox log full path */
637
    tmp_string=get_sandbox_log();
660
		tmp_string = get_sandbox_log();
638
    strncpy(sandbox_log, tmp_string, 254);
661
    strncpy(sandbox_log, tmp_string, 254);
639
    if (tmp_string)
662
    if (tmp_string)
640
      free(tmp_string);
663
      free(tmp_string);
Lines 642-648 Link Here
642
665
643
    setenv(ENV_SANDBOX_LOG, sandbox_log, 1);
666
    setenv(ENV_SANDBOX_LOG, sandbox_log, 1);
644
667
645
    snprintf(sandbox_debug_log, 254, "%s%s%s", DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
668
		snprintf(sandbox_debug_log, 254, "%s%s%s",
669
						 DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
646
    setenv(ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log, 1);
670
    setenv(ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log, 1);
647
671
648
    home_dir = getenv("HOME");
672
    home_dir = getenv("HOME");
Lines 656-664 Link Here
656
     * this, access is denied to /var/tmp, hurtin' ebuilds.
680
     * this, access is denied to /var/tmp, hurtin' ebuilds.
657
     */
681
     */
658
682
659
    realpath(getenv("PORTAGE_TMPDIR"),portage_tmp_dir);
683
		realpath(getenv("PORTAGE_TMPDIR"), portage_tmp_dir);
660
    realpath("/var/tmp",var_tmp_dir);
684
		realpath("/var/tmp", var_tmp_dir);
661
    realpath("/tmp",tmp_dir);
685
		realpath("/tmp", tmp_dir);
662
686
663
    setenv(ENV_SANDBOX_DIR, sandbox_dir, 1);
687
    setenv(ENV_SANDBOX_DIR, sandbox_dir, 1);
664
    setenv(ENV_SANDBOX_LIB, sandbox_lib, 1);
688
    setenv(ENV_SANDBOX_LIB, sandbox_lib, 1);
Lines 680-686 Link Here
680
    if (NULL != portage_tmp_dir)
704
    if (NULL != portage_tmp_dir)
681
      chdir(portage_tmp_dir);
705
      chdir(portage_tmp_dir);
682
706
683
    argv_bash=(char **)malloc(6 * sizeof(char *));
707
		argv_bash = (char **) malloc(6 * sizeof (char *));
684
    argv_bash[0] = strdup("/bin/bash");
708
    argv_bash[0] = strdup("/bin/bash");
685
    argv_bash[1] = strdup("-rcfile");
709
    argv_bash[1] = strdup("-rcfile");
686
    argv_bash[2] = strdup(sandbox_rc);
710
    argv_bash[2] = strdup(sandbox_rc);
Lines 694-706 Link Here
694
    argv_bash[5] = NULL;
718
    argv_bash[5] = NULL;
695
    
719
    
696
    if (argc >= 2) {
720
    if (argc >= 2) {
697
      for (i = 1; i< argc; i++) {
721
			for (i = 1; i < argc; i++) {
698
        if (NULL == argv_bash[4])
722
        if (NULL == argv_bash[4])
699
          len = 0;
723
          len = 0;
700
        else
724
        else
701
          len = strlen(argv_bash[4]);
725
          len = strlen(argv_bash[4]);
702
        
726
        
703
        argv_bash[4]=(char *)realloc(argv_bash[4], (len + strlen(argv[i]) + 2) * sizeof(char));
727
				argv_bash[4] =
728
						(char *) realloc(argv_bash[4],
729
														 (len + strlen(argv[i]) + 2) * sizeof (char));
704
        
730
        
705
        if (0 == len)
731
        if (0 == len)
706
          argv_bash[4][0] = 0;
732
          argv_bash[4][0] = 0;
Lines 711-717 Link Here
711
      }
737
      }
712
    }
738
    }
713
#if 0
739
#if 0
714
    char* argv_bash[] = {
740
		char *argv_bash[] = {
715
                        "/bin/bash",
741
                        "/bin/bash",
716
                        "-rcfile",
742
                        "-rcfile",
717
                        NULL,
743
                        NULL,
Lines 773-779 Link Here
773
        else
799
        else
774
          sprintf(pid_string, "%d\n", pids_array[i]);
800
          sprintf(pid_string, "%d\n", pids_array[i]);
775
        
801
        
776
        if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
802
				if (write(pids_file, pid_string, strlen(pid_string)) !=
803
						strlen(pid_string)) {
777
          perror(">>> pids file write");
804
          perror(">>> pids file write");
778
          success = 0;
805
          success = 0;
779
          break;
806
          break;
Lines 798-804 Link Here
798
    /* STARTING PROTECTED ENVIRONMENT */
825
    /* STARTING PROTECTED ENVIRONMENT */
799
    if (print_debug) {
826
    if (print_debug) {
800
      printf("The protected environment has been started.\n");
827
      printf("The protected environment has been started.\n");
801
      printf("--------------------------------------------------------------------------------\n");
828
			printf
829
					("--------------------------------------------------------------------------------\n");
802
    }
830
    }
803
831
804
    if (print_debug)
832
    if (print_debug)
Lines 827-833 Link Here
827
    cleanup();
855
    cleanup();
828
856
829
    if (print_debug) {
857
    if (print_debug) {
830
      printf("========================== Gentoo linux path sandbox ===========================\n");
858
			printf
859
					("========================== Gentoo linux path sandbox ===========================\n");
831
      printf("The protected environment has been shut down.\n");
860
      printf("The protected environment has been shut down.\n");
832
    }
861
    }
833
862
Lines 844-850 Link Here
844
873
845
      sandbox_log_file = -1;
874
      sandbox_log_file = -1;
846
    } else if (print_debug) {
875
    } else if (print_debug) {
847
      printf("--------------------------------------------------------------------------------\n");
876
			printf
877
					("--------------------------------------------------------------------------------\n");
848
    }
878
    }
849
879
850
    if ((sandbox_log_presence) || (!success))
880
    if ((sandbox_log_presence) || (!success))
(-)sandbox_futils.c (-38 / +60 lines)
Lines 26-33 Link Here
26
26
27
#include "sandbox.h"
27
#include "sandbox.h"
28
28
29
/* glibc modified getcwd() functions */
30
char *egetcwd(char *, size_t);
29
31
30
char *get_sandbox_path(char *argv0)
32
char *
33
get_sandbox_path(char *argv0)
31
{
34
{
32
  char path[255];
35
  char path[255];
33
  char *cwd = NULL;
36
  char *cwd = NULL;
Lines 38-54 Link Here
38
41
39
  /* ARGV[0] specifies relative path */
42
  /* ARGV[0] specifies relative path */
40
  } else {
43
  } else {
41
      getcwd(cwd, 253);
44
		egetcwd(cwd, 253);
42
      sprintf(path, "%s/%s", cwd, argv0);
45
      sprintf(path, "%s/%s", cwd, argv0);
43
      if (cwd) free(cwd);
46
		if (cwd)
47
			free(cwd);
44
      cwd = NULL;
48
      cwd = NULL;
45
  }
49
  }
46
50
47
  /* Return just directory */
51
  /* Return just directory */
48
  return(sb_dirname(path));
52
	return (sb_dirname(path));
49
}
53
}
50
54
51
char *get_sandbox_lib(char *sb_path)
55
char *
56
get_sandbox_lib(char *sb_path)
52
{
57
{
53
  char path[255];
58
  char path[255];
54
59
Lines 56-65 Link Here
56
  if (file_exist(path, 0) <= 0) {
61
  if (file_exist(path, 0) <= 0) {
57
    snprintf(path, 254, "%s%s", sb_path, LIB_NAME);
62
    snprintf(path, 254, "%s%s", sb_path, LIB_NAME);
58
  }
63
  }
59
  return(strdup(path));
64
	return (strdup(path));
60
}
65
}
61
66
62
char *get_sandbox_rc(char *sb_path)
67
char *
68
get_sandbox_rc(char *sb_path)
63
{
69
{
64
  char path[255];
70
  char path[255];
65
71
Lines 67-76 Link Here
67
  if (file_exist(path, 0) <= 0) {
73
  if (file_exist(path, 0) <= 0) {
68
    snprintf(path, 254, "%s%s", sb_path, BASHRC_NAME);
74
    snprintf(path, 254, "%s%s", sb_path, BASHRC_NAME);
69
  }
75
  }
70
  return(strdup(path));
76
	return (strdup(path));
71
}
77
}
72
78
73
char *get_sandbox_log()
79
char *
80
get_sandbox_log()
74
{
81
{
75
  char path[255];
82
  char path[255];
76
  char pid_string[20];
83
  char pid_string[20];
Lines 86-96 Link Here
86
  }
93
  }
87
  strcat(path, pid_string);
94
  strcat(path, pid_string);
88
  strcat(path, LOG_FILE_EXT);
95
  strcat(path, LOG_FILE_EXT);
89
  return(strdup(path));
96
	return (strdup(path));
90
}
97
}
91
98
92
/* Obtain base directory name. Do not allow trailing / */
99
/* Obtain base directory name. Do not allow trailing / */
93
char *sb_dirname(const char *path)
100
char *
101
sb_dirname(const char *path)
94
{
102
{
95
  char *ret = NULL;
103
  char *ret = NULL;
96
  char *ptr = NULL;
104
  char *ptr = NULL;
Lines 98-116 Link Here
98
  int cut_len = -1;
106
  int cut_len = -1;
99
107
100
  /* don't think NULL will ever be passed, but just in case */
108
  /* don't think NULL will ever be passed, but just in case */
101
  if (NULL == path) return(strdup("."));
109
	if (NULL == path)
110
		return (strdup("."));
102
111
103
  /* Grab pointer to last slash */
112
  /* Grab pointer to last slash */
104
  ptr = strrchr(path, '/');
113
  ptr = strrchr(path, '/');
105
  if (NULL == ptr) {
114
  if (NULL == ptr) {
106
    return(strdup("."));
115
		return (strdup("."));
107
  }
116
  }
108
117
109
  /* decimal location of pointer */
118
  /* decimal location of pointer */
110
  loc = ptr - path;
119
  loc = ptr - path;
111
120
112
  /* Remove any trailing slash */
121
  /* Remove any trailing slash */
113
  for (i = loc-1; i >= 0; i--) {
122
	for (i = loc - 1; i >= 0; i--) {
114
    if (path[i] != '/') {
123
    if (path[i] != '/') {
115
      cut_len = i + 1;  /* make cut_len the length of the string to keep */
124
      cut_len = i + 1;  /* make cut_len the length of the string to keep */
116
      break;
125
      break;
Lines 118-131 Link Here
118
  }
127
  }
119
  
128
  
120
  /* It could have been just a plain /, return a 1byte 0 filled string */
129
  /* It could have been just a plain /, return a 1byte 0 filled string */
121
  if (-1 == cut_len) return(strdup(""));
130
	if (-1 == cut_len)
131
		return (strdup(""));
122
132
123
  /* Allocate memory, and return the directory */
133
  /* Allocate memory, and return the directory */
124
  ret = (char *)malloc((cut_len + 1) * sizeof(char));
134
	ret = (char *) malloc((cut_len + 1) * sizeof (char));
125
  memcpy(ret, path, cut_len);
135
  memcpy(ret, path, cut_len);
126
  ret[cut_len] = 0;
136
  ret[cut_len] = 0;
127
137
128
  return(ret);
138
	return (ret);
129
}
139
}
130
140
131
/*
141
/*
Lines 153-159 Link Here
153
}*/
163
}*/
154
164
155
/* Convert text (string) modes to integer values */
165
/* Convert text (string) modes to integer values */
156
int file_getmode(char *mode)
166
int
167
file_getmode(char *mode)
157
{
168
{
158
  int mde = 0;
169
  int mde = 0;
159
  if (0 == strcasecmp(mode, "r+")) {
170
  if (0 == strcasecmp(mode, "r+")) {
Lines 171-187 Link Here
171
  } else {
182
  } else {
172
    mde = O_RDONLY;
183
    mde = O_RDONLY;
173
  }
184
  }
174
  return(mde);
185
	return (mde);
175
}
186
}
176
187
177
/* Get current position in file */
188
/* Get current position in file */
178
long file_tell(int fp)
189
long
190
file_tell(int fp)
179
{
191
{
180
  return(lseek(fp, 0L, SEEK_CUR));
192
	return (lseek(fp, 0L, SEEK_CUR));
181
}
193
}
182
194
183
/* lock the file, preferrably the POSIX way */
195
/* lock the file, preferrably the POSIX way */
184
int file_lock(int fd, int lock, char *filename)
196
int
197
file_lock(int fd, int lock, char *filename)
185
{
198
{
186
  int err;
199
  int err;
187
#ifdef USE_FLOCK
200
#ifdef USE_FLOCK
Lines 207-213 Link Here
207
}
220
}
208
221
209
/* unlock the file, preferrably the POSIX way */
222
/* unlock the file, preferrably the POSIX way */
210
int file_unlock(int fd)
223
int
224
file_unlock(int fd)
211
{
225
{
212
#ifdef USE_FLOCK
226
#ifdef USE_FLOCK
213
  if (flock(fd, LOCK_UN) < 0) {
227
  if (flock(fd, LOCK_UN) < 0) {
Lines 232-254 Link Here
232
/* Auto-determine from how the file was opened, what kind of lock to lock
246
/* Auto-determine from how the file was opened, what kind of lock to lock
233
 * the file with
247
 * the file with
234
 */
248
 */
235
int file_locktype(char *mode)
249
int
250
file_locktype(char *mode)
236
{
251
{
237
#ifdef USE_FLOCK
252
#ifdef USE_FLOCK
238
  if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+')) || (NULL != strchr(mode, 'a')))
253
	if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+'))
239
    return(LOCK_EX);
254
			|| (NULL != strchr(mode, 'a')))
240
  return(LOCK_SH);
255
		return (LOCK_EX);
256
	return (LOCK_SH);
241
#else
257
#else
242
  if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+')) || (NULL != strchr(mode, 'a')))
258
	if (NULL != (strchr(mode, 'w')) || (NULL != strchr(mode, '+'))
243
    return(F_WRLCK);
259
			|| (NULL != strchr(mode, 'a')))
244
  return(F_RDLCK);
260
		return (F_WRLCK);
261
	return (F_RDLCK);
245
#endif
262
#endif
246
}
263
}
247
264
248
/* Use standard fopen style modes to open the specified file.  Also auto-determines and
265
/* Use standard fopen style modes to open the specified file.  Also auto-determines and
249
 * locks the file either in shared or exclusive mode depending on opening mode
266
 * locks the file either in shared or exclusive mode depending on opening mode
250
 */
267
 */
251
int file_open(char *filename, char *mode, int perm_specified, ...)
268
int
269
file_open(char *filename, char *mode, int perm_specified, ...)
252
{
270
{
253
  int fd;
271
  int fd;
254
  char error[250];
272
  char error[250];
Lines 268-274 Link Here
268
  if (-1 == fd) {
286
  if (-1 == fd) {
269
    snprintf(error, 249, ">>> %s file mode: %s open", filename, mode);
287
    snprintf(error, 249, ">>> %s file mode: %s open", filename, mode);
270
    perror(error);
288
    perror(error);
271
    return(fd);
289
		return (fd);
272
  }
290
  }
273
  /* Only lock the file if opening succeeded */
291
  /* Only lock the file if opening succeeded */
274
  if (-1 != fd) {
292
  if (-1 != fd) {
Lines 280-290 Link Here
280
    snprintf(error, 249, ">>> %s file mode:%s open", filename, mode);
298
    snprintf(error, 249, ">>> %s file mode:%s open", filename, mode);
281
    perror(error);
299
    perror(error);
282
  }
300
  }
283
  return(fd);
301
	return (fd);
284
}
302
}
285
303
286
/* Close and unlock file */
304
/* Close and unlock file */
287
void file_close(int fd)
305
void
306
file_close(int fd)
288
{
307
{
289
  if (-1 != fd) {
308
  if (-1 != fd) {
290
    file_unlock(fd);
309
    file_unlock(fd);
Lines 293-309 Link Here
293
}
312
}
294
313
295
/* Return length of file */
314
/* Return length of file */
296
long file_length(int fd)
315
long
316
file_length(int fd)
297
{
317
{
298
  long pos, len;
318
  long pos, len;
299
  pos = file_tell(fd);
319
  pos = file_tell(fd);
300
  len = lseek(fd, 0L, SEEK_END);
320
  len = lseek(fd, 0L, SEEK_END);
301
  lseek(fd, pos, SEEK_SET);
321
  lseek(fd, pos, SEEK_SET);
302
  return(len);
322
	return (len);
303
}
323
}
304
324
305
/* Zero out file */
325
/* Zero out file */
306
int file_truncate(int fd)
326
int
327
file_truncate(int fd)
307
{
328
{
308
  lseek(fd, 0L, SEEK_SET);
329
  lseek(fd, 0L, SEEK_SET);
309
  if (ftruncate(fd, 0) < 0) {
330
  if (ftruncate(fd, 0) < 0) {
Lines 314-320 Link Here
314
}
335
}
315
336
316
/* Check to see if a file exists Return: 1 success, 0 file not found, -1 error */
337
/* Check to see if a file exists Return: 1 success, 0 file not found, -1 error */
317
int file_exist(char *filename, int checkmode)
338
int
339
file_exist(char *filename, int checkmode)
318
{
340
{
319
  struct stat mystat;
341
  struct stat mystat;
320
342

Return to bug 21766