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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* This file deals with protection in the file system. It contains the code
* for four system calls that relate to protection.
*
* The entry points into this file are
* do_chmod: perform the CHMOD and FCHMOD system calls
* do_chown: perform the CHOWN and FCHOWN system calls
* do_umask: perform the UMASK system call
* do_access: perform the ACCESS system call
*/
#include "fs.h"
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <minix/callnr.h>
#include "file.h"
#include "path.h"
#include <minix/vfsif.h>
#include "vnode.h"
#include "vmnt.h"
/*===========================================================================*
* do_chmod *
*===========================================================================*/
int do_chmod(void)
{
/* Perform the chmod(name, mode) and fchmod(fd, mode) system calls.
* syscall might provide 'name' embedded in the message.
*/
struct filp *flp;
struct vnode *vp;
struct vmnt *vmp;
int r, rfd;
mode_t result_mode;
char fullpath[PATH_MAX];
struct lookup resolve;
mode_t new_mode;
flp = NULL;
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_WRITE;
if (job_call_nr == VFS_CHMOD) {
new_mode = job_m_in.m_lc_vfs_path.mode;
/* Temporarily open the file */
if (copy_path(fullpath, sizeof(fullpath)) != OK)
return(err_code);
if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);
} else { /* call_nr == VFS_FCHMOD */
rfd = job_m_in.m_lc_vfs_fchmod.fd;
new_mode = job_m_in.m_lc_vfs_fchmod.mode;
/* File is already opened; get a pointer to vnode from filp. */
if ((flp = get_filp(rfd, VNODE_WRITE)) == NULL) return(err_code);
vp = flp->filp_vno;
assert(vp);
dup_vnode(vp);
}
assert(vp);
/* Only the owner or the super_user may change the mode of a file.
* No one may change the mode of a file on a read-only file system.
*/
if (vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID)
r = EPERM;
else
r = read_only(vp);
if (r == OK) {
/* Now make the change. Clear setgid bit if file is not in caller's
* group */
if (fp->fp_effuid != SU_UID && vp->v_gid != fp->fp_effgid)
new_mode &= ~I_SET_GID_BIT;
r = req_chmod(vp->v_fs_e, vp->v_inode_nr, new_mode, &result_mode);
if (r == OK)
vp->v_mode = result_mode;
}
if (job_call_nr == VFS_CHMOD) {
unlock_vnode(vp);
unlock_vmnt(vmp);
} else { /* VFS_FCHMOD */
unlock_filp(flp);
}
put_vnode(vp);
return(r);
}
/*===========================================================================*
* do_chown *
*===========================================================================*/
int do_chown(void)
{
/* Perform the chown(path, owner, group) and fchmod(fd, owner, group) system
* calls. */
struct filp *flp;
struct vnode *vp;
struct vmnt *vmp;
int r, rfd;
uid_t uid, new_uid;
gid_t gid, new_gid;
mode_t new_mode;
char fullpath[PATH_MAX];
struct lookup resolve;
vir_bytes vname1;
size_t vname1_length;
flp = NULL;
uid = job_m_in.m_lc_vfs_chown.owner;
gid = job_m_in.m_lc_vfs_chown.group;
if (job_call_nr == VFS_CHOWN) {
vname1 = job_m_in.m_lc_vfs_chown.name;
vname1_length = job_m_in.m_lc_vfs_chown.len;
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_WRITE;
/* Temporarily open the file. */
if (fetch_name(vname1, vname1_length, fullpath) != OK)
return(err_code);
if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);
} else { /* call_nr == VFS_FCHOWN */
rfd = job_m_in.m_lc_vfs_chown.fd;
/* File is already opened; get a pointer to the vnode from filp. */
if ((flp = get_filp(rfd, VNODE_WRITE)) == NULL)
return(err_code);
vp = flp->filp_vno;
dup_vnode(vp);
}
r = read_only(vp);
if (r == OK) {
/* FS is R/W. Whether call is allowed depends on ownership, etc. */
/* The super user can do anything, so check permissions only if we're
a regular user. */
if (fp->fp_effuid != SU_UID) {
/* Regular users can only change groups of their own files. */
if (vp->v_uid != fp->fp_effuid) r = EPERM;
if (vp->v_uid != uid) r = EPERM; /* no giving away */
if (fp->fp_effgid != gid) r = EPERM;
}
}
if (r == OK) {
/* Do not change uid/gid if new uid/gid is -1. */
new_uid = (uid == (uid_t)-1 ? vp->v_uid : uid);
new_gid = (gid == (gid_t)-1 ? vp->v_gid : gid);
if (new_uid > UID_MAX || new_gid > GID_MAX)
r = EINVAL;
else if ((r = req_chown(vp->v_fs_e, vp->v_inode_nr, new_uid, new_gid,
&new_mode)) == OK) {
vp->v_uid = new_uid;
vp->v_gid = new_gid;
vp->v_mode = new_mode;
}
}
if (job_call_nr == VFS_CHOWN) {
unlock_vnode(vp);
unlock_vmnt(vmp);
} else { /* VFS_FCHOWN */
unlock_filp(flp);
}
put_vnode(vp);
return(r);
}
/*===========================================================================*
* do_umask *
*===========================================================================*/
int do_umask(void)
{
/* Perform the umask(2) system call. */
mode_t complement, new_umask;
new_umask = job_m_in.m_lc_vfs_umask.mask;
complement = ~fp->fp_umask; /* set 'r' to complement of old mask */
fp->fp_umask = ~(new_umask & RWX_MODES);
return(complement); /* return complement of old mask */
}
/*===========================================================================*
* do_access *
*===========================================================================*/
int do_access(void)
{
/* Perform the access(name, mode) system call.
* syscall might provide 'name' embedded in the message.
*/
int r;
struct vnode *vp;
struct vmnt *vmp;
char fullpath[PATH_MAX];
struct lookup resolve;
mode_t access;
access = job_m_in.m_lc_vfs_path.mode;
lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp);
resolve.l_vmnt_lock = VMNT_READ;
resolve.l_vnode_lock = VNODE_READ;
/* First check to see if the mode is correct. */
if ( (access & ~(R_OK | W_OK | X_OK)) != 0 && access != F_OK)
return(EINVAL);
/* Temporarily open the file. */
if (copy_path(fullpath, sizeof(fullpath)) != OK)
return(err_code);
if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);
r = forbidden(fp, vp, access);
unlock_vnode(vp);
unlock_vmnt(vmp);
put_vnode(vp);
return(r);
}
/*===========================================================================*
* forbidden *
*===========================================================================*/
int forbidden(struct fproc *rfp, struct vnode *vp, mode_t access_desired)
{
/* Given a pointer to an vnode, 'vp', and the access desired, determine
* if the access is allowed, and if not why not. The routine looks up the
* caller's uid in the 'fproc' table. If access is allowed, OK is returned
* if it is forbidden, EACCES is returned.
*/
register mode_t bits, perm_bits;
uid_t uid;
gid_t gid;
int r, shift;
if (vp->v_uid == (uid_t) -1 || vp->v_gid == (gid_t) -1) return(EACCES);
/* Isolate the relevant rwx bits from the mode. */
bits = vp->v_mode;
uid = (job_call_nr == VFS_ACCESS ? rfp->fp_realuid : rfp->fp_effuid);
gid = (job_call_nr == VFS_ACCESS ? rfp->fp_realgid : rfp->fp_effgid);
if (uid == SU_UID) {
/* Grant read and write permission. Grant search permission for
* directories. Grant execute permission (for non-directories) if
* and only if one of the 'X' bits is set.
*/
if ( S_ISDIR(bits) || bits & ((X_BIT << 6) | (X_BIT << 3) | X_BIT))
perm_bits = R_BIT | W_BIT | X_BIT;
else
perm_bits = R_BIT | W_BIT;
} else {
if (uid == vp->v_uid) shift = 6; /* owner */
else if (gid == vp->v_gid) shift = 3; /* group */
else if (in_group(fp, vp->v_gid) == OK) shift = 3; /* suppl. groups */
else shift = 0; /* other */
perm_bits = (bits >> shift) & (R_BIT | W_BIT | X_BIT);
}
/* If access desired is not a subset of what is allowed, it is refused. */
r = OK;
if ((perm_bits | access_desired) != perm_bits) r = EACCES;
/* Check to see if someone is trying to write on a file system that is
* mounted read-only.
*/
if (r == OK)
if (access_desired & W_BIT)
r = read_only(vp);
return(r);
}
/*===========================================================================*
* read_only *
*===========================================================================*/
int
read_only(
struct vnode *vp /* ptr to inode whose file sys is to be cked */
)
{
/* Check to see if the file system on which the inode 'ip' resides is mounted
* read only. If so, return EROFS, else return OK.
*/
assert(vp);
return(vp->v_vmnt && (vp->v_vmnt->m_flags & VMNT_READONLY) ? EROFS : OK);
}
|