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 | * |
| 6 | * Copyright (C) 2005-2009 NTT DATA CORPORATION |
| 7 | * |
Tetsuo Handa | 39826a1 | 2009-04-08 22:31:28 +0900 | [diff] [blame] | 8 | * Version: 2.2.0 2009/04/01 |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/security.h> |
| 13 | #include "common.h" |
| 14 | #include "tomoyo.h" |
| 15 | #include "realpath.h" |
| 16 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 17 | static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) |
| 18 | { |
| 19 | new->security = NULL; |
| 20 | return 0; |
| 21 | } |
| 22 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 23 | static int tomoyo_cred_prepare(struct cred *new, const struct cred *old, |
| 24 | gfp_t gfp) |
| 25 | { |
| 26 | /* |
| 27 | * Since "struct tomoyo_domain_info *" is a sharable pointer, |
| 28 | * we don't need to duplicate. |
| 29 | */ |
| 30 | new->security = old->security; |
| 31 | return 0; |
| 32 | } |
| 33 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 34 | static void tomoyo_cred_transfer(struct cred *new, const struct cred *old) |
| 35 | { |
| 36 | /* |
| 37 | * Since "struct tomoyo_domain_info *" is a sharable pointer, |
| 38 | * we don't need to duplicate. |
| 39 | */ |
| 40 | new->security = old->security; |
| 41 | } |
| 42 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 43 | static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) |
| 44 | { |
Herton Ronaldo Krzesinski | b1338d1 | 2009-05-26 12:15:53 +0900 | [diff] [blame] | 45 | int rc; |
| 46 | |
| 47 | rc = cap_bprm_set_creds(bprm); |
| 48 | if (rc) |
| 49 | return rc; |
| 50 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 51 | /* |
| 52 | * Do only if this function is called for the first time of an execve |
| 53 | * operation. |
| 54 | */ |
| 55 | if (bprm->cred_prepared) |
| 56 | return 0; |
| 57 | /* |
| 58 | * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested |
| 59 | * for the first time. |
| 60 | */ |
| 61 | if (!tomoyo_policy_loaded) |
| 62 | tomoyo_load_policy(bprm->filename); |
| 63 | /* |
| 64 | * Tell tomoyo_bprm_check_security() is called for the first time of an |
| 65 | * execve operation. |
| 66 | */ |
| 67 | bprm->cred->security = NULL; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int tomoyo_bprm_check_security(struct linux_binprm *bprm) |
| 72 | { |
| 73 | struct tomoyo_domain_info *domain = bprm->cred->security; |
| 74 | |
| 75 | /* |
| 76 | * Execute permission is checked against pathname passed to do_execve() |
| 77 | * using current domain. |
| 78 | */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame^] | 79 | if (!domain) { |
| 80 | /* |
| 81 | * We will need to protect whole execve() operation when GC |
| 82 | * starts kfree()ing "struct tomoyo_domain_info" because |
| 83 | * bprm->cred->security points to "struct tomoyo_domain_info" |
| 84 | * but "struct tomoyo_domain_info" does not have a refcounter. |
| 85 | */ |
| 86 | const int idx = tomoyo_read_lock(); |
| 87 | const int err = tomoyo_find_next_domain(bprm); |
| 88 | tomoyo_read_unlock(idx); |
| 89 | return err; |
| 90 | } |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 91 | /* |
| 92 | * Read permission is checked against interpreters using next domain. |
| 93 | * '1' is the result of open_to_namei_flags(O_RDONLY). |
| 94 | */ |
| 95 | return tomoyo_check_open_permission(domain, &bprm->file->f_path, 1); |
| 96 | } |
| 97 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 98 | static int tomoyo_path_truncate(struct path *path, loff_t length, |
| 99 | unsigned int time_attrs) |
| 100 | { |
| 101 | return tomoyo_check_1path_perm(tomoyo_domain(), |
| 102 | TOMOYO_TYPE_TRUNCATE_ACL, |
| 103 | path); |
| 104 | } |
| 105 | |
| 106 | static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry) |
| 107 | { |
| 108 | struct path path = { parent->mnt, dentry }; |
| 109 | return tomoyo_check_1path_perm(tomoyo_domain(), |
| 110 | TOMOYO_TYPE_UNLINK_ACL, |
| 111 | &path); |
| 112 | } |
| 113 | |
| 114 | static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry, |
| 115 | int mode) |
| 116 | { |
| 117 | struct path path = { parent->mnt, dentry }; |
| 118 | return tomoyo_check_1path_perm(tomoyo_domain(), |
| 119 | TOMOYO_TYPE_MKDIR_ACL, |
| 120 | &path); |
| 121 | } |
| 122 | |
| 123 | static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry) |
| 124 | { |
| 125 | struct path path = { parent->mnt, dentry }; |
| 126 | return tomoyo_check_1path_perm(tomoyo_domain(), |
| 127 | TOMOYO_TYPE_RMDIR_ACL, |
| 128 | &path); |
| 129 | } |
| 130 | |
| 131 | static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry, |
| 132 | const char *old_name) |
| 133 | { |
| 134 | struct path path = { parent->mnt, dentry }; |
| 135 | return tomoyo_check_1path_perm(tomoyo_domain(), |
| 136 | TOMOYO_TYPE_SYMLINK_ACL, |
| 137 | &path); |
| 138 | } |
| 139 | |
| 140 | static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry, |
| 141 | int mode, unsigned int dev) |
| 142 | { |
| 143 | struct path path = { parent->mnt, dentry }; |
| 144 | int type = TOMOYO_TYPE_CREATE_ACL; |
| 145 | |
| 146 | switch (mode & S_IFMT) { |
| 147 | case S_IFCHR: |
| 148 | type = TOMOYO_TYPE_MKCHAR_ACL; |
| 149 | break; |
| 150 | case S_IFBLK: |
| 151 | type = TOMOYO_TYPE_MKBLOCK_ACL; |
| 152 | break; |
| 153 | case S_IFIFO: |
| 154 | type = TOMOYO_TYPE_MKFIFO_ACL; |
| 155 | break; |
| 156 | case S_IFSOCK: |
| 157 | type = TOMOYO_TYPE_MKSOCK_ACL; |
| 158 | break; |
| 159 | } |
| 160 | return tomoyo_check_1path_perm(tomoyo_domain(), |
| 161 | type, &path); |
| 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 }; |
| 169 | return tomoyo_check_2path_perm(tomoyo_domain(), |
| 170 | TOMOYO_TYPE_LINK_ACL, |
| 171 | &path1, &path2); |
| 172 | } |
| 173 | |
| 174 | static int tomoyo_path_rename(struct path *old_parent, |
| 175 | struct dentry *old_dentry, |
| 176 | struct path *new_parent, |
| 177 | struct dentry *new_dentry) |
| 178 | { |
| 179 | struct path path1 = { old_parent->mnt, old_dentry }; |
| 180 | struct path path2 = { new_parent->mnt, new_dentry }; |
| 181 | return tomoyo_check_2path_perm(tomoyo_domain(), |
| 182 | TOMOYO_TYPE_RENAME_ACL, |
| 183 | &path1, &path2); |
| 184 | } |
| 185 | |
| 186 | static int tomoyo_file_fcntl(struct file *file, unsigned int cmd, |
| 187 | unsigned long arg) |
| 188 | { |
| 189 | if (cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)) |
| 190 | return tomoyo_check_rewrite_permission(tomoyo_domain(), file); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int tomoyo_dentry_open(struct file *f, const struct cred *cred) |
| 195 | { |
| 196 | int flags = f->f_flags; |
| 197 | |
| 198 | if ((flags + 1) & O_ACCMODE) |
| 199 | flags++; |
| 200 | flags |= f->f_flags & (O_APPEND | O_TRUNC); |
| 201 | /* Don't check read permission here if called from do_execve(). */ |
| 202 | if (current->in_execve) |
| 203 | return 0; |
| 204 | return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags); |
| 205 | } |
| 206 | |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 207 | static int tomoyo_file_ioctl(struct file *file, unsigned int cmd, |
| 208 | unsigned long arg) |
| 209 | { |
| 210 | return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_IOCTL_ACL, |
| 211 | &file->f_path); |
| 212 | } |
| 213 | |
| 214 | static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt, |
| 215 | mode_t mode) |
| 216 | { |
| 217 | struct path path = { mnt, dentry }; |
| 218 | return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHMOD_ACL, |
| 219 | &path); |
| 220 | } |
| 221 | |
| 222 | static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid) |
| 223 | { |
| 224 | int error = 0; |
| 225 | if (uid != (uid_t) -1) |
| 226 | error = tomoyo_check_1path_perm(tomoyo_domain(), |
| 227 | TOMOYO_TYPE_CHOWN_ACL, path); |
| 228 | if (!error && gid != (gid_t) -1) |
| 229 | error = tomoyo_check_1path_perm(tomoyo_domain(), |
| 230 | TOMOYO_TYPE_CHGRP_ACL, path); |
| 231 | return error; |
| 232 | } |
| 233 | |
| 234 | static int tomoyo_path_chroot(struct path *path) |
| 235 | { |
| 236 | return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_CHROOT_ACL, |
| 237 | path); |
| 238 | } |
| 239 | |
| 240 | static int tomoyo_sb_mount(char *dev_name, struct path *path, |
| 241 | char *type, unsigned long flags, void *data) |
| 242 | { |
| 243 | return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_MOUNT_ACL, |
| 244 | path); |
| 245 | } |
| 246 | |
| 247 | static int tomoyo_sb_umount(struct vfsmount *mnt, int flags) |
| 248 | { |
| 249 | struct path path = { mnt, mnt->mnt_root }; |
| 250 | return tomoyo_check_1path_perm(tomoyo_domain(), TOMOYO_TYPE_UMOUNT_ACL, |
| 251 | &path); |
| 252 | } |
| 253 | |
| 254 | static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path) |
| 255 | { |
| 256 | return tomoyo_check_2path_perm(tomoyo_domain(), |
| 257 | TOMOYO_TYPE_PIVOT_ROOT_ACL, |
| 258 | new_path, old_path); |
| 259 | } |
| 260 | |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 261 | /* |
| 262 | * tomoyo_security_ops is a "struct security_operations" which is used for |
| 263 | * registering TOMOYO. |
| 264 | */ |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 265 | static struct security_operations tomoyo_security_ops = { |
| 266 | .name = "tomoyo", |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 267 | .cred_alloc_blank = tomoyo_cred_alloc_blank, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 268 | .cred_prepare = tomoyo_cred_prepare, |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 269 | .cred_transfer = tomoyo_cred_transfer, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 270 | .bprm_set_creds = tomoyo_bprm_set_creds, |
| 271 | .bprm_check_security = tomoyo_bprm_check_security, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 272 | .file_fcntl = tomoyo_file_fcntl, |
| 273 | .dentry_open = tomoyo_dentry_open, |
| 274 | .path_truncate = tomoyo_path_truncate, |
| 275 | .path_unlink = tomoyo_path_unlink, |
| 276 | .path_mkdir = tomoyo_path_mkdir, |
| 277 | .path_rmdir = tomoyo_path_rmdir, |
| 278 | .path_symlink = tomoyo_path_symlink, |
| 279 | .path_mknod = tomoyo_path_mknod, |
| 280 | .path_link = tomoyo_path_link, |
| 281 | .path_rename = tomoyo_path_rename, |
Tetsuo Handa | 937bf61 | 2009-12-02 21:09:48 +0900 | [diff] [blame] | 282 | .file_ioctl = tomoyo_file_ioctl, |
| 283 | .path_chmod = tomoyo_path_chmod, |
| 284 | .path_chown = tomoyo_path_chown, |
| 285 | .path_chroot = tomoyo_path_chroot, |
| 286 | .sb_mount = tomoyo_sb_mount, |
| 287 | .sb_umount = tomoyo_sb_umount, |
| 288 | .sb_pivotroot = tomoyo_sb_pivotroot, |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 289 | }; |
| 290 | |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame^] | 291 | /* Lock for GC. */ |
| 292 | struct srcu_struct tomoyo_ss; |
| 293 | |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 294 | static int __init tomoyo_init(void) |
| 295 | { |
| 296 | struct cred *cred = (struct cred *) current_cred(); |
| 297 | |
| 298 | if (!security_module_enable(&tomoyo_security_ops)) |
| 299 | return 0; |
| 300 | /* register ourselves with the security framework */ |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame^] | 301 | if (register_security(&tomoyo_security_ops) || |
| 302 | init_srcu_struct(&tomoyo_ss)) |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 303 | panic("Failure registering TOMOYO Linux"); |
| 304 | printk(KERN_INFO "TOMOYO Linux initialized\n"); |
| 305 | cred->security = &tomoyo_kernel_domain; |
Tetsuo Handa | 1581e7d | 2009-02-21 20:40:50 +0900 | [diff] [blame] | 306 | tomoyo_realpath_init(); |
Kentaro Takeda | f743324 | 2009-02-05 17:18:16 +0900 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | security_initcall(tomoyo_init); |