xref: /openbmc/linux/security/tomoyo/tomoyo.c (revision 820831de)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f7433243SKentaro Takeda /*
3f7433243SKentaro Takeda  * security/tomoyo/tomoyo.c
4f7433243SKentaro Takeda  *
50f2a55d5STetsuo Handa  * Copyright (C) 2005-2011  NTT DATA CORPORATION
6f7433243SKentaro Takeda  */
7f7433243SKentaro Takeda 
83c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
9f7433243SKentaro Takeda #include "common.h"
10f7433243SKentaro Takeda 
110f2a55d5STetsuo Handa /**
128c6cb983STetsuo Handa  * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
130f2a55d5STetsuo Handa  *
148c6cb983STetsuo Handa  * Returns pointer to "struct tomoyo_domain_info" for current thread.
150f2a55d5STetsuo Handa  */
tomoyo_domain(void)168c6cb983STetsuo Handa struct tomoyo_domain_info *tomoyo_domain(void)
17ee18d64cSDavid Howells {
188c6cb983STetsuo Handa 	struct tomoyo_task *s = tomoyo_task(current);
1943fc4609SCasey Schaufler 
208c6cb983STetsuo Handa 	if (s->old_domain_info && !current->in_execve) {
218c6cb983STetsuo Handa 		atomic_dec(&s->old_domain_info->users);
228c6cb983STetsuo Handa 		s->old_domain_info = NULL;
238c6cb983STetsuo Handa 	}
248c6cb983STetsuo Handa 	return s->domain_info;
25ee18d64cSDavid Howells }
26ee18d64cSDavid Howells 
270f2a55d5STetsuo Handa /**
280f2a55d5STetsuo Handa  * tomoyo_cred_prepare - Target for security_prepare_creds().
290f2a55d5STetsuo Handa  *
300f2a55d5STetsuo Handa  * @new: Pointer to "struct cred".
310f2a55d5STetsuo Handa  * @old: Pointer to "struct cred".
320f2a55d5STetsuo Handa  * @gfp: Memory allocation flags.
330f2a55d5STetsuo Handa  *
340f2a55d5STetsuo Handa  * Returns 0.
350f2a55d5STetsuo Handa  */
tomoyo_cred_prepare(struct cred * new,const struct cred * old,gfp_t gfp)36f7433243SKentaro Takeda static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
37f7433243SKentaro Takeda 			       gfp_t gfp)
38f7433243SKentaro Takeda {
398c6cb983STetsuo Handa 	/* Restore old_domain_info saved by previous execve() request. */
408c6cb983STetsuo Handa 	struct tomoyo_task *s = tomoyo_task(current);
4143fc4609SCasey Schaufler 
428c6cb983STetsuo Handa 	if (s->old_domain_info && !current->in_execve) {
438c6cb983STetsuo Handa 		atomic_dec(&s->domain_info->users);
448c6cb983STetsuo Handa 		s->domain_info = s->old_domain_info;
458c6cb983STetsuo Handa 		s->old_domain_info = NULL;
468c6cb983STetsuo Handa 	}
47f7433243SKentaro Takeda 	return 0;
48f7433243SKentaro Takeda }
49f7433243SKentaro Takeda 
500f2a55d5STetsuo Handa /**
518c6cb983STetsuo Handa  * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds().
520f2a55d5STetsuo Handa  *
538c6cb983STetsuo Handa  * @bprm: Pointer to "struct linux_binprm".
540f2a55d5STetsuo Handa  */
tomoyo_bprm_committed_creds(struct linux_binprm * bprm)558c6cb983STetsuo Handa static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm)
56ee18d64cSDavid Howells {
578c6cb983STetsuo Handa 	/* Clear old_domain_info saved by execve() request. */
588c6cb983STetsuo Handa 	struct tomoyo_task *s = tomoyo_task(current);
598c6cb983STetsuo Handa 
608c6cb983STetsuo Handa 	atomic_dec(&s->old_domain_info->users);
618c6cb983STetsuo Handa 	s->old_domain_info = NULL;
62ec8e6a4eSTetsuo Handa }
63ec8e6a4eSTetsuo Handa 
648c6cb983STetsuo Handa #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
650f2a55d5STetsuo Handa /**
6698eaa63eSChenXiaoSong  * tomoyo_bprm_creds_for_exec - Target for security_bprm_creds_for_exec().
670f2a55d5STetsuo Handa  *
680f2a55d5STetsuo Handa  * @bprm: Pointer to "struct linux_binprm".
690f2a55d5STetsuo Handa  *
708c6cb983STetsuo Handa  * Returns 0.
710f2a55d5STetsuo Handa  */
tomoyo_bprm_creds_for_exec(struct linux_binprm * bprm)72b8bff599SEric W. Biederman static int tomoyo_bprm_creds_for_exec(struct linux_binprm *bprm)
73f7433243SKentaro Takeda {
74f7433243SKentaro Takeda 	/*
75f7433243SKentaro Takeda 	 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
76f7433243SKentaro Takeda 	 * for the first time.
77f7433243SKentaro Takeda 	 */
78f7433243SKentaro Takeda 	if (!tomoyo_policy_loaded)
79f7433243SKentaro Takeda 		tomoyo_load_policy(bprm->filename);
80f7433243SKentaro Takeda 	return 0;
81f7433243SKentaro Takeda }
828c6cb983STetsuo Handa #endif
83f7433243SKentaro Takeda 
840f2a55d5STetsuo Handa /**
850f2a55d5STetsuo Handa  * tomoyo_bprm_check_security - Target for security_bprm_check().
860f2a55d5STetsuo Handa  *
870f2a55d5STetsuo Handa  * @bprm: Pointer to "struct linux_binprm".
880f2a55d5STetsuo Handa  *
890f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
900f2a55d5STetsuo Handa  */
tomoyo_bprm_check_security(struct linux_binprm * bprm)91f7433243SKentaro Takeda static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
92f7433243SKentaro Takeda {
938c6cb983STetsuo Handa 	struct tomoyo_task *s = tomoyo_task(current);
94f7433243SKentaro Takeda 
95f7433243SKentaro Takeda 	/*
96be619f7fSEric W. Biederman 	 * Execute permission is checked against pathname passed to execve()
97f7433243SKentaro Takeda 	 * using current domain.
98f7433243SKentaro Takeda 	 */
998c6cb983STetsuo Handa 	if (!s->old_domain_info) {
100fdb8ebb7STetsuo Handa 		const int idx = tomoyo_read_lock();
101fdb8ebb7STetsuo Handa 		const int err = tomoyo_find_next_domain(bprm);
102cdcf6723STetsuo Handa 
103fdb8ebb7STetsuo Handa 		tomoyo_read_unlock(idx);
104fdb8ebb7STetsuo Handa 		return err;
105fdb8ebb7STetsuo Handa 	}
106f7433243SKentaro Takeda 	/*
107f7433243SKentaro Takeda 	 * Read permission is checked against interpreters using next domain.
108f7433243SKentaro Takeda 	 */
1098c6cb983STetsuo Handa 	return tomoyo_check_open_permission(s->domain_info,
1108c6cb983STetsuo Handa 					    &bprm->file->f_path, O_RDONLY);
111f7433243SKentaro Takeda }
112f7433243SKentaro Takeda 
1130f2a55d5STetsuo Handa /**
1140f2a55d5STetsuo Handa  * tomoyo_inode_getattr - Target for security_inode_getattr().
1150f2a55d5STetsuo Handa  *
11698eaa63eSChenXiaoSong  * @path: Pointer to "struct path".
1170f2a55d5STetsuo Handa  *
1180f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
1190f2a55d5STetsuo Handa  */
tomoyo_inode_getattr(const struct path * path)1203f7036a0SAl Viro static int tomoyo_inode_getattr(const struct path *path)
1217c75964fSTetsuo Handa {
1223f7036a0SAl Viro 	return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
1237c75964fSTetsuo Handa }
1247c75964fSTetsuo Handa 
1250f2a55d5STetsuo Handa /**
1260f2a55d5STetsuo Handa  * tomoyo_path_truncate - Target for security_path_truncate().
1270f2a55d5STetsuo Handa  *
1280f2a55d5STetsuo Handa  * @path: Pointer to "struct path".
1290f2a55d5STetsuo Handa  *
1300f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
1310f2a55d5STetsuo Handa  */
tomoyo_path_truncate(const struct path * path)13281f4c506SAl Viro static int tomoyo_path_truncate(const struct path *path)
133f7433243SKentaro Takeda {
13497fb35e4STetsuo Handa 	return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
135f7433243SKentaro Takeda }
136f7433243SKentaro Takeda 
1370f2a55d5STetsuo Handa /**
1383350607dSGünther Noack  * tomoyo_file_truncate - Target for security_file_truncate().
1393350607dSGünther Noack  *
1403350607dSGünther Noack  * @file: Pointer to "struct file".
1413350607dSGünther Noack  *
1423350607dSGünther Noack  * Returns 0 on success, negative value otherwise.
1433350607dSGünther Noack  */
tomoyo_file_truncate(struct file * file)1443350607dSGünther Noack static int tomoyo_file_truncate(struct file *file)
1453350607dSGünther Noack {
1463350607dSGünther Noack 	return tomoyo_path_truncate(&file->f_path);
1473350607dSGünther Noack }
1483350607dSGünther Noack 
1493350607dSGünther Noack /**
1500f2a55d5STetsuo Handa  * tomoyo_path_unlink - Target for security_path_unlink().
1510f2a55d5STetsuo Handa  *
1520f2a55d5STetsuo Handa  * @parent: Pointer to "struct path".
1530f2a55d5STetsuo Handa  * @dentry: Pointer to "struct dentry".
1540f2a55d5STetsuo Handa  *
1550f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
1560f2a55d5STetsuo Handa  */
tomoyo_path_unlink(const struct path * parent,struct dentry * dentry)157989f74e0SAl Viro static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
158f7433243SKentaro Takeda {
1598291798dSKees Cook 	struct path path = { .mnt = parent->mnt, .dentry = dentry };
160cdcf6723STetsuo Handa 
16197fb35e4STetsuo Handa 	return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
162f7433243SKentaro Takeda }
163f7433243SKentaro Takeda 
1640f2a55d5STetsuo Handa /**
1650f2a55d5STetsuo Handa  * tomoyo_path_mkdir - Target for security_path_mkdir().
1660f2a55d5STetsuo Handa  *
1670f2a55d5STetsuo Handa  * @parent: Pointer to "struct path".
1680f2a55d5STetsuo Handa  * @dentry: Pointer to "struct dentry".
1690f2a55d5STetsuo Handa  * @mode:   DAC permission mode.
1700f2a55d5STetsuo Handa  *
1710f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
1720f2a55d5STetsuo Handa  */
tomoyo_path_mkdir(const struct path * parent,struct dentry * dentry,umode_t mode)173d3607752SAl Viro static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
1744572befeSAl Viro 			     umode_t mode)
175f7433243SKentaro Takeda {
1768291798dSKees Cook 	struct path path = { .mnt = parent->mnt, .dentry = dentry };
177cdcf6723STetsuo Handa 
178a1f9bb6aSTetsuo Handa 	return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
179a1f9bb6aSTetsuo Handa 				       mode & S_IALLUGO);
180f7433243SKentaro Takeda }
181f7433243SKentaro Takeda 
1820f2a55d5STetsuo Handa /**
1830f2a55d5STetsuo Handa  * tomoyo_path_rmdir - Target for security_path_rmdir().
1840f2a55d5STetsuo Handa  *
1850f2a55d5STetsuo Handa  * @parent: Pointer to "struct path".
1860f2a55d5STetsuo Handa  * @dentry: Pointer to "struct dentry".
1870f2a55d5STetsuo Handa  *
1880f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
1890f2a55d5STetsuo Handa  */
tomoyo_path_rmdir(const struct path * parent,struct dentry * dentry)190989f74e0SAl Viro static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
191f7433243SKentaro Takeda {
1928291798dSKees Cook 	struct path path = { .mnt = parent->mnt, .dentry = dentry };
193cdcf6723STetsuo Handa 
19497fb35e4STetsuo Handa 	return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
195f7433243SKentaro Takeda }
196f7433243SKentaro Takeda 
1970f2a55d5STetsuo Handa /**
1980f2a55d5STetsuo Handa  * tomoyo_path_symlink - Target for security_path_symlink().
1990f2a55d5STetsuo Handa  *
2000f2a55d5STetsuo Handa  * @parent:   Pointer to "struct path".
2010f2a55d5STetsuo Handa  * @dentry:   Pointer to "struct dentry".
2020f2a55d5STetsuo Handa  * @old_name: Symlink's content.
2030f2a55d5STetsuo Handa  *
2040f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
2050f2a55d5STetsuo Handa  */
tomoyo_path_symlink(const struct path * parent,struct dentry * dentry,const char * old_name)206d3607752SAl Viro static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
207f7433243SKentaro Takeda 			       const char *old_name)
208f7433243SKentaro Takeda {
2098291798dSKees Cook 	struct path path = { .mnt = parent->mnt, .dentry = dentry };
210cdcf6723STetsuo Handa 
21197fb35e4STetsuo Handa 	return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
212f7433243SKentaro Takeda }
213f7433243SKentaro Takeda 
2140f2a55d5STetsuo Handa /**
2150f2a55d5STetsuo Handa  * tomoyo_path_mknod - Target for security_path_mknod().
2160f2a55d5STetsuo Handa  *
2170f2a55d5STetsuo Handa  * @parent: Pointer to "struct path".
2180f2a55d5STetsuo Handa  * @dentry: Pointer to "struct dentry".
2190f2a55d5STetsuo Handa  * @mode:   DAC permission mode.
2200f2a55d5STetsuo Handa  * @dev:    Device attributes.
2210f2a55d5STetsuo Handa  *
2220f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
2230f2a55d5STetsuo Handa  */
tomoyo_path_mknod(const struct path * parent,struct dentry * dentry,umode_t mode,unsigned int dev)224d3607752SAl Viro static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
22504fc66e7SAl Viro 			     umode_t mode, unsigned int dev)
226f7433243SKentaro Takeda {
2278291798dSKees Cook 	struct path path = { .mnt = parent->mnt, .dentry = dentry };
2287ef61233STetsuo Handa 	int type = TOMOYO_TYPE_CREATE;
229a1f9bb6aSTetsuo Handa 	const unsigned int perm = mode & S_IALLUGO;
230f7433243SKentaro Takeda 
231f7433243SKentaro Takeda 	switch (mode & S_IFMT) {
232f7433243SKentaro Takeda 	case S_IFCHR:
2337ef61233STetsuo Handa 		type = TOMOYO_TYPE_MKCHAR;
234f7433243SKentaro Takeda 		break;
235f7433243SKentaro Takeda 	case S_IFBLK:
2367ef61233STetsuo Handa 		type = TOMOYO_TYPE_MKBLOCK;
237f7433243SKentaro Takeda 		break;
238a1f9bb6aSTetsuo Handa 	default:
239a1f9bb6aSTetsuo Handa 		goto no_dev;
240a1f9bb6aSTetsuo Handa 	}
24175093152STetsuo Handa 	return tomoyo_mkdev_perm(type, &path, perm, dev);
242a1f9bb6aSTetsuo Handa  no_dev:
243a1f9bb6aSTetsuo Handa 	switch (mode & S_IFMT) {
244f7433243SKentaro Takeda 	case S_IFIFO:
2457ef61233STetsuo Handa 		type = TOMOYO_TYPE_MKFIFO;
246f7433243SKentaro Takeda 		break;
247f7433243SKentaro Takeda 	case S_IFSOCK:
2487ef61233STetsuo Handa 		type = TOMOYO_TYPE_MKSOCK;
249f7433243SKentaro Takeda 		break;
250f7433243SKentaro Takeda 	}
251a1f9bb6aSTetsuo Handa 	return tomoyo_path_number_perm(type, &path, perm);
252f7433243SKentaro Takeda }
253f7433243SKentaro Takeda 
2540f2a55d5STetsuo Handa /**
2550f2a55d5STetsuo Handa  * tomoyo_path_link - Target for security_path_link().
2560f2a55d5STetsuo Handa  *
2570f2a55d5STetsuo Handa  * @old_dentry: Pointer to "struct dentry".
2580f2a55d5STetsuo Handa  * @new_dir:    Pointer to "struct path".
2590f2a55d5STetsuo Handa  * @new_dentry: Pointer to "struct dentry".
2600f2a55d5STetsuo Handa  *
2610f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
2620f2a55d5STetsuo Handa  */
tomoyo_path_link(struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry)2633ccee46aSAl Viro static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
264f7433243SKentaro Takeda 			    struct dentry *new_dentry)
265f7433243SKentaro Takeda {
2668291798dSKees Cook 	struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
2678291798dSKees Cook 	struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
268cdcf6723STetsuo Handa 
26997d6931eSTetsuo Handa 	return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
270f7433243SKentaro Takeda }
271f7433243SKentaro Takeda 
2720f2a55d5STetsuo Handa /**
2730f2a55d5STetsuo Handa  * tomoyo_path_rename - Target for security_path_rename().
2740f2a55d5STetsuo Handa  *
2750f2a55d5STetsuo Handa  * @old_parent: Pointer to "struct path".
2760f2a55d5STetsuo Handa  * @old_dentry: Pointer to "struct dentry".
2770f2a55d5STetsuo Handa  * @new_parent: Pointer to "struct path".
2780f2a55d5STetsuo Handa  * @new_dentry: Pointer to "struct dentry".
279100f59d9SMickaël Salaün  * @flags: Rename options.
2800f2a55d5STetsuo Handa  *
2810f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
2820f2a55d5STetsuo Handa  */
tomoyo_path_rename(const struct path * old_parent,struct dentry * old_dentry,const struct path * new_parent,struct dentry * new_dentry,const unsigned int flags)2833ccee46aSAl Viro static int tomoyo_path_rename(const struct path *old_parent,
284f7433243SKentaro Takeda 			      struct dentry *old_dentry,
2853ccee46aSAl Viro 			      const struct path *new_parent,
286100f59d9SMickaël Salaün 			      struct dentry *new_dentry,
287100f59d9SMickaël Salaün 			      const unsigned int flags)
288f7433243SKentaro Takeda {
2898291798dSKees Cook 	struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
2908291798dSKees Cook 	struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
291cdcf6723STetsuo Handa 
292100f59d9SMickaël Salaün 	if (flags & RENAME_EXCHANGE) {
293100f59d9SMickaël Salaün 		const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2,
294100f59d9SMickaël Salaün 				&path1);
295100f59d9SMickaël Salaün 
296100f59d9SMickaël Salaün 		if (err)
297100f59d9SMickaël Salaün 			return err;
298100f59d9SMickaël Salaün 	}
29997d6931eSTetsuo Handa 	return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
300f7433243SKentaro Takeda }
301f7433243SKentaro Takeda 
3020f2a55d5STetsuo Handa /**
3030f2a55d5STetsuo Handa  * tomoyo_file_fcntl - Target for security_file_fcntl().
3040f2a55d5STetsuo Handa  *
3050f2a55d5STetsuo Handa  * @file: Pointer to "struct file".
3060f2a55d5STetsuo Handa  * @cmd:  Command for fcntl().
3070f2a55d5STetsuo Handa  * @arg:  Argument for @cmd.
3080f2a55d5STetsuo Handa  *
3090f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
3100f2a55d5STetsuo Handa  */
tomoyo_file_fcntl(struct file * file,unsigned int cmd,unsigned long arg)311f7433243SKentaro Takeda static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
312f7433243SKentaro Takeda 			     unsigned long arg)
313f7433243SKentaro Takeda {
3147c75964fSTetsuo Handa 	if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
315f7433243SKentaro Takeda 		return 0;
3167c75964fSTetsuo Handa 	return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
3177c75964fSTetsuo Handa 					    O_WRONLY | (arg & O_APPEND));
318f7433243SKentaro Takeda }
319f7433243SKentaro Takeda 
3200f2a55d5STetsuo Handa /**
32183d49856SEric Paris  * tomoyo_file_open - Target for security_file_open().
3220f2a55d5STetsuo Handa  *
3230f2a55d5STetsuo Handa  * @f: Pointer to "struct file".
3240f2a55d5STetsuo Handa  *
3250f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
3260f2a55d5STetsuo Handa  */
tomoyo_file_open(struct file * f)32794817692SAl Viro static int tomoyo_file_open(struct file *f)
328f7433243SKentaro Takeda {
329be619f7fSEric W. Biederman 	/* Don't check read permission here if called from execve(). */
330f7433243SKentaro Takeda 	if (current->in_execve)
331f7433243SKentaro Takeda 		return 0;
332cdcf6723STetsuo Handa 	return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path,
333cdcf6723STetsuo Handa 					    f->f_flags);
334f7433243SKentaro Takeda }
335f7433243SKentaro Takeda 
3360f2a55d5STetsuo Handa /**
3370f2a55d5STetsuo Handa  * tomoyo_file_ioctl - Target for security_file_ioctl().
3380f2a55d5STetsuo Handa  *
3390f2a55d5STetsuo Handa  * @file: Pointer to "struct file".
3400f2a55d5STetsuo Handa  * @cmd:  Command for ioctl().
3410f2a55d5STetsuo Handa  * @arg:  Argument for @cmd.
3420f2a55d5STetsuo Handa  *
3430f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
3440f2a55d5STetsuo Handa  */
tomoyo_file_ioctl(struct file * file,unsigned int cmd,unsigned long arg)345937bf613STetsuo Handa static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
346937bf613STetsuo Handa 			     unsigned long arg)
347937bf613STetsuo Handa {
348a1f9bb6aSTetsuo Handa 	return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
349937bf613STetsuo Handa }
350937bf613STetsuo Handa 
3510f2a55d5STetsuo Handa /**
3520f2a55d5STetsuo Handa  * tomoyo_path_chmod - Target for security_path_chmod().
3530f2a55d5STetsuo Handa  *
354cdcf116dSAl Viro  * @path: Pointer to "struct path".
3550f2a55d5STetsuo Handa  * @mode: DAC permission mode.
3560f2a55d5STetsuo Handa  *
3570f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
3580f2a55d5STetsuo Handa  */
tomoyo_path_chmod(const struct path * path,umode_t mode)359be01f9f2SAl Viro static int tomoyo_path_chmod(const struct path *path, umode_t mode)
360937bf613STetsuo Handa {
361cdcf116dSAl Viro 	return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
362a1f9bb6aSTetsuo Handa 				       mode & S_IALLUGO);
363937bf613STetsuo Handa }
364937bf613STetsuo Handa 
3650f2a55d5STetsuo Handa /**
3660f2a55d5STetsuo Handa  * tomoyo_path_chown - Target for security_path_chown().
3670f2a55d5STetsuo Handa  *
3680f2a55d5STetsuo Handa  * @path: Pointer to "struct path".
3690f2a55d5STetsuo Handa  * @uid:  Owner ID.
3700f2a55d5STetsuo Handa  * @gid:  Group ID.
3710f2a55d5STetsuo Handa  *
3720f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
3730f2a55d5STetsuo Handa  */
tomoyo_path_chown(const struct path * path,kuid_t uid,kgid_t gid)3747fd25dacSAl Viro static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
375937bf613STetsuo Handa {
376937bf613STetsuo Handa 	int error = 0;
377cdcf6723STetsuo Handa 
378d2b31ca6SEric W. Biederman 	if (uid_valid(uid))
379d2b31ca6SEric W. Biederman 		error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
380d2b31ca6SEric W. Biederman 						from_kuid(&init_user_ns, uid));
381d2b31ca6SEric W. Biederman 	if (!error && gid_valid(gid))
382d2b31ca6SEric W. Biederman 		error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
383d2b31ca6SEric W. Biederman 						from_kgid(&init_user_ns, gid));
384937bf613STetsuo Handa 	return error;
385937bf613STetsuo Handa }
386937bf613STetsuo Handa 
3870f2a55d5STetsuo Handa /**
3880f2a55d5STetsuo Handa  * tomoyo_path_chroot - Target for security_path_chroot().
3890f2a55d5STetsuo Handa  *
3900f2a55d5STetsuo Handa  * @path: Pointer to "struct path".
3910f2a55d5STetsuo Handa  *
3920f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
3930f2a55d5STetsuo Handa  */
tomoyo_path_chroot(const struct path * path)39477b286c0SAl Viro static int tomoyo_path_chroot(const struct path *path)
395937bf613STetsuo Handa {
39697fb35e4STetsuo Handa 	return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
397937bf613STetsuo Handa }
398937bf613STetsuo Handa 
3990f2a55d5STetsuo Handa /**
4000f2a55d5STetsuo Handa  * tomoyo_sb_mount - Target for security_sb_mount().
4010f2a55d5STetsuo Handa  *
4020f2a55d5STetsuo Handa  * @dev_name: Name of device file. Maybe NULL.
4030f2a55d5STetsuo Handa  * @path:     Pointer to "struct path".
4040f2a55d5STetsuo Handa  * @type:     Name of filesystem type. Maybe NULL.
4050f2a55d5STetsuo Handa  * @flags:    Mount options.
4060f2a55d5STetsuo Handa  * @data:     Optional data. Maybe NULL.
4070f2a55d5STetsuo Handa  *
4080f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
4090f2a55d5STetsuo Handa  */
tomoyo_sb_mount(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data)4108a04c43bSAl Viro static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
411808d4e3cSAl Viro 			   const char *type, unsigned long flags, void *data)
412937bf613STetsuo Handa {
4132106ccd9STetsuo Handa 	return tomoyo_mount_permission(dev_name, path, type, flags, data);
414937bf613STetsuo Handa }
415937bf613STetsuo Handa 
4160f2a55d5STetsuo Handa /**
4170f2a55d5STetsuo Handa  * tomoyo_sb_umount - Target for security_sb_umount().
4180f2a55d5STetsuo Handa  *
4190f2a55d5STetsuo Handa  * @mnt:   Pointer to "struct vfsmount".
4200f2a55d5STetsuo Handa  * @flags: Unmount options.
4210f2a55d5STetsuo Handa  *
4220f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
4230f2a55d5STetsuo Handa  */
tomoyo_sb_umount(struct vfsmount * mnt,int flags)424937bf613STetsuo Handa static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
425937bf613STetsuo Handa {
4268291798dSKees Cook 	struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
427cdcf6723STetsuo Handa 
42897fb35e4STetsuo Handa 	return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
429937bf613STetsuo Handa }
430937bf613STetsuo Handa 
4310f2a55d5STetsuo Handa /**
4320f2a55d5STetsuo Handa  * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
4330f2a55d5STetsuo Handa  *
4340f2a55d5STetsuo Handa  * @old_path: Pointer to "struct path".
4350f2a55d5STetsuo Handa  * @new_path: Pointer to "struct path".
4360f2a55d5STetsuo Handa  *
4370f2a55d5STetsuo Handa  * Returns 0 on success, negative value otherwise.
4380f2a55d5STetsuo Handa  */
tomoyo_sb_pivotroot(const struct path * old_path,const struct path * new_path)4393b73b68cSAl Viro static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
440937bf613STetsuo Handa {
44197d6931eSTetsuo Handa 	return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
442937bf613STetsuo Handa }
443937bf613STetsuo Handa 
444059d84dbSTetsuo Handa /**
445059d84dbSTetsuo Handa  * tomoyo_socket_listen - Check permission for listen().
446059d84dbSTetsuo Handa  *
447059d84dbSTetsuo Handa  * @sock:    Pointer to "struct socket".
448059d84dbSTetsuo Handa  * @backlog: Backlog parameter.
449059d84dbSTetsuo Handa  *
450059d84dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
451059d84dbSTetsuo Handa  */
tomoyo_socket_listen(struct socket * sock,int backlog)452059d84dbSTetsuo Handa static int tomoyo_socket_listen(struct socket *sock, int backlog)
453059d84dbSTetsuo Handa {
454059d84dbSTetsuo Handa 	return tomoyo_socket_listen_permission(sock);
455059d84dbSTetsuo Handa }
456059d84dbSTetsuo Handa 
457059d84dbSTetsuo Handa /**
458059d84dbSTetsuo Handa  * tomoyo_socket_connect - Check permission for connect().
459059d84dbSTetsuo Handa  *
460059d84dbSTetsuo Handa  * @sock:     Pointer to "struct socket".
461059d84dbSTetsuo Handa  * @addr:     Pointer to "struct sockaddr".
462059d84dbSTetsuo Handa  * @addr_len: Size of @addr.
463059d84dbSTetsuo Handa  *
464059d84dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
465059d84dbSTetsuo Handa  */
tomoyo_socket_connect(struct socket * sock,struct sockaddr * addr,int addr_len)466059d84dbSTetsuo Handa static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
467059d84dbSTetsuo Handa 				 int addr_len)
468059d84dbSTetsuo Handa {
469059d84dbSTetsuo Handa 	return tomoyo_socket_connect_permission(sock, addr, addr_len);
470059d84dbSTetsuo Handa }
471059d84dbSTetsuo Handa 
472059d84dbSTetsuo Handa /**
473059d84dbSTetsuo Handa  * tomoyo_socket_bind - Check permission for bind().
474059d84dbSTetsuo Handa  *
475059d84dbSTetsuo Handa  * @sock:     Pointer to "struct socket".
476059d84dbSTetsuo Handa  * @addr:     Pointer to "struct sockaddr".
477059d84dbSTetsuo Handa  * @addr_len: Size of @addr.
478059d84dbSTetsuo Handa  *
479059d84dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
480059d84dbSTetsuo Handa  */
tomoyo_socket_bind(struct socket * sock,struct sockaddr * addr,int addr_len)481059d84dbSTetsuo Handa static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
482059d84dbSTetsuo Handa 			      int addr_len)
483059d84dbSTetsuo Handa {
484059d84dbSTetsuo Handa 	return tomoyo_socket_bind_permission(sock, addr, addr_len);
485059d84dbSTetsuo Handa }
486059d84dbSTetsuo Handa 
487059d84dbSTetsuo Handa /**
488059d84dbSTetsuo Handa  * tomoyo_socket_sendmsg - Check permission for sendmsg().
489059d84dbSTetsuo Handa  *
490059d84dbSTetsuo Handa  * @sock: Pointer to "struct socket".
491059d84dbSTetsuo Handa  * @msg:  Pointer to "struct msghdr".
492059d84dbSTetsuo Handa  * @size: Size of message.
493059d84dbSTetsuo Handa  *
494059d84dbSTetsuo Handa  * Returns 0 on success, negative value otherwise.
495059d84dbSTetsuo Handa  */
tomoyo_socket_sendmsg(struct socket * sock,struct msghdr * msg,int size)496059d84dbSTetsuo Handa static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
497059d84dbSTetsuo Handa 				 int size)
498059d84dbSTetsuo Handa {
499059d84dbSTetsuo Handa 	return tomoyo_socket_sendmsg_permission(sock, msg, size);
500059d84dbSTetsuo Handa }
501059d84dbSTetsuo Handa 
502f22f9aafSPaul Moore struct lsm_blob_sizes tomoyo_blob_sizes __ro_after_init = {
5038c6cb983STetsuo Handa 	.lbs_task = sizeof(struct tomoyo_task),
504bbd3662aSCasey Schaufler };
505bbd3662aSCasey Schaufler 
5068c6cb983STetsuo Handa /**
5078c6cb983STetsuo Handa  * tomoyo_task_alloc - Target for security_task_alloc().
5088c6cb983STetsuo Handa  *
5098c6cb983STetsuo Handa  * @task:        Pointer to "struct task_struct".
51098eaa63eSChenXiaoSong  * @clone_flags: clone() flags.
5118c6cb983STetsuo Handa  *
5128c6cb983STetsuo Handa  * Returns 0.
5138c6cb983STetsuo Handa  */
tomoyo_task_alloc(struct task_struct * task,unsigned long clone_flags)5148c6cb983STetsuo Handa static int tomoyo_task_alloc(struct task_struct *task,
5158c6cb983STetsuo Handa 			     unsigned long clone_flags)
5168c6cb983STetsuo Handa {
5178c6cb983STetsuo Handa 	struct tomoyo_task *old = tomoyo_task(current);
5188c6cb983STetsuo Handa 	struct tomoyo_task *new = tomoyo_task(task);
5198c6cb983STetsuo Handa 
5208c6cb983STetsuo Handa 	new->domain_info = old->domain_info;
5218c6cb983STetsuo Handa 	atomic_inc(&new->domain_info->users);
5228c6cb983STetsuo Handa 	new->old_domain_info = NULL;
5238c6cb983STetsuo Handa 	return 0;
5248c6cb983STetsuo Handa }
5258c6cb983STetsuo Handa 
5268c6cb983STetsuo Handa /**
5278c6cb983STetsuo Handa  * tomoyo_task_free - Target for security_task_free().
5288c6cb983STetsuo Handa  *
5298c6cb983STetsuo Handa  * @task: Pointer to "struct task_struct".
5308c6cb983STetsuo Handa  */
tomoyo_task_free(struct task_struct * task)5318c6cb983STetsuo Handa static void tomoyo_task_free(struct task_struct *task)
5328c6cb983STetsuo Handa {
5338c6cb983STetsuo Handa 	struct tomoyo_task *s = tomoyo_task(task);
5348c6cb983STetsuo Handa 
5358c6cb983STetsuo Handa 	if (s->domain_info) {
5368c6cb983STetsuo Handa 		atomic_dec(&s->domain_info->users);
5378c6cb983STetsuo Handa 		s->domain_info = NULL;
5388c6cb983STetsuo Handa 	}
5398c6cb983STetsuo Handa 	if (s->old_domain_info) {
5408c6cb983STetsuo Handa 		atomic_dec(&s->old_domain_info->users);
5418c6cb983STetsuo Handa 		s->old_domain_info = NULL;
5428c6cb983STetsuo Handa 	}
5438c6cb983STetsuo Handa }
5448c6cb983STetsuo Handa 
545c3fa109aSTetsuo Handa /*
546c3fa109aSTetsuo Handa  * tomoyo_security_ops is a "struct security_operations" which is used for
547c3fa109aSTetsuo Handa  * registering TOMOYO.
548c3fa109aSTetsuo Handa  */
549f22f9aafSPaul Moore static struct security_hook_list tomoyo_hooks[] __ro_after_init = {
550e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
5518c6cb983STetsuo Handa 	LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
5528c6cb983STetsuo Handa 	LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
5538c6cb983STetsuo Handa 	LSM_HOOK_INIT(task_free, tomoyo_task_free),
5548c6cb983STetsuo Handa #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
555b8bff599SEric W. Biederman 	LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
5568c6cb983STetsuo Handa #endif
557e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
558e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
559e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, tomoyo_file_open),
5603350607dSGünther Noack 	LSM_HOOK_INIT(file_truncate, tomoyo_file_truncate),
561e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
562e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
563e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
564e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
565e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
566e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
567e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_link, tomoyo_path_link),
568e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
569e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
570e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
571*820831deSAlfred Piccioni 	LSM_HOOK_INIT(file_ioctl_compat, tomoyo_file_ioctl),
572e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
573e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
574e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
575e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
576e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
577e20b043aSCasey Schaufler 	LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
578e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
579e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
580e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
581e20b043aSCasey Schaufler 	LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
582f7433243SKentaro Takeda };
583f7433243SKentaro Takeda 
584fdb8ebb7STetsuo Handa /* Lock for GC. */
585505f14f7SLai Jiangshan DEFINE_SRCU(tomoyo_ss);
586fdb8ebb7STetsuo Handa 
587f22f9aafSPaul Moore int tomoyo_enabled __ro_after_init = 1;
58843fc4609SCasey Schaufler 
5890f2a55d5STetsuo Handa /**
5900f2a55d5STetsuo Handa  * tomoyo_init - Register TOMOYO Linux as a LSM module.
5910f2a55d5STetsuo Handa  *
5920f2a55d5STetsuo Handa  * Returns 0.
5930f2a55d5STetsuo Handa  */
tomoyo_init(void)594f7433243SKentaro Takeda static int __init tomoyo_init(void)
595f7433243SKentaro Takeda {
5968c6cb983STetsuo Handa 	struct tomoyo_task *s = tomoyo_task(current);
597f7433243SKentaro Takeda 
598f7433243SKentaro Takeda 	/* register ourselves with the security framework */
599d69dece5SCasey Schaufler 	security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
600cdcf6723STetsuo Handa 	pr_info("TOMOYO Linux initialized\n");
6018c6cb983STetsuo Handa 	s->domain_info = &tomoyo_kernel_domain;
6028c6cb983STetsuo Handa 	atomic_inc(&tomoyo_kernel_domain.users);
6038c6cb983STetsuo Handa 	s->old_domain_info = NULL;
604c3ef1500STetsuo Handa 	tomoyo_mm_init();
60543fc4609SCasey Schaufler 
606f7433243SKentaro Takeda 	return 0;
607f7433243SKentaro Takeda }
608f7433243SKentaro Takeda 
6093d6e5f6dSKees Cook DEFINE_LSM(tomoyo) = {
61007aed2f2SKees Cook 	.name = "tomoyo",
61143fc4609SCasey Schaufler 	.enabled = &tomoyo_enabled,
612a5e2fe7eSKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR,
613bbd3662aSCasey Schaufler 	.blobs = &tomoyo_blob_sizes,
6143d6e5f6dSKees Cook 	.init = tomoyo_init,
6153d6e5f6dSKees Cook };
616