Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 2 | /* |
| 3 | * security/tomoyo/tomoyo.c |
| 4 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 8 | #include <linux/lsm_hooks.h> |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 9 | #include "common.h" |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 10 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 11 | /** |
| 12 | * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank(). |
| 13 | * |
| 14 | * @new: Pointer to "struct cred". |
| 15 | * @gfp: Memory allocation flags. |
| 16 | * |
| 17 | * Returns 0. |
| 18 | */ |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 19 | static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) |
| 20 | { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 21 | struct tomoyo_domain_info **blob = tomoyo_cred(new); |
| 22 | |
| 23 | *blob = NULL; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 24 | return 0; |
| 25 | } |
| 26 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 27 | /** |
| 28 | * tomoyo_cred_prepare - Target for security_prepare_creds(). |
| 29 | * |
| 30 | * @new: Pointer to "struct cred". |
| 31 | * @old: Pointer to "struct cred". |
| 32 | * @gfp: Memory allocation flags. |
| 33 | * |
| 34 | * Returns 0. |
| 35 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 36 | static int tomoyo_cred_prepare(struct cred *new, const struct cred *old, |
| 37 | gfp_t gfp) |
| 38 | { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 39 | struct tomoyo_domain_info **old_blob = tomoyo_cred(old); |
| 40 | struct tomoyo_domain_info **new_blob = tomoyo_cred(new); |
| 41 | struct tomoyo_domain_info *domain; |
| 42 | |
| 43 | domain = *old_blob; |
| 44 | *new_blob = domain; |
| 45 | |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 46 | if (domain) |
| 47 | atomic_inc(&domain->users); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 51 | /** |
| 52 | * tomoyo_cred_transfer - Target for security_transfer_creds(). |
| 53 | * |
| 54 | * @new: Pointer to "struct cred". |
| 55 | * @old: Pointer to "struct cred". |
| 56 | */ |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 57 | static void tomoyo_cred_transfer(struct cred *new, const struct cred *old) |
| 58 | { |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 59 | tomoyo_cred_prepare(new, old, 0); |
| 60 | } |
| 61 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 62 | /** |
| 63 | * tomoyo_cred_free - Target for security_cred_free(). |
| 64 | * |
| 65 | * @cred: Pointer to "struct cred". |
| 66 | */ |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 67 | static void tomoyo_cred_free(struct cred *cred) |
| 68 | { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 69 | struct tomoyo_domain_info **blob = tomoyo_cred(cred); |
| 70 | struct tomoyo_domain_info *domain = *blob; |
| 71 | |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 72 | if (domain) |
| 73 | atomic_dec(&domain->users); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 76 | /** |
| 77 | * tomoyo_bprm_set_creds - Target for security_bprm_set_creds(). |
| 78 | * |
| 79 | * @bprm: Pointer to "struct linux_binprm". |
| 80 | * |
| 81 | * Returns 0 on success, negative value otherwise. |
| 82 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 83 | static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) |
| 84 | { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 85 | struct tomoyo_domain_info **blob; |
| 86 | struct tomoyo_domain_info *domain; |
| 87 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 88 | /* |
| 89 | * Do only if this function is called for the first time of an execve |
| 90 | * operation. |
| 91 | */ |
Kees Cook | ddb4a14 | 2017-07-18 15:25:23 -0700 | [diff] [blame] | 92 | if (bprm->called_set_creds) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 93 | return 0; |
Tetsuo Handa | 7986cf2 | 2011-06-29 13:07:52 +0900 | [diff] [blame] | 94 | #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 95 | /* |
| 96 | * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested |
| 97 | * for the first time. |
| 98 | */ |
| 99 | if (!tomoyo_policy_loaded) |
| 100 | tomoyo_load_policy(bprm->filename); |
Tetsuo Handa | 7986cf2 | 2011-06-29 13:07:52 +0900 | [diff] [blame] | 101 | #endif |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 102 | /* |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 103 | * Release reference to "struct tomoyo_domain_info" stored inside |
| 104 | * "bprm->cred->security". New reference to "struct tomoyo_domain_info" |
| 105 | * stored inside "bprm->cred->security" will be acquired later inside |
| 106 | * tomoyo_find_next_domain(). |
| 107 | */ |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 108 | blob = tomoyo_cred(bprm->cred); |
| 109 | domain = *blob; |
| 110 | atomic_dec(&domain->users); |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 111 | /* |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 112 | * Tell tomoyo_bprm_check_security() is called for the first time of an |
| 113 | * execve operation. |
| 114 | */ |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 115 | *blob = NULL; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 119 | /** |
| 120 | * tomoyo_bprm_check_security - Target for security_bprm_check(). |
| 121 | * |
| 122 | * @bprm: Pointer to "struct linux_binprm". |
| 123 | * |
| 124 | * Returns 0 on success, negative value otherwise. |
| 125 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 126 | static int tomoyo_bprm_check_security(struct linux_binprm *bprm) |
| 127 | { |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 128 | struct tomoyo_domain_info **blob; |
| 129 | struct tomoyo_domain_info *domain; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 130 | |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 131 | blob = tomoyo_cred(bprm->cred); |
| 132 | domain = *blob; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 133 | /* |
| 134 | * Execute permission is checked against pathname passed to do_execve() |
| 135 | * using current domain. |
| 136 | */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 137 | if (!domain) { |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 138 | const int idx = tomoyo_read_lock(); |
| 139 | const int err = tomoyo_find_next_domain(bprm); |
| 140 | tomoyo_read_unlock(idx); |
| 141 | return err; |
| 142 | } |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 143 | /* |
| 144 | * Read permission is checked against interpreters using next domain. |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 145 | */ |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 146 | return tomoyo_check_open_permission(domain, &bprm->file->f_path, |
| 147 | O_RDONLY); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 150 | /** |
| 151 | * tomoyo_inode_getattr - Target for security_inode_getattr(). |
| 152 | * |
| 153 | * @mnt: Pointer to "struct vfsmount". |
| 154 | * @dentry: Pointer to "struct dentry". |
| 155 | * |
| 156 | * Returns 0 on success, negative value otherwise. |
| 157 | */ |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 158 | static int tomoyo_inode_getattr(const struct path *path) |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 159 | { |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 160 | return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL); |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 163 | /** |
| 164 | * tomoyo_path_truncate - Target for security_path_truncate(). |
| 165 | * |
| 166 | * @path: Pointer to "struct path". |
| 167 | * |
| 168 | * Returns 0 on success, negative value otherwise. |
| 169 | */ |
Al Viro | 81f4c50 | 2016-03-25 14:22:01 -0400 | [diff] [blame] | 170 | static int tomoyo_path_truncate(const struct path *path) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 171 | { |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 172 | return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 173 | } |
| 174 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 175 | /** |
| 176 | * tomoyo_path_unlink - Target for security_path_unlink(). |
| 177 | * |
| 178 | * @parent: Pointer to "struct path". |
| 179 | * @dentry: Pointer to "struct dentry". |
| 180 | * |
| 181 | * Returns 0 on success, negative value otherwise. |
| 182 | */ |
Al Viro | 989f74e | 2016-03-25 15:13:39 -0400 | [diff] [blame] | 183 | static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 184 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 185 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 186 | return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 189 | /** |
| 190 | * tomoyo_path_mkdir - Target for security_path_mkdir(). |
| 191 | * |
| 192 | * @parent: Pointer to "struct path". |
| 193 | * @dentry: Pointer to "struct dentry". |
| 194 | * @mode: DAC permission mode. |
| 195 | * |
| 196 | * Returns 0 on success, negative value otherwise. |
| 197 | */ |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 198 | static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry, |
Al Viro | 4572bef | 2011-11-21 14:56:21 -0500 | [diff] [blame] | 199 | umode_t mode) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 200 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 201 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 202 | return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path, |
| 203 | mode & S_IALLUGO); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 204 | } |
| 205 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 206 | /** |
| 207 | * tomoyo_path_rmdir - Target for security_path_rmdir(). |
| 208 | * |
| 209 | * @parent: Pointer to "struct path". |
| 210 | * @dentry: Pointer to "struct dentry". |
| 211 | * |
| 212 | * Returns 0 on success, negative value otherwise. |
| 213 | */ |
Al Viro | 989f74e | 2016-03-25 15:13:39 -0400 | [diff] [blame] | 214 | static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 215 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 216 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 217 | return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 218 | } |
| 219 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 220 | /** |
| 221 | * tomoyo_path_symlink - Target for security_path_symlink(). |
| 222 | * |
| 223 | * @parent: Pointer to "struct path". |
| 224 | * @dentry: Pointer to "struct dentry". |
| 225 | * @old_name: Symlink's content. |
| 226 | * |
| 227 | * Returns 0 on success, negative value otherwise. |
| 228 | */ |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 229 | static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 230 | const char *old_name) |
| 231 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 232 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 233 | return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 234 | } |
| 235 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 236 | /** |
| 237 | * tomoyo_path_mknod - Target for security_path_mknod(). |
| 238 | * |
| 239 | * @parent: Pointer to "struct path". |
| 240 | * @dentry: Pointer to "struct dentry". |
| 241 | * @mode: DAC permission mode. |
| 242 | * @dev: Device attributes. |
| 243 | * |
| 244 | * Returns 0 on success, negative value otherwise. |
| 245 | */ |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 246 | static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry, |
Al Viro | 04fc66e | 2011-11-21 14:58:38 -0500 | [diff] [blame] | 247 | umode_t mode, unsigned int dev) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 248 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 249 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 250 | int type = TOMOYO_TYPE_CREATE; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 251 | const unsigned int perm = mode & S_IALLUGO; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 252 | |
| 253 | switch (mode & S_IFMT) { |
| 254 | case S_IFCHR: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 255 | type = TOMOYO_TYPE_MKCHAR; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 256 | break; |
| 257 | case S_IFBLK: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 258 | type = TOMOYO_TYPE_MKBLOCK; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 259 | break; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 260 | default: |
| 261 | goto no_dev; |
| 262 | } |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 263 | return tomoyo_mkdev_perm(type, &path, perm, dev); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 264 | no_dev: |
| 265 | switch (mode & S_IFMT) { |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 266 | case S_IFIFO: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 267 | type = TOMOYO_TYPE_MKFIFO; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 268 | break; |
| 269 | case S_IFSOCK: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 270 | type = TOMOYO_TYPE_MKSOCK; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 271 | break; |
| 272 | } |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 273 | return tomoyo_path_number_perm(type, &path, perm); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 274 | } |
| 275 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 276 | /** |
| 277 | * tomoyo_path_link - Target for security_path_link(). |
| 278 | * |
| 279 | * @old_dentry: Pointer to "struct dentry". |
| 280 | * @new_dir: Pointer to "struct path". |
| 281 | * @new_dentry: Pointer to "struct dentry". |
| 282 | * |
| 283 | * Returns 0 on success, negative value otherwise. |
| 284 | */ |
Al Viro | 3ccee46 | 2016-03-25 15:27:45 -0400 | [diff] [blame] | 285 | static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 286 | struct dentry *new_dentry) |
| 287 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 288 | struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry }; |
| 289 | struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 290 | return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 293 | /** |
| 294 | * tomoyo_path_rename - Target for security_path_rename(). |
| 295 | * |
| 296 | * @old_parent: Pointer to "struct path". |
| 297 | * @old_dentry: Pointer to "struct dentry". |
| 298 | * @new_parent: Pointer to "struct path". |
| 299 | * @new_dentry: Pointer to "struct dentry". |
| 300 | * |
| 301 | * Returns 0 on success, negative value otherwise. |
| 302 | */ |
Al Viro | 3ccee46 | 2016-03-25 15:27:45 -0400 | [diff] [blame] | 303 | static int tomoyo_path_rename(const struct path *old_parent, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 304 | struct dentry *old_dentry, |
Al Viro | 3ccee46 | 2016-03-25 15:27:45 -0400 | [diff] [blame] | 305 | const struct path *new_parent, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 306 | struct dentry *new_dentry) |
| 307 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 308 | struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry }; |
| 309 | struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 310 | return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 311 | } |
| 312 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 313 | /** |
| 314 | * tomoyo_file_fcntl - Target for security_file_fcntl(). |
| 315 | * |
| 316 | * @file: Pointer to "struct file". |
| 317 | * @cmd: Command for fcntl(). |
| 318 | * @arg: Argument for @cmd. |
| 319 | * |
| 320 | * Returns 0 on success, negative value otherwise. |
| 321 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 322 | static int tomoyo_file_fcntl(struct file *file, unsigned int cmd, |
| 323 | unsigned long arg) |
| 324 | { |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 325 | if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))) |
| 326 | return 0; |
| 327 | return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path, |
| 328 | O_WRONLY | (arg & O_APPEND)); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 331 | /** |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 332 | * tomoyo_file_open - Target for security_file_open(). |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 333 | * |
| 334 | * @f: Pointer to "struct file". |
| 335 | * @cred: Pointer to "struct cred". |
| 336 | * |
| 337 | * Returns 0 on success, negative value otherwise. |
| 338 | */ |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 339 | static int tomoyo_file_open(struct file *f) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 340 | { |
| 341 | int flags = f->f_flags; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 342 | /* Don't check read permission here if called from do_execve(). */ |
| 343 | if (current->in_execve) |
| 344 | return 0; |
| 345 | return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags); |
| 346 | } |
| 347 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 348 | /** |
| 349 | * tomoyo_file_ioctl - Target for security_file_ioctl(). |
| 350 | * |
| 351 | * @file: Pointer to "struct file". |
| 352 | * @cmd: Command for ioctl(). |
| 353 | * @arg: Argument for @cmd. |
| 354 | * |
| 355 | * Returns 0 on success, negative value otherwise. |
| 356 | */ |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 357 | static int tomoyo_file_ioctl(struct file *file, unsigned int cmd, |
| 358 | unsigned long arg) |
| 359 | { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 360 | return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 363 | /** |
| 364 | * tomoyo_path_chmod - Target for security_path_chmod(). |
| 365 | * |
Al Viro | cdcf116 | 2011-12-08 10:51:53 -0500 | [diff] [blame] | 366 | * @path: Pointer to "struct path". |
| 367 | * @mode: DAC permission mode. |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 368 | * |
| 369 | * Returns 0 on success, negative value otherwise. |
| 370 | */ |
Al Viro | be01f9f | 2016-03-25 14:56:23 -0400 | [diff] [blame] | 371 | static int tomoyo_path_chmod(const struct path *path, umode_t mode) |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 372 | { |
Al Viro | cdcf116 | 2011-12-08 10:51:53 -0500 | [diff] [blame] | 373 | return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path, |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 374 | mode & S_IALLUGO); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 375 | } |
| 376 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 377 | /** |
| 378 | * tomoyo_path_chown - Target for security_path_chown(). |
| 379 | * |
| 380 | * @path: Pointer to "struct path". |
| 381 | * @uid: Owner ID. |
| 382 | * @gid: Group ID. |
| 383 | * |
| 384 | * Returns 0 on success, negative value otherwise. |
| 385 | */ |
Al Viro | 7fd25da | 2016-03-25 14:44:41 -0400 | [diff] [blame] | 386 | static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 387 | { |
| 388 | int error = 0; |
Eric W. Biederman | d2b31ca | 2012-06-01 16:14:19 -0600 | [diff] [blame] | 389 | if (uid_valid(uid)) |
| 390 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, |
| 391 | from_kuid(&init_user_ns, uid)); |
| 392 | if (!error && gid_valid(gid)) |
| 393 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, |
| 394 | from_kgid(&init_user_ns, gid)); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 395 | return error; |
| 396 | } |
| 397 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 398 | /** |
| 399 | * tomoyo_path_chroot - Target for security_path_chroot(). |
| 400 | * |
| 401 | * @path: Pointer to "struct path". |
| 402 | * |
| 403 | * Returns 0 on success, negative value otherwise. |
| 404 | */ |
Al Viro | 77b286c | 2016-03-25 15:28:43 -0400 | [diff] [blame] | 405 | static int tomoyo_path_chroot(const struct path *path) |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 406 | { |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 407 | return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 408 | } |
| 409 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 410 | /** |
| 411 | * tomoyo_sb_mount - Target for security_sb_mount(). |
| 412 | * |
| 413 | * @dev_name: Name of device file. Maybe NULL. |
| 414 | * @path: Pointer to "struct path". |
| 415 | * @type: Name of filesystem type. Maybe NULL. |
| 416 | * @flags: Mount options. |
| 417 | * @data: Optional data. Maybe NULL. |
| 418 | * |
| 419 | * Returns 0 on success, negative value otherwise. |
| 420 | */ |
Al Viro | 8a04c43 | 2016-03-25 14:52:53 -0400 | [diff] [blame] | 421 | static int tomoyo_sb_mount(const char *dev_name, const struct path *path, |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 422 | const char *type, unsigned long flags, void *data) |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 423 | { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 424 | return tomoyo_mount_permission(dev_name, path, type, flags, data); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 425 | } |
| 426 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 427 | /** |
| 428 | * tomoyo_sb_umount - Target for security_sb_umount(). |
| 429 | * |
| 430 | * @mnt: Pointer to "struct vfsmount". |
| 431 | * @flags: Unmount options. |
| 432 | * |
| 433 | * Returns 0 on success, negative value otherwise. |
| 434 | */ |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 435 | static int tomoyo_sb_umount(struct vfsmount *mnt, int flags) |
| 436 | { |
Kees Cook | 8291798 | 2017-03-29 16:52:58 -0700 | [diff] [blame] | 437 | struct path path = { .mnt = mnt, .dentry = mnt->mnt_root }; |
Tetsuo Handa | 97fb35e | 2011-07-08 13:25:53 +0900 | [diff] [blame] | 438 | return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 439 | } |
| 440 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 441 | /** |
| 442 | * tomoyo_sb_pivotroot - Target for security_sb_pivotroot(). |
| 443 | * |
| 444 | * @old_path: Pointer to "struct path". |
| 445 | * @new_path: Pointer to "struct path". |
| 446 | * |
| 447 | * Returns 0 on success, negative value otherwise. |
| 448 | */ |
Al Viro | 3b73b68 | 2016-03-25 15:31:19 -0400 | [diff] [blame] | 449 | static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path) |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 450 | { |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 451 | return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 452 | } |
| 453 | |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 454 | /** |
| 455 | * tomoyo_socket_listen - Check permission for listen(). |
| 456 | * |
| 457 | * @sock: Pointer to "struct socket". |
| 458 | * @backlog: Backlog parameter. |
| 459 | * |
| 460 | * Returns 0 on success, negative value otherwise. |
| 461 | */ |
| 462 | static int tomoyo_socket_listen(struct socket *sock, int backlog) |
| 463 | { |
| 464 | return tomoyo_socket_listen_permission(sock); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * tomoyo_socket_connect - Check permission for connect(). |
| 469 | * |
| 470 | * @sock: Pointer to "struct socket". |
| 471 | * @addr: Pointer to "struct sockaddr". |
| 472 | * @addr_len: Size of @addr. |
| 473 | * |
| 474 | * Returns 0 on success, negative value otherwise. |
| 475 | */ |
| 476 | static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr, |
| 477 | int addr_len) |
| 478 | { |
| 479 | return tomoyo_socket_connect_permission(sock, addr, addr_len); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * tomoyo_socket_bind - Check permission for bind(). |
| 484 | * |
| 485 | * @sock: Pointer to "struct socket". |
| 486 | * @addr: Pointer to "struct sockaddr". |
| 487 | * @addr_len: Size of @addr. |
| 488 | * |
| 489 | * Returns 0 on success, negative value otherwise. |
| 490 | */ |
| 491 | static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr, |
| 492 | int addr_len) |
| 493 | { |
| 494 | return tomoyo_socket_bind_permission(sock, addr, addr_len); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * tomoyo_socket_sendmsg - Check permission for sendmsg(). |
| 499 | * |
| 500 | * @sock: Pointer to "struct socket". |
| 501 | * @msg: Pointer to "struct msghdr". |
| 502 | * @size: Size of message. |
| 503 | * |
| 504 | * Returns 0 on success, negative value otherwise. |
| 505 | */ |
| 506 | static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
| 507 | int size) |
| 508 | { |
| 509 | return tomoyo_socket_sendmsg_permission(sock, msg, size); |
| 510 | } |
| 511 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame^] | 512 | struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = { |
| 513 | .lbs_cred = sizeof(struct tomoyo_domain_info *), |
| 514 | }; |
| 515 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 516 | /* |
| 517 | * tomoyo_security_ops is a "struct security_operations" which is used for |
| 518 | * registering TOMOYO. |
| 519 | */ |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 520 | static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 521 | LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank), |
| 522 | LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare), |
| 523 | LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer), |
| 524 | LSM_HOOK_INIT(cred_free, tomoyo_cred_free), |
| 525 | LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds), |
| 526 | LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security), |
| 527 | LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl), |
| 528 | LSM_HOOK_INIT(file_open, tomoyo_file_open), |
| 529 | LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate), |
| 530 | LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink), |
| 531 | LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir), |
| 532 | LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir), |
| 533 | LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink), |
| 534 | LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod), |
| 535 | LSM_HOOK_INIT(path_link, tomoyo_path_link), |
| 536 | LSM_HOOK_INIT(path_rename, tomoyo_path_rename), |
| 537 | LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr), |
| 538 | LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl), |
| 539 | LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod), |
| 540 | LSM_HOOK_INIT(path_chown, tomoyo_path_chown), |
| 541 | LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot), |
| 542 | LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount), |
| 543 | LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount), |
| 544 | LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot), |
| 545 | LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind), |
| 546 | LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect), |
| 547 | LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen), |
| 548 | LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg), |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 549 | }; |
| 550 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 551 | /* Lock for GC. */ |
Lai Jiangshan | 505f14f | 2013-03-16 00:50:53 +0800 | [diff] [blame] | 552 | DEFINE_SRCU(tomoyo_ss); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 553 | |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 554 | int tomoyo_enabled __lsm_ro_after_init = 1; |
| 555 | |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 556 | /** |
| 557 | * tomoyo_init - Register TOMOYO Linux as a LSM module. |
| 558 | * |
| 559 | * Returns 0. |
| 560 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 561 | static int __init tomoyo_init(void) |
| 562 | { |
| 563 | struct cred *cred = (struct cred *) current_cred(); |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 564 | struct tomoyo_domain_info **blob; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 565 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 566 | /* register ourselves with the security framework */ |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 567 | security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo"); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 568 | printk(KERN_INFO "TOMOYO Linux initialized\n"); |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame^] | 569 | lsm_early_cred(cred); |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 570 | blob = tomoyo_cred(cred); |
| 571 | *blob = &tomoyo_kernel_domain; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 572 | tomoyo_mm_init(); |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 573 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 577 | DEFINE_LSM(tomoyo) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 578 | .name = "tomoyo", |
Casey Schaufler | 43fc460 | 2018-09-21 17:18:07 -0700 | [diff] [blame] | 579 | .enabled = &tomoyo_enabled, |
Kees Cook | 14bd99c | 2018-09-19 19:57:06 -0700 | [diff] [blame] | 580 | .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame^] | 581 | .blobs = &tomoyo_blob_sizes, |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 582 | .init = tomoyo_init, |
| 583 | }; |