-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesys_hooks.c
205 lines (145 loc) · 5.86 KB
/
filesys_hooks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "common.h"
#include "actions.h"
#include "config.h"
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
int (*enhancer_real_chown) (const char *file, uid_t owner, gid_t group)=NULL;
int (*enhancer_real_fchown) (int fd, uid_t owner, gid_t group)=NULL;
int (*enhancer_real_chmod) (const char *file, mode_t mode)=NULL;
int (*enhancer_real_fchmod) (int fd, mode_t mode)=NULL;
int (*enhancer_real_chdir) (const char *path)=NULL;
int (*enhancer_real_fchdir) (int fd)=NULL;
int (*enhancer_real_chroot) (const char *path)=NULL;
int (*enhancer_real_fchroot) (int fd)=NULL;
int fchown (int fd, uid_t owner, gid_t group)
{
int result, Flags;
struct stat Stat;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHOWN, "fchown", NULL, NULL, owner, group);
if (Flags & FLAG_DENY) return(-1);
if (Flags & FLAG_PRETEND) return(0);
fstat(fd, &Stat);
if ((Flags & FLAG_DENY_LINKS) && (Stat.st_nlink > 1)) result=-1;
else if ((Flags & FLAG_DENY_SYMLINKS) && (S_ISLNK(Stat.st_mode))) result=-1;
else result=enhancer_real_fchown(fd,owner,group);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("fchown");
return(result);
}
int chown (const char *path, uid_t owner, gid_t group)
{
int result, Flags, fd;
struct stat Stat;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHOWN, "chown", path, NULL, owner, group);
if (Flags & FLAG_DENY) return(-1);
if (Flags & FLAG_PRETEND) return(0);
fd=open(path,O_RDWR);
fstat(fd,&Stat);
if ((Flags & FLAG_DENY_LINKS) && (Stat.st_nlink > 1)) result=-1;
else if ((Flags & FLAG_DENY_SYMLINKS) && (S_ISLNK(Stat.st_mode))) result=-1;
else result=enhancer_real_fchown(fd,owner,group);
close(fd);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("chown");
return(result);
}
int fchmod (int fd, mode_t mode)
{
int result, Flags;
struct stat Stat;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHMOD, "fchmod", NULL, NULL, mode, 0);
if (Flags & FLAG_DENY) return(-1);
if (Flags & FLAG_PRETEND) return(0);
fstat(fd,&Stat);
if ((Flags & FLAG_DENY_LINKS) && (Stat.st_nlink > 1)) result=-1;
else if ((Flags & FLAG_DENY_SYMLINKS) && (S_ISLNK(Stat.st_mode))) result=-1;
else result=enhancer_real_fchmod(fd,mode);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("fchmod");
return(result);
}
int chmod(const char *path, mode_t mode)
{
int result, Flags, fd;
struct stat Stat;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHOWN, "chmod", path, NULL, mode, 0);
fd=open(path,O_RDWR);
if (Flags & FLAG_DENY) return(-1);
if (Flags & FLAG_PRETEND) return(0);
fstat(fd,&Stat);
if ((Flags & FLAG_DENY_LINKS) && (Stat.st_nlink > 1)) result=-1;
else if ((Flags & FLAG_DENY_SYMLINKS) && (S_ISLNK(Stat.st_mode))) result=-1;
else result=enhancer_real_fchmod(fd,mode);
close(fd);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("chmod");
return(result);
}
int chdir(const char *path)
{
int result, Flags, fd;
struct stat Stat;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHDIR, "chdir", path, NULL, 0, 0);
if (Flags & FLAG_DENY) return(-1);
fd=open(path, O_PATH);
fstat(fd,&Stat);
if ((Flags & FLAG_DENY_LINKS) && (Stat.st_nlink > 1)) result=-1;
else if ((Flags & FLAG_DENY_SYMLINKS) && (S_ISLNK(Stat.st_mode))) result=-1;
else result=enhancer_real_fchdir(fd);
close(fd);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("chdir");
return(result);
}
int fchdir(int fd)
{
int result, Flags;
struct stat Stat;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHDIR, "fchdir", NULL, NULL, 0, 0);
if (Flags & FLAG_DENY) return(-1);
fstat(fd,&Stat);
if ((Flags & FLAG_DENY_LINKS) && (Stat.st_nlink > 1)) result=-1;
else if ((Flags & FLAG_DENY_SYMLINKS) && (S_ISLNK(Stat.st_mode))) result=-1;
else result=enhancer_real_fchdir(fd);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("fchdir");
return(result);
}
int chroot(const char *path)
{
int result, Flags, fd;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHROOT, "chroot", path, NULL, 0, 0);
if (Flags & FLAG_DENY) return(-1);
if (Flags & FLAG_PRETEND) return(0);
fd=open(path,O_RDWR);
result=enhancer_real_fchroot(fd);
if (result==0) enhancer_flags |= ENHANCER_STATE_CHROOTED;
close(fd);
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("chroot");
return(result);
}
int fchroot(int fd)
{
int result, Flags;
Flags=enhancer_checkconfig_chfile_function(FUNC_CHROOT, "fchroot", NULL, NULL, 0, 0);
if (Flags & FLAG_DENY) return(-1);
if (Flags & FLAG_PRETEND) return(0);
result=enhancer_real_fchroot(fd);
if (result==0) enhancer_flags |= ENHANCER_STATE_CHROOTED;
if ((result !=0) && (Flags & FLAG_FAILDIE)) enhancer_fail_die("fchroot");
return(result);
}
void enhancer_filesys_hooks()
{
if (! enhancer_real_chown) enhancer_real_chown= dlsym(RTLD_NEXT, "chown");
if (! enhancer_real_fchown) enhancer_real_fchown= dlsym(RTLD_NEXT, "fchown");
if (! enhancer_real_chmod) enhancer_real_chmod= dlsym(RTLD_NEXT, "chmod");
if (! enhancer_real_fchmod) enhancer_real_fchmod= dlsym(RTLD_NEXT, "fchmod");
if (! enhancer_real_chdir) enhancer_real_chdir= dlsym(RTLD_NEXT, "chdir");
if (! enhancer_real_fchdir) enhancer_real_fchdir= dlsym(RTLD_NEXT, "fchdir");
if (! enhancer_real_chroot) enhancer_real_chroot= dlsym(RTLD_NEXT, "chroot");
/*
if (! enhancer_real_lchmod) enhancer_real_setuid= dlsym(RTLD_NEXT, "lchown");
if (! enhancer_real_fchownat) enhancer_real_setuid= dlsym(RTLD_NEXT, "fchownat");
*/
/*
if (! enhancer_real_lchown) enhancer_real_setuid= dlsym(RTLD_NEXT, "lchown");
if (! enhancer_real_fchownat) enhancer_real_setuid= dlsym(RTLD_NEXT, "fchownat");
*/
}