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