xref: /dflybsd-src/sys/kern/vfs_helper.c (revision cabeba4772bc70c9d55f7e8ef07c932d5a30e411)
1 /*
2  * (The copyright below applies to ufs_access())
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * @(#)ufs_vnops.c	8.27 (Berkeley) 5/27/95
41  * $DragonFly: src/sys/kern/vfs_helper.c,v 1.3 2007/11/20 21:37:54 dillon Exp $
42  */
43 
44 #include "opt_quota.h"
45 #include "opt_suiddir.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/fcntl.h>
51 #include <sys/stat.h>
52 #include <sys/mount.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/file.h>		/* XXX */
56 #include <sys/jail.h>
57 
58 /*
59  * vop_helper_access()
60  *
61  *	Provide standard UNIX semanics for VOP_ACCESS, but without the quota
62  *	code.  This procedure was basically pulled out of UFS.
63  */
64 int
65 vop_helper_access(struct vop_access_args *ap, uid_t ino_uid, gid_t ino_gid,
66 		  mode_t ino_mode, u_int32_t ino_flags)
67 {
68 	struct vnode *vp = ap->a_vp;
69 	struct ucred *cred = ap->a_cred;
70 	mode_t mask, mode = ap->a_mode;
71 	gid_t *gp;
72 	int i;
73 #ifdef QUOTA
74 	/*int error;*/
75 #endif
76 
77 	/*
78 	 * Disallow write attempts on read-only filesystems;
79 	 * unless the file is a socket, fifo, or a block or
80 	 * character device resident on the filesystem.
81 	 */
82 	if (mode & VWRITE) {
83 		switch (vp->v_type) {
84 		case VDIR:
85 		case VLNK:
86 		case VREG:
87 		case VDATABASE:
88 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
89 				return (EROFS);
90 #ifdef QUOTA
91 			/* check quota here XXX */
92 #endif
93 			break;
94 		default:
95 			break;
96 		}
97 	}
98 
99 	/* If immutable bit set, nobody gets to write it. */
100 	if ((mode & VWRITE) && (ino_flags & IMMUTABLE))
101 		return (EPERM);
102 
103 	/* Otherwise, user id 0 always gets access. */
104 	if (cred->cr_uid == 0)
105 		return (0);
106 
107 	mask = 0;
108 
109 	/* Otherwise, check the owner. */
110 	if (cred->cr_uid == ino_uid) {
111 		if (mode & VEXEC)
112 			mask |= S_IXUSR;
113 		if (mode & VREAD)
114 			mask |= S_IRUSR;
115 		if (mode & VWRITE)
116 			mask |= S_IWUSR;
117 		return ((ino_mode & mask) == mask ? 0 : EACCES);
118 	}
119 
120 	/* Otherwise, check the groups. */
121 	for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
122 		if (ino_gid == *gp) {
123 			if (mode & VEXEC)
124 				mask |= S_IXGRP;
125 			if (mode & VREAD)
126 				mask |= S_IRGRP;
127 			if (mode & VWRITE)
128 				mask |= S_IWGRP;
129 			return ((ino_mode & mask) == mask ? 0 : EACCES);
130 		}
131 
132 	/* Otherwise, check everyone else. */
133 	if (mode & VEXEC)
134 		mask |= S_IXOTH;
135 	if (mode & VREAD)
136 		mask |= S_IROTH;
137 	if (mode & VWRITE)
138 		mask |= S_IWOTH;
139 	return ((ino_mode & mask) == mask ? 0 : EACCES);
140 }
141 
142 int
143 vop_helper_setattr_flags(u_int32_t *ino_flags, u_int32_t vaflags,
144 			 uid_t uid, struct ucred *cred)
145 {
146 	int error;
147 
148 	/*
149 	 * If uid doesn't match only the super-user can change the flags
150 	 */
151 	if (cred->cr_uid != uid &&
152 	    (error = suser_cred(cred, PRISON_ROOT))) {
153 		return(error);
154 	}
155 	if (cred->cr_uid == 0 &&
156 	    (!jailed(cred)|| jail_chflags_allowed)) {
157 		if ((*ino_flags & (SF_NOUNLINK|SF_IMMUTABLE|SF_APPEND)) &&
158 		    securelevel > 0)
159 			return (EPERM);
160 		*ino_flags = vaflags;
161 	} else {
162 		if (*ino_flags & (SF_NOUNLINK|SF_IMMUTABLE|SF_APPEND) ||
163 		    (vaflags & UF_SETTABLE) != vaflags)
164 			return (EPERM);
165 		*ino_flags &= SF_SETTABLE;
166 		*ino_flags |= vaflags & UF_SETTABLE;
167 	}
168 	return(0);
169 }
170 
171 uid_t
172 vop_helper_create_uid(struct mount *mp, mode_t dmode, uid_t duid,
173 		      struct ucred *cred, mode_t *modep)
174 {
175 #ifdef SUIDDIR
176 	if ((mount->mnt_flag & MNT_SUIDDIR) && (dmode & ISUID) &&
177 	    duid != cred->cr_uid && duid) {
178 		*modep &= ~07111;
179 		return(duid);
180 	}
181 #endif
182 	return(cred->cr_uid);
183 }
184 
185