xref: /OK3568_Linux_fs/kernel/security/tomoyo/mount.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * security/tomoyo/mount.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005-2011  NTT DATA CORPORATION
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include <uapi/linux/mount.h>
10*4882a593Smuzhiyun #include "common.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun /* String table for special mount operations. */
13*4882a593Smuzhiyun static const char * const tomoyo_mounts[TOMOYO_MAX_SPECIAL_MOUNT] = {
14*4882a593Smuzhiyun 	[TOMOYO_MOUNT_BIND]            = "--bind",
15*4882a593Smuzhiyun 	[TOMOYO_MOUNT_MOVE]            = "--move",
16*4882a593Smuzhiyun 	[TOMOYO_MOUNT_REMOUNT]         = "--remount",
17*4882a593Smuzhiyun 	[TOMOYO_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
18*4882a593Smuzhiyun 	[TOMOYO_MOUNT_MAKE_PRIVATE]    = "--make-private",
19*4882a593Smuzhiyun 	[TOMOYO_MOUNT_MAKE_SLAVE]      = "--make-slave",
20*4882a593Smuzhiyun 	[TOMOYO_MOUNT_MAKE_SHARED]     = "--make-shared",
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /**
24*4882a593Smuzhiyun  * tomoyo_audit_mount_log - Audit mount log.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * @r: Pointer to "struct tomoyo_request_info".
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Returns 0 on success, negative value otherwise.
29*4882a593Smuzhiyun  */
tomoyo_audit_mount_log(struct tomoyo_request_info * r)30*4882a593Smuzhiyun static int tomoyo_audit_mount_log(struct tomoyo_request_info *r)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	return tomoyo_supervisor(r, "file mount %s %s %s 0x%lX\n",
33*4882a593Smuzhiyun 				 r->param.mount.dev->name,
34*4882a593Smuzhiyun 				 r->param.mount.dir->name,
35*4882a593Smuzhiyun 				 r->param.mount.type->name,
36*4882a593Smuzhiyun 				 r->param.mount.flags);
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun  * tomoyo_check_mount_acl - Check permission for path path path number operation.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * @r:   Pointer to "struct tomoyo_request_info".
43*4882a593Smuzhiyun  * @ptr: Pointer to "struct tomoyo_acl_info".
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * Returns true if granted, false otherwise.
46*4882a593Smuzhiyun  */
tomoyo_check_mount_acl(struct tomoyo_request_info * r,const struct tomoyo_acl_info * ptr)47*4882a593Smuzhiyun static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
48*4882a593Smuzhiyun 				   const struct tomoyo_acl_info *ptr)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	const struct tomoyo_mount_acl *acl =
51*4882a593Smuzhiyun 		container_of(ptr, typeof(*acl), head);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	return tomoyo_compare_number_union(r->param.mount.flags,
54*4882a593Smuzhiyun 					   &acl->flags) &&
55*4882a593Smuzhiyun 		tomoyo_compare_name_union(r->param.mount.type,
56*4882a593Smuzhiyun 					  &acl->fs_type) &&
57*4882a593Smuzhiyun 		tomoyo_compare_name_union(r->param.mount.dir,
58*4882a593Smuzhiyun 					  &acl->dir_name) &&
59*4882a593Smuzhiyun 		(!r->param.mount.need_dev ||
60*4882a593Smuzhiyun 		 tomoyo_compare_name_union(r->param.mount.dev,
61*4882a593Smuzhiyun 					   &acl->dev_name));
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun  * tomoyo_mount_acl - Check permission for mount() operation.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * @r:        Pointer to "struct tomoyo_request_info".
68*4882a593Smuzhiyun  * @dev_name: Name of device file. Maybe NULL.
69*4882a593Smuzhiyun  * @dir:      Pointer to "struct path".
70*4882a593Smuzhiyun  * @type:     Name of filesystem type.
71*4882a593Smuzhiyun  * @flags:    Mount options.
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * Returns 0 on success, negative value otherwise.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * Caller holds tomoyo_read_lock().
76*4882a593Smuzhiyun  */
tomoyo_mount_acl(struct tomoyo_request_info * r,const char * dev_name,const struct path * dir,const char * type,unsigned long flags)77*4882a593Smuzhiyun static int tomoyo_mount_acl(struct tomoyo_request_info *r,
78*4882a593Smuzhiyun 			    const char *dev_name,
79*4882a593Smuzhiyun 			    const struct path *dir, const char *type,
80*4882a593Smuzhiyun 			    unsigned long flags)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	struct tomoyo_obj_info obj = { };
83*4882a593Smuzhiyun 	struct path path;
84*4882a593Smuzhiyun 	struct file_system_type *fstype = NULL;
85*4882a593Smuzhiyun 	const char *requested_type = NULL;
86*4882a593Smuzhiyun 	const char *requested_dir_name = NULL;
87*4882a593Smuzhiyun 	const char *requested_dev_name = NULL;
88*4882a593Smuzhiyun 	struct tomoyo_path_info rtype;
89*4882a593Smuzhiyun 	struct tomoyo_path_info rdev;
90*4882a593Smuzhiyun 	struct tomoyo_path_info rdir;
91*4882a593Smuzhiyun 	int need_dev = 0;
92*4882a593Smuzhiyun 	int error = -ENOMEM;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	r->obj = &obj;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	/* Get fstype. */
97*4882a593Smuzhiyun 	requested_type = tomoyo_encode(type);
98*4882a593Smuzhiyun 	if (!requested_type)
99*4882a593Smuzhiyun 		goto out;
100*4882a593Smuzhiyun 	rtype.name = requested_type;
101*4882a593Smuzhiyun 	tomoyo_fill_path_info(&rtype);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	/* Get mount point. */
104*4882a593Smuzhiyun 	obj.path2 = *dir;
105*4882a593Smuzhiyun 	requested_dir_name = tomoyo_realpath_from_path(dir);
106*4882a593Smuzhiyun 	if (!requested_dir_name) {
107*4882a593Smuzhiyun 		error = -ENOMEM;
108*4882a593Smuzhiyun 		goto out;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 	rdir.name = requested_dir_name;
111*4882a593Smuzhiyun 	tomoyo_fill_path_info(&rdir);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Compare fs name. */
114*4882a593Smuzhiyun 	if (type == tomoyo_mounts[TOMOYO_MOUNT_REMOUNT]) {
115*4882a593Smuzhiyun 		/* dev_name is ignored. */
116*4882a593Smuzhiyun 	} else if (type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE] ||
117*4882a593Smuzhiyun 		   type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE] ||
118*4882a593Smuzhiyun 		   type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE] ||
119*4882a593Smuzhiyun 		   type == tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED]) {
120*4882a593Smuzhiyun 		/* dev_name is ignored. */
121*4882a593Smuzhiyun 	} else if (type == tomoyo_mounts[TOMOYO_MOUNT_BIND] ||
122*4882a593Smuzhiyun 		   type == tomoyo_mounts[TOMOYO_MOUNT_MOVE]) {
123*4882a593Smuzhiyun 		need_dev = -1; /* dev_name is a directory */
124*4882a593Smuzhiyun 	} else {
125*4882a593Smuzhiyun 		fstype = get_fs_type(type);
126*4882a593Smuzhiyun 		if (!fstype) {
127*4882a593Smuzhiyun 			error = -ENODEV;
128*4882a593Smuzhiyun 			goto out;
129*4882a593Smuzhiyun 		}
130*4882a593Smuzhiyun 		if (fstype->fs_flags & FS_REQUIRES_DEV)
131*4882a593Smuzhiyun 			/* dev_name is a block device file. */
132*4882a593Smuzhiyun 			need_dev = 1;
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 	if (need_dev) {
135*4882a593Smuzhiyun 		/* Get mount point or device file. */
136*4882a593Smuzhiyun 		if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
137*4882a593Smuzhiyun 			error = -ENOENT;
138*4882a593Smuzhiyun 			goto out;
139*4882a593Smuzhiyun 		}
140*4882a593Smuzhiyun 		obj.path1 = path;
141*4882a593Smuzhiyun 		requested_dev_name = tomoyo_realpath_from_path(&path);
142*4882a593Smuzhiyun 		if (!requested_dev_name) {
143*4882a593Smuzhiyun 			error = -ENOENT;
144*4882a593Smuzhiyun 			goto out;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 	} else {
147*4882a593Smuzhiyun 		/* Map dev_name to "<NULL>" if no dev_name given. */
148*4882a593Smuzhiyun 		if (!dev_name)
149*4882a593Smuzhiyun 			dev_name = "<NULL>";
150*4882a593Smuzhiyun 		requested_dev_name = tomoyo_encode(dev_name);
151*4882a593Smuzhiyun 		if (!requested_dev_name) {
152*4882a593Smuzhiyun 			error = -ENOMEM;
153*4882a593Smuzhiyun 			goto out;
154*4882a593Smuzhiyun 		}
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 	rdev.name = requested_dev_name;
157*4882a593Smuzhiyun 	tomoyo_fill_path_info(&rdev);
158*4882a593Smuzhiyun 	r->param_type = TOMOYO_TYPE_MOUNT_ACL;
159*4882a593Smuzhiyun 	r->param.mount.need_dev = need_dev;
160*4882a593Smuzhiyun 	r->param.mount.dev = &rdev;
161*4882a593Smuzhiyun 	r->param.mount.dir = &rdir;
162*4882a593Smuzhiyun 	r->param.mount.type = &rtype;
163*4882a593Smuzhiyun 	r->param.mount.flags = flags;
164*4882a593Smuzhiyun 	do {
165*4882a593Smuzhiyun 		tomoyo_check_acl(r, tomoyo_check_mount_acl);
166*4882a593Smuzhiyun 		error = tomoyo_audit_mount_log(r);
167*4882a593Smuzhiyun 	} while (error == TOMOYO_RETRY_REQUEST);
168*4882a593Smuzhiyun  out:
169*4882a593Smuzhiyun 	kfree(requested_dev_name);
170*4882a593Smuzhiyun 	kfree(requested_dir_name);
171*4882a593Smuzhiyun 	if (fstype)
172*4882a593Smuzhiyun 		put_filesystem(fstype);
173*4882a593Smuzhiyun 	kfree(requested_type);
174*4882a593Smuzhiyun 	/* Drop refcount obtained by kern_path(). */
175*4882a593Smuzhiyun 	if (obj.path1.dentry)
176*4882a593Smuzhiyun 		path_put(&obj.path1);
177*4882a593Smuzhiyun 	return error;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun  * tomoyo_mount_permission - Check permission for mount() operation.
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * @dev_name:  Name of device file. Maybe NULL.
184*4882a593Smuzhiyun  * @path:      Pointer to "struct path".
185*4882a593Smuzhiyun  * @type:      Name of filesystem type. Maybe NULL.
186*4882a593Smuzhiyun  * @flags:     Mount options.
187*4882a593Smuzhiyun  * @data_page: Optional data. Maybe NULL.
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * Returns 0 on success, negative value otherwise.
190*4882a593Smuzhiyun  */
tomoyo_mount_permission(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data_page)191*4882a593Smuzhiyun int tomoyo_mount_permission(const char *dev_name, const struct path *path,
192*4882a593Smuzhiyun 			    const char *type, unsigned long flags,
193*4882a593Smuzhiyun 			    void *data_page)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct tomoyo_request_info r;
196*4882a593Smuzhiyun 	int error;
197*4882a593Smuzhiyun 	int idx;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	if (tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_MOUNT)
200*4882a593Smuzhiyun 	    == TOMOYO_CONFIG_DISABLED)
201*4882a593Smuzhiyun 		return 0;
202*4882a593Smuzhiyun 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
203*4882a593Smuzhiyun 		flags &= ~MS_MGC_MSK;
204*4882a593Smuzhiyun 	if (flags & MS_REMOUNT) {
205*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_REMOUNT];
206*4882a593Smuzhiyun 		flags &= ~MS_REMOUNT;
207*4882a593Smuzhiyun 	} else if (flags & MS_BIND) {
208*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_BIND];
209*4882a593Smuzhiyun 		flags &= ~MS_BIND;
210*4882a593Smuzhiyun 	} else if (flags & MS_SHARED) {
211*4882a593Smuzhiyun 		if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
212*4882a593Smuzhiyun 			return -EINVAL;
213*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SHARED];
214*4882a593Smuzhiyun 		flags &= ~MS_SHARED;
215*4882a593Smuzhiyun 	} else if (flags & MS_PRIVATE) {
216*4882a593Smuzhiyun 		if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
217*4882a593Smuzhiyun 			return -EINVAL;
218*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_PRIVATE];
219*4882a593Smuzhiyun 		flags &= ~MS_PRIVATE;
220*4882a593Smuzhiyun 	} else if (flags & MS_SLAVE) {
221*4882a593Smuzhiyun 		if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
222*4882a593Smuzhiyun 			return -EINVAL;
223*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_SLAVE];
224*4882a593Smuzhiyun 		flags &= ~MS_SLAVE;
225*4882a593Smuzhiyun 	} else if (flags & MS_UNBINDABLE) {
226*4882a593Smuzhiyun 		if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
227*4882a593Smuzhiyun 			return -EINVAL;
228*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_MAKE_UNBINDABLE];
229*4882a593Smuzhiyun 		flags &= ~MS_UNBINDABLE;
230*4882a593Smuzhiyun 	} else if (flags & MS_MOVE) {
231*4882a593Smuzhiyun 		type = tomoyo_mounts[TOMOYO_MOUNT_MOVE];
232*4882a593Smuzhiyun 		flags &= ~MS_MOVE;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 	if (!type)
235*4882a593Smuzhiyun 		type = "<NULL>";
236*4882a593Smuzhiyun 	idx = tomoyo_read_lock();
237*4882a593Smuzhiyun 	error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
238*4882a593Smuzhiyun 	tomoyo_read_unlock(idx);
239*4882a593Smuzhiyun 	return error;
240*4882a593Smuzhiyun }
241