Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/tomoyo.c |
| 3 | * |
| 4 | * LSM hooks for TOMOYO Linux. |
| 5 | * |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/security.h> |
| 10 | #include "common.h" |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 11 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 12 | static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) |
| 13 | { |
| 14 | new->security = NULL; |
| 15 | return 0; |
| 16 | } |
| 17 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 18 | static int tomoyo_cred_prepare(struct cred *new, const struct cred *old, |
| 19 | gfp_t gfp) |
| 20 | { |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 21 | struct tomoyo_domain_info *domain = old->security; |
| 22 | new->security = domain; |
| 23 | if (domain) |
| 24 | atomic_inc(&domain->users); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 25 | return 0; |
| 26 | } |
| 27 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 28 | static void tomoyo_cred_transfer(struct cred *new, const struct cred *old) |
| 29 | { |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 30 | tomoyo_cred_prepare(new, old, 0); |
| 31 | } |
| 32 | |
| 33 | static void tomoyo_cred_free(struct cred *cred) |
| 34 | { |
| 35 | struct tomoyo_domain_info *domain = cred->security; |
| 36 | if (domain) |
| 37 | atomic_dec(&domain->users); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 40 | static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) |
| 41 | { |
Herton Ronaldo Krzesinski | b1338d1 | 2009-05-26 12:15:53 +0900 | [diff] [blame] | 42 | int rc; |
| 43 | |
| 44 | rc = cap_bprm_set_creds(bprm); |
| 45 | if (rc) |
| 46 | return rc; |
| 47 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 48 | /* |
| 49 | * Do only if this function is called for the first time of an execve |
| 50 | * operation. |
| 51 | */ |
| 52 | if (bprm->cred_prepared) |
| 53 | return 0; |
| 54 | /* |
| 55 | * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested |
| 56 | * for the first time. |
| 57 | */ |
| 58 | if (!tomoyo_policy_loaded) |
| 59 | tomoyo_load_policy(bprm->filename); |
| 60 | /* |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 61 | * Release reference to "struct tomoyo_domain_info" stored inside |
| 62 | * "bprm->cred->security". New reference to "struct tomoyo_domain_info" |
| 63 | * stored inside "bprm->cred->security" will be acquired later inside |
| 64 | * tomoyo_find_next_domain(). |
| 65 | */ |
| 66 | atomic_dec(&((struct tomoyo_domain_info *) |
| 67 | bprm->cred->security)->users); |
| 68 | /* |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 69 | * Tell tomoyo_bprm_check_security() is called for the first time of an |
| 70 | * execve operation. |
| 71 | */ |
| 72 | bprm->cred->security = NULL; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int tomoyo_bprm_check_security(struct linux_binprm *bprm) |
| 77 | { |
| 78 | struct tomoyo_domain_info *domain = bprm->cred->security; |
| 79 | |
| 80 | /* |
| 81 | * Execute permission is checked against pathname passed to do_execve() |
| 82 | * using current domain. |
| 83 | */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 84 | if (!domain) { |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 85 | const int idx = tomoyo_read_lock(); |
| 86 | const int err = tomoyo_find_next_domain(bprm); |
| 87 | tomoyo_read_unlock(idx); |
| 88 | return err; |
| 89 | } |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 90 | /* |
| 91 | * Read permission is checked against interpreters using next domain. |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 92 | */ |
Al Viro | 6d12552 | 2009-12-24 06:58:56 -0500 | [diff] [blame] | 93 | return tomoyo_check_open_permission(domain, &bprm->file->f_path, O_RDONLY); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame^] | 96 | static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) |
| 97 | { |
| 98 | struct path path = { mnt, dentry }; |
| 99 | return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path); |
| 100 | } |
| 101 | |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 102 | static int tomoyo_path_truncate(struct path *path) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 103 | { |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 104 | return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry) |
| 108 | { |
| 109 | struct path path = { parent->mnt, dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 110 | return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry, |
| 114 | int mode) |
| 115 | { |
| 116 | struct path path = { parent->mnt, dentry }; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 117 | return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path, |
| 118 | mode & S_IALLUGO); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry) |
| 122 | { |
| 123 | struct path path = { parent->mnt, dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 124 | return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry, |
| 128 | const char *old_name) |
| 129 | { |
| 130 | struct path path = { parent->mnt, dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 131 | return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry, |
| 135 | int mode, unsigned int dev) |
| 136 | { |
| 137 | struct path path = { parent->mnt, dentry }; |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 138 | int type = TOMOYO_TYPE_CREATE; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 139 | const unsigned int perm = mode & S_IALLUGO; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 140 | |
| 141 | switch (mode & S_IFMT) { |
| 142 | case S_IFCHR: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 143 | type = TOMOYO_TYPE_MKCHAR; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 144 | break; |
| 145 | case S_IFBLK: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 146 | type = TOMOYO_TYPE_MKBLOCK; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 147 | break; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 148 | default: |
| 149 | goto no_dev; |
| 150 | } |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 151 | return tomoyo_mkdev_perm(type, &path, perm, dev); |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 152 | no_dev: |
| 153 | switch (mode & S_IFMT) { |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 154 | case S_IFIFO: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 155 | type = TOMOYO_TYPE_MKFIFO; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 156 | break; |
| 157 | case S_IFSOCK: |
Tetsuo Handa | 7ef6123 | 2010-02-16 08:03:30 +0900 | [diff] [blame] | 158 | type = TOMOYO_TYPE_MKSOCK; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 159 | break; |
| 160 | } |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 161 | return tomoyo_path_number_perm(type, &path, perm); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir, |
| 165 | struct dentry *new_dentry) |
| 166 | { |
| 167 | struct path path1 = { new_dir->mnt, old_dentry }; |
| 168 | struct path path2 = { new_dir->mnt, new_dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 169 | return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static int tomoyo_path_rename(struct path *old_parent, |
| 173 | struct dentry *old_dentry, |
| 174 | struct path *new_parent, |
| 175 | struct dentry *new_dentry) |
| 176 | { |
| 177 | struct path path1 = { old_parent->mnt, old_dentry }; |
| 178 | struct path path2 = { new_parent->mnt, new_dentry }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 179 | return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int tomoyo_file_fcntl(struct file *file, unsigned int cmd, |
| 183 | unsigned long arg) |
| 184 | { |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame^] | 185 | if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND))) |
| 186 | return 0; |
| 187 | return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path, |
| 188 | O_WRONLY | (arg & O_APPEND)); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int tomoyo_dentry_open(struct file *f, const struct cred *cred) |
| 192 | { |
| 193 | int flags = f->f_flags; |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 194 | /* Don't check read permission here if called from do_execve(). */ |
| 195 | if (current->in_execve) |
| 196 | return 0; |
| 197 | return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags); |
| 198 | } |
| 199 | |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 200 | static int tomoyo_file_ioctl(struct file *file, unsigned int cmd, |
| 201 | unsigned long arg) |
| 202 | { |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 203 | return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt, |
| 207 | mode_t mode) |
| 208 | { |
| 209 | struct path path = { mnt, dentry }; |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 210 | return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path, |
| 211 | mode & S_IALLUGO); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid) |
| 215 | { |
| 216 | int error = 0; |
| 217 | if (uid != (uid_t) -1) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 218 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 219 | if (!error && gid != (gid_t) -1) |
Tetsuo Handa | a1f9bb6 | 2010-05-17 10:09:15 +0900 | [diff] [blame] | 220 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 221 | return error; |
| 222 | } |
| 223 | |
| 224 | static int tomoyo_path_chroot(struct path *path) |
| 225 | { |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 226 | return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int tomoyo_sb_mount(char *dev_name, struct path *path, |
| 230 | char *type, unsigned long flags, void *data) |
| 231 | { |
Tetsuo Handa | 2106ccd | 2010-05-17 10:10:31 +0900 | [diff] [blame] | 232 | return tomoyo_mount_permission(dev_name, path, type, flags, data); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int tomoyo_sb_umount(struct vfsmount *mnt, int flags) |
| 236 | { |
| 237 | struct path path = { mnt, mnt->mnt_root }; |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 238 | return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path) |
| 242 | { |
Tetsuo Handa | 97d6931 | 2010-02-16 09:46:15 +0900 | [diff] [blame] | 243 | return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path); |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 244 | } |
| 245 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 246 | /* |
| 247 | * tomoyo_security_ops is a "struct security_operations" which is used for |
| 248 | * registering TOMOYO. |
| 249 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 250 | static struct security_operations tomoyo_security_ops = { |
| 251 | .name = "tomoyo", |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 252 | .cred_alloc_blank = tomoyo_cred_alloc_blank, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 253 | .cred_prepare = tomoyo_cred_prepare, |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 254 | .cred_transfer = tomoyo_cred_transfer, |
Tetsuo Handa | ec8e6a4 | 2010-02-11 09:43:20 +0900 | [diff] [blame] | 255 | .cred_free = tomoyo_cred_free, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 256 | .bprm_set_creds = tomoyo_bprm_set_creds, |
| 257 | .bprm_check_security = tomoyo_bprm_check_security, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 258 | .file_fcntl = tomoyo_file_fcntl, |
| 259 | .dentry_open = tomoyo_dentry_open, |
| 260 | .path_truncate = tomoyo_path_truncate, |
| 261 | .path_unlink = tomoyo_path_unlink, |
| 262 | .path_mkdir = tomoyo_path_mkdir, |
| 263 | .path_rmdir = tomoyo_path_rmdir, |
| 264 | .path_symlink = tomoyo_path_symlink, |
| 265 | .path_mknod = tomoyo_path_mknod, |
| 266 | .path_link = tomoyo_path_link, |
| 267 | .path_rename = tomoyo_path_rename, |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame^] | 268 | .inode_getattr = tomoyo_inode_getattr, |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 269 | .file_ioctl = tomoyo_file_ioctl, |
| 270 | .path_chmod = tomoyo_path_chmod, |
| 271 | .path_chown = tomoyo_path_chown, |
| 272 | .path_chroot = tomoyo_path_chroot, |
| 273 | .sb_mount = tomoyo_sb_mount, |
| 274 | .sb_umount = tomoyo_sb_umount, |
| 275 | .sb_pivotroot = tomoyo_sb_pivotroot, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 276 | }; |
| 277 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 278 | /* Lock for GC. */ |
| 279 | struct srcu_struct tomoyo_ss; |
| 280 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 281 | static int __init tomoyo_init(void) |
| 282 | { |
| 283 | struct cred *cred = (struct cred *) current_cred(); |
| 284 | |
| 285 | if (!security_module_enable(&tomoyo_security_ops)) |
| 286 | return 0; |
| 287 | /* register ourselves with the security framework */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 288 | if (register_security(&tomoyo_security_ops) || |
| 289 | init_srcu_struct(&tomoyo_ss)) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 290 | panic("Failure registering TOMOYO Linux"); |
| 291 | printk(KERN_INFO "TOMOYO Linux initialized\n"); |
| 292 | cred->security = &tomoyo_kernel_domain; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 293 | tomoyo_mm_init(); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | security_initcall(tomoyo_init); |