Pull security subsystem updates from James Morris: - Extend LSM stacking to allow sharing of cred, file, ipc, inode, and task blobs. This paves the way for more full-featured LSMs to be merged, and is specifically aimed at LandLock and SARA LSMs. This work is from Casey and Kees. - There's a new LSM from Micah Morton: "SafeSetID gates the setid family of syscalls to restrict UID/GID transitions from a given UID/GID to only those approved by a system-wide whitelist." This feature is currently shipping in ChromeOS. * 'next-general' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (62 commits) keys: fix missing __user in KEYCTL_PKEY_QUERY LSM: Update list of SECURITYFS users in Kconfig LSM: Ignore "security=" when "lsm=" is specified LSM: Update function documentation for cap_capable security: mark expected switch fall-throughs and add a missing break tomoyo: Bump version. LSM: fix return value check in safesetid_init_securityfs() LSM: SafeSetID: add selftest LSM: SafeSetID: remove unused include LSM: SafeSetID: 'depend' on CONFIG_SECURITY LSM: Add 'name' field for SafeSetID in DEFINE_LSM LSM: add SafeSetID module that gates setid calls LSM: add SafeSetID module that gates setid calls tomoyo: Allow multiple use_group lines. tomoyo: Coding style fix. tomoyo: Swicth from cred->security to task_struct->security. security: keys: annotate implicit fall throughs security: keys: annotate implicit fall throughs security: keys: annotate implicit fall through capabilities:: annotate implicit fall through ...
Unified · Split
| @@ -0,0 +1,107 @@ | ||
| 1 | +========= | |
| 2 | +SafeSetID | |
| 3 | +========= | |
| 4 | +SafeSetID is an LSM module that gates the setid family of syscalls to restrict | |
| 5 | +UID/GID transitions from a given UID/GID to only those approved by a | |
| 6 | +system-wide whitelist. These restrictions also prohibit the given UIDs/GIDs | |
| 7 | +from obtaining auxiliary privileges associated with CAP_SET{U/G}ID, such as | |
| 8 | +allowing a user to set up user namespace UID mappings. | |
| 9 | + | |
| 10 | + | |
| 11 | +Background | |
| 12 | +========== | |
| 13 | +In absence of file capabilities, processes spawned on a Linux system that need | |
| 14 | +to switch to a different user must be spawned with CAP_SETUID privileges. | |
| 15 | +CAP_SETUID is granted to programs running as root or those running as a non-root | |
| 16 | +user that have been explicitly given the CAP_SETUID runtime capability. It is | |
| 17 | +often preferable to use Linux runtime capabilities rather than file | |
| 18 | +capabilities, since using file capabilities to run a program with elevated | |
| 19 | +privileges opens up possible security holes since any user with access to the | |
| 20 | +file can exec() that program to gain the elevated privileges. | |
| 21 | + | |
| 22 | +While it is possible to implement a tree of processes by giving full | |
| 23 | +CAP_SET{U/G}ID capabilities, this is often at odds with the goals of running a | |
| 24 | +tree of processes under non-root user(s) in the first place. Specifically, | |
| 25 | +since CAP_SETUID allows changing to any user on the system, including the root | |
| 26 | +user, it is an overpowered capability for what is needed in this scenario, | |
| 27 | +especially since programs often only call setuid() to drop privileges to a | |
| 28 | +lesser-privileged user -- not elevate privileges. Unfortunately, there is no | |
| 29 | +generally feasible way in Linux to restrict the potential UIDs that a user can | |
| 30 | +switch to through setuid() beyond allowing a switch to any user on the system. | |
| 31 | +This SafeSetID LSM seeks to provide a solution for restricting setid | |
| 32 | +capabilities in such a way. | |
| 33 | + | |
| 34 | +The main use case for this LSM is to allow a non-root program to transition to | |
| 35 | +other untrusted uids without full blown CAP_SETUID capabilities. The non-root | |
| 36 | +program would still need CAP_SETUID to do any kind of transition, but the | |
| 37 | +additional restrictions imposed by this LSM would mean it is a "safer" version | |
| 38 | +of CAP_SETUID since the non-root program cannot take advantage of CAP_SETUID to | |
| 39 | +do any unapproved actions (e.g. setuid to uid 0 or create/enter new user | |
| 40 | +namespace). The higher level goal is to allow for uid-based sandboxing of system | |
| 41 | +services without having to give out CAP_SETUID all over the place just so that | |
| 42 | +non-root programs can drop to even-lesser-privileged uids. This is especially | |
| 43 | +relevant when one non-root daemon on the system should be allowed to spawn other | |
| 44 | +processes as different uids, but its undesirable to give the daemon a | |
| 45 | +basically-root-equivalent CAP_SETUID. | |
| 46 | + | |
| 47 | + | |
| 48 | +Other Approaches Considered | |
| 49 | +=========================== | |
| 50 | + | |
| 51 | +Solve this problem in userspace | |
| 52 | +------------------------------- | |
| 53 | +For candidate applications that would like to have restricted setid capabilities | |
| 54 | +as implemented in this LSM, an alternative option would be to simply take away | |
| 55 | +setid capabilities from the application completely and refactor the process | |
| 56 | +spawning semantics in the application (e.g. by using a privileged helper program | |
| 57 | +to do process spawning and UID/GID transitions). Unfortunately, there are a | |
| 58 | +number of semantics around process spawning that would be affected by this, such | |
| 59 | +as fork() calls where the program doesn???t immediately call exec() after the | |
| 60 | +fork(), parent processes specifying custom environment variables or command line | |
| 61 | +args for spawned child processes, or inheritance of file handles across a | |
| 62 | +fork()/exec(). Because of this, as solution that uses a privileged helper in | |
| 63 | +userspace would likely be less appealing to incorporate into existing projects | |
| 64 | +that rely on certain process-spawning semantics in Linux. | |
| 65 | + | |
| 66 | +Use user namespaces | |
| 67 | +------------------- | |
| 68 | +Another possible approach would be to run a given process tree in its own user | |
| 69 | +namespace and give programs in the tree setid capabilities. In this way, | |
| 70 | +programs in the tree could change to any desired UID/GID in the context of their | |
| 71 | +own user namespace, and only approved UIDs/GIDs could be mapped back to the | |
| 72 | +initial system user namespace, affectively preventing privilege escalation. | |
| 73 | +Unfortunately, it is not generally feasible to use user namespaces in isolation, | |
| 74 | +without pairing them with other namespace types, which is not always an option. | |
| 75 | +Linux checks for capabilities based off of the user namespace that ???owns??? some | |
| 76 | +entity. For example, Linux has the notion that network namespaces are owned by | |
| 77 | +the user namespace in which they were created. A consequence of this is that | |
| 78 | +capability checks for access to a given network namespace are done by checking | |
| 79 | +whether a task has the given capability in the context of the user namespace | |
| 80 | +that owns the network namespace -- not necessarily the user namespace under | |
| 81 | +which the given task runs. Therefore spawning a process in a new user namespace | |
| 82 | +effectively prevents it from accessing the network namespace owned by the | |
| 83 | +initial namespace. This is a deal-breaker for any application that expects to | |
| 84 | +retain the CAP_NET_ADMIN capability for the purpose of adjusting network | |
| 85 | +configurations. Using user namespaces in isolation causes problems regarding | |
| 86 | +other system interactions, including use of pid namespaces and device creation. | |
| 87 | + | |
| 88 | +Use an existing LSM | |
| 89 | +------------------- | |
| 90 | +None of the other in-tree LSMs have the capability to gate setid transitions, or | |
| 91 | +even employ the security_task_fix_setuid hook at all. SELinux says of that hook: | |
| 92 | +"Since setuid only affects the current process, and since the SELinux controls | |
| 93 | +are not based on the Linux identity attributes, SELinux does not need to control | |
| 94 | +this operation." | |
| 95 | + | |
| 96 | + | |
| 97 | +Directions for use | |
| 98 | +================== | |
| 99 | +This LSM hooks the setid syscalls to make sure transitions are allowed if an | |
| 100 | +applicable restriction policy is in place. Policies are configured through | |
| 101 | +securityfs by writing to the safesetid/add_whitelist_policy and | |
| 102 | +safesetid/flush_whitelist_policies files at the location where securityfs is | |
| 103 | +mounted. The format for adding a policy is '<UID>:<UID>', using literal | |
| 104 | +numbers, such as '123:456'. To flush the policies, any write to the file is | |
| 105 | +sufficient. Again, configuring a policy for a UID will prevent that UID from | |
| 106 | +obtaining auxiliary setid privileges, such as allowing a user to set up user | |
| 107 | +namespace UID mappings. | |
| @@ -17,9 +17,8 @@ MAC extensions, other extensions can be built using the LSM to provide · 16 unmodified lines | ||
| 17 | 17 | specific changes to system operation when these tweaks are not available |
| 18 | 18 | in the core functionality of Linux itself. |
| 19 | 19 | |
| 20 | −Without a specific LSM built into the kernel, the default LSM will be the | |
| 21 | −Linux capabilities system. Most LSMs choose to extend the capabilities | |
| 22 | −system, building their checks on top of the defined capability hooks. | |
| 20 | +The Linux capabilities modules will always be included. This may be | |
| 21 | +followed by any number of "minor" modules and at most one "major" module. | |
| 23 | 22 | For more details on capabilities, see ``capabilities(7)`` in the Linux |
| 24 | 23 | man-pages project. |
| 25 | 24 | |
| @@ -30,6 +29,14 @@ order in which checks are made. The capability module will always · 4 unmodified lines | ||
| 30 | 29 | be first, followed by any "minor" modules (e.g. Yama) and then |
| 31 | 30 | the one "major" module (e.g. SELinux) if there is one configured. |
| 32 | 31 | |
| 32 | +Process attributes associated with "major" security modules should | |
| 33 | +be accessed and maintained using the special files in ``/proc/.../attr``. | |
| 34 | +A security module may maintain a module specific subdirectory there, | |
| 35 | +named after the module. ``/proc/.../attr/smack`` is provided by the Smack | |
| 36 | +security module and contains all its special files. The files directly | |
| 37 | +in ``/proc/.../attr`` remain as legacy interfaces for modules that provide | |
| 38 | +subdirectories. | |
| 39 | + | |
| 33 | 40 | .. toctree:: |
| 34 | 41 | :maxdepth: 1 |
| 35 | 42 | |
| @@ -39,3 +46,4 @@ the one "major" module (e.g. SELinux) if there is one configured. · 3 unmodified lines | ||
| 39 | 46 | Smack |
| 40 | 47 | tomoyo |
| 41 | 48 | Yama |
| 49 | + SafeSetID | |
| @@ -2333,6 +2333,10 @@ · 2332 unmodified lines | ||
| 2333 | 2333 | |
| 2334 | 2334 | lsm.debug [SECURITY] Enable LSM initialization debugging output. |
| 2335 | 2335 | |
| 2336 | + lsm=lsm1,...,lsmN | |
| 2337 | + [SECURITY] Choose order of LSM initialization. This | |
| 2338 | + overrides CONFIG_LSM, and the "security=" parameter. | |
| 2339 | + | |
| 2336 | 2340 | machvec= [IA-64] Force the use of a particular machine-vector |
| 2337 | 2341 | (machvec) in a generic kernel. |
| 2338 | 2342 | Example: machvec=hpzx1_swiotlb |
| @@ -4110,11 +4114,9 @@ · 1771 unmodified lines | ||
| 4110 | 4114 | Note: increases power consumption, thus should only be |
| 4111 | 4115 | enabled if running jitter sensitive (HPC/RT) workloads. |
| 4112 | 4116 | |
| 4113 | − security= [SECURITY] Choose a security module to enable at boot. | |
| 4114 | − If this boot parameter is not specified, only the first | |
| 4115 | − security module asking for security registration will be | |
| 4116 | − loaded. An invalid security module name will be treated | |
| 4117 | − as if no module has been chosen. | |
| 4117 | + security= [SECURITY] Choose a legacy "major" security module to | |
| 4118 | + enable at boot. This has been deprecated by the | |
| 4119 | + "lsm=" parameter. | |
| 4118 | 4120 | |
| 4119 | 4121 | selinux= [SELINUX] Disable or enable SELinux at boot time. |
| 4120 | 4122 | Format: { "0" | "1" } |
| @@ -15557,12 +15557,11 @@ F: mm/shmem.c · 15556 unmodified lines | ||
| 15557 | 15557 | TOMOYO SECURITY MODULE |
| 15558 | 15558 | M: Kentaro Takeda <takedakn@nttdata.co.jp> |
| 15559 | 15559 | M: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> |
| 15560 | −L: tomoyo-dev-en@lists.sourceforge.jp (subscribers-only, for developers in English) | |
| 15561 | −L: tomoyo-users-en@lists.sourceforge.jp (subscribers-only, for users in English) | |
| 15562 | −L: tomoyo-dev@lists.sourceforge.jp (subscribers-only, for developers in Japanese) | |
| 15563 | −L: tomoyo-users@lists.sourceforge.jp (subscribers-only, for users in Japanese) | |
| 15564 | −W: http://tomoyo.sourceforge.jp/ | |
| 15565 | −T: quilt http://svn.sourceforge.jp/svnroot/tomoyo/trunk/2.5.x/tomoyo-lsm/patches/ | |
| 15560 | +L: tomoyo-dev-en@lists.osdn.me (subscribers-only, for developers in English) | |
| 15561 | +L: tomoyo-users-en@lists.osdn.me (subscribers-only, for users in English) | |
| 15562 | +L: tomoyo-dev@lists.osdn.me (subscribers-only, for developers in Japanese) | |
| 15563 | +L: tomoyo-users@lists.osdn.me (subscribers-only, for users in Japanese) | |
| 15564 | +W: https://tomoyo.osdn.jp/ | |
| 15566 | 15565 | S: Maintained |
| 15567 | 15566 | F: security/tomoyo/ |
| 15568 | 15567 | |
| @@ -140,9 +140,13 @@ struct pid_entry { · 139 unmodified lines | ||
| 140 | 140 | #define REG(NAME, MODE, fops) \ |
| 141 | 141 | NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) |
| 142 | 142 | #define ONE(NAME, MODE, show) \ |
| 143 | − NOD(NAME, (S_IFREG|(MODE)), \ | |
| 143 | + NOD(NAME, (S_IFREG|(MODE)), \ | |
| 144 | 144 | NULL, &proc_single_file_operations, \ |
| 145 | 145 | { .proc_show = show } ) |
| 146 | +#define ATTR(LSM, NAME, MODE) \ | |
| 147 | + NOD(NAME, (S_IFREG|(MODE)), \ | |
| 148 | + NULL, &proc_pid_attr_operations, \ | |
| 149 | + { .lsm = LSM }) | |
| 146 | 150 | |
| 147 | 151 | /* |
| 148 | 152 | * Count the number of hardlinks for the pid_entry table, excluding the . |
| @@ -2521,7 +2525,7 @@ static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, · 2372 unmodified lines | ||
| 2521 | 2525 | if (!task) |
| 2522 | 2526 | return -ESRCH; |
| 2523 | 2527 | |
| 2524 | − length = security_getprocattr(task, | |
| 2528 | + length = security_getprocattr(task, PROC_I(inode)->op.lsm, | |
| 2525 | 2529 | (char*)file->f_path.dentry->d_name.name, |
| 2526 | 2530 | &p); |
| 2527 | 2531 | put_task_struct(task); |
| @@ -2570,7 +2574,9 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, · 42 unmodified lines | ||
| 2570 | 2574 | if (rv < 0) |
| 2571 | 2575 | goto out_free; |
| 2572 | 2576 | |
| 2573 | − rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count); | |
| 2577 | + rv = security_setprocattr(PROC_I(inode)->op.lsm, | |
| 2578 | + file->f_path.dentry->d_name.name, page, | |
| 2579 | + count); | |
| 2574 | 2580 | mutex_unlock(¤t->signal->cred_guard_mutex); |
| 2575 | 2581 | out_free: |
| 2576 | 2582 | kfree(page); |
| @@ -2584,13 +2590,53 @@ static const struct file_operations proc_pid_attr_operations = { · 7 unmodified lines | ||
| 2584 | 2590 | .llseek = generic_file_llseek, |
| 2585 | 2591 | }; |
| 2586 | 2592 | |
| 2593 | +#define LSM_DIR_OPS(LSM) \ | |
| 2594 | +static int proc_##LSM##_attr_dir_iterate(struct file *filp, \ | |
| 2595 | + struct dir_context *ctx) \ | |
| 2596 | +{ \ | |
| 2597 | + return proc_pident_readdir(filp, ctx, \ | |
| 2598 | + LSM##_attr_dir_stuff, \ | |
| 2599 | + ARRAY_SIZE(LSM##_attr_dir_stuff)); \ | |
| 2600 | +} \ | |
| 2601 | +\ | |
| 2602 | +static const struct file_operations proc_##LSM##_attr_dir_ops = { \ | |
| 2603 | + .read = generic_read_dir, \ | |
| 2604 | + .iterate = proc_##LSM##_attr_dir_iterate, \ | |
| 2605 | + .llseek = default_llseek, \ | |
| 2606 | +}; \ | |
| 2607 | +\ | |
| 2608 | +static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \ | |
| 2609 | + struct dentry *dentry, unsigned int flags) \ | |
| 2610 | +{ \ | |
| 2611 | + return proc_pident_lookup(dir, dentry, \ | |
| 2612 | + LSM##_attr_dir_stuff, \ | |
| 2613 | + ARRAY_SIZE(LSM##_attr_dir_stuff)); \ | |
| 2614 | +} \ | |
| 2615 | +\ | |
| 2616 | +static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \ | |
| 2617 | + .lookup = proc_##LSM##_attr_dir_lookup, \ | |
| 2618 | + .getattr = pid_getattr, \ | |
| 2619 | + .setattr = proc_setattr, \ | |
| 2620 | +} | |
| 2621 | + | |
| 2622 | +#ifdef CONFIG_SECURITY_SMACK | |
| 2623 | +static const struct pid_entry smack_attr_dir_stuff[] = { | |
| 2624 | + ATTR("smack", "current", 0666), | |
| 2625 | +}; | |
| 2626 | +LSM_DIR_OPS(smack); | |
| 2627 | +#endif | |
| 2628 | + | |
| 2587 | 2629 | static const struct pid_entry attr_dir_stuff[] = { |
| 2588 | − REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), | |
| 2589 | − REG("prev", S_IRUGO, proc_pid_attr_operations), | |
| 2590 | − REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), | |
| 2591 | − REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), | |
| 2592 | − REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), | |
| 2593 | − REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), | |
| 2630 | + ATTR(NULL, "current", 0666), | |
| 2631 | + ATTR(NULL, "prev", 0444), | |
| 2632 | + ATTR(NULL, "exec", 0666), | |
| 2633 | + ATTR(NULL, "fscreate", 0666), | |
| 2634 | + ATTR(NULL, "keycreate", 0666), | |
| 2635 | + ATTR(NULL, "sockcreate", 0666), | |
| 2636 | +#ifdef CONFIG_SECURITY_SMACK | |
| 2637 | + DIR("smack", 0555, | |
| 2638 | + proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops), | |
| 2639 | +#endif | |
| 2594 | 2640 | }; |
| 2595 | 2641 | |
| 2596 | 2642 | static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) |
| @@ -82,6 +82,7 @@ union proc_op { · 81 unmodified lines | ||
| 82 | 82 | int (*proc_show)(struct seq_file *m, |
| 83 | 83 | struct pid_namespace *ns, struct pid *pid, |
| 84 | 84 | struct task_struct *task); |
| 85 | + const char *lsm; | |
| 85 | 86 | }; |
| 86 | 87 | |
| 87 | 88 | struct proc_inode { |
| @@ -209,6 +209,7 @@ extern bool has_ns_capability_noaudit(struct task_struct *t, · 208 unmodified lines | ||
| 209 | 209 | extern bool capable(int cap); |
| 210 | 210 | extern bool ns_capable(struct user_namespace *ns, int cap); |
| 211 | 211 | extern bool ns_capable_noaudit(struct user_namespace *ns, int cap); |
| 212 | +extern bool ns_capable_setid(struct user_namespace *ns, int cap); | |
| 212 | 213 | #else |
| 213 | 214 | static inline bool has_capability(struct task_struct *t, int cap) |
| 214 | 215 | { |
| @@ -240,6 +241,10 @@ static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap) · 25 unmodified lines | ||
| 240 | 241 | { |
| 241 | 242 | return true; |
| 242 | 243 | } |
| 244 | +static inline bool ns_capable_setid(struct user_namespace *ns, int cap) | |
| 245 | +{ | |
| 246 | + return true; | |
| 247 | +} | |
| 243 | 248 | #endif /* CONFIG_MULTIUSER */ |
| 244 | 249 | extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode); |
| 245 | 250 | extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap); |
| @@ -15,7 +15,6 @@ · 14 unmodified lines | ||
| 15 | 15 | #include <linux/capability.h> |
| 16 | 16 | #include <linux/init.h> |
| 17 | 17 | #include <linux/key.h> |
| 18 | −#include <linux/selinux.h> | |
| 19 | 18 | #include <linux/atomic.h> |
| 20 | 19 | #include <linux/uidgid.h> |
| 21 | 20 | #include <linux/sched.h> |
| @@ -1270,7 +1270,7 @@ · 1269 unmodified lines | ||
| 1270 | 1270 | * @cred contains the credentials to use. |
| 1271 | 1271 | * @ns contains the user namespace we want the capability in |
| 1272 | 1272 | * @cap contains the capability <include/linux/capability.h>. |
| 1273 | − * @audit contains whether to write an audit message or not | |
| 1273 | + * @opts contains options for the capable check <include/linux/security.h> | |
| 1274 | 1274 | * Return 0 if the capability is granted for @tsk. |
| 1275 | 1275 | * @syslog: |
| 1276 | 1276 | * Check permission before accessing the kernel message ring or changing |
| @@ -1446,8 +1446,10 @@ union security_list_options { · 169 unmodified lines | ||
| 1446 | 1446 | const kernel_cap_t *effective, |
| 1447 | 1447 | const kernel_cap_t *inheritable, |
| 1448 | 1448 | const kernel_cap_t *permitted); |
| 1449 | − int (*capable)(const struct cred *cred, struct user_namespace *ns, | |
| 1450 | − int cap, int audit); | |
| 1449 | + int (*capable)(const struct cred *cred, | |
| 1450 | + struct user_namespace *ns, | |
| 1451 | + int cap, | |
| 1452 | + unsigned int opts); | |
| 1451 | 1453 | int (*quotactl)(int cmds, int type, int id, struct super_block *sb); |
| 1452 | 1454 | int (*quota_on)(struct dentry *dentry); |
| 1453 | 1455 | int (*syslog)(int type); |
| @@ -2027,6 +2029,18 @@ struct security_hook_list { · 573 unmodified lines | ||
| 2027 | 2029 | char *lsm; |
| 2028 | 2030 | } __randomize_layout; |
| 2029 | 2031 | |
| 2032 | +/* | |
| 2033 | + * Security blob size or offset data. | |
| 2034 | + */ | |
| 2035 | +struct lsm_blob_sizes { | |
| 2036 | + int lbs_cred; | |
| 2037 | + int lbs_file; | |
| 2038 | + int lbs_inode; | |
| 2039 | + int lbs_ipc; | |
| 2040 | + int lbs_msg_msg; | |
| 2041 | + int lbs_task; | |
| 2042 | +}; | |
| 2043 | + | |
| 2030 | 2044 | /* |
| 2031 | 2045 | * Initializing a security_hook_list structure takes |
| 2032 | 2046 | * up a lot of space in a source file. This macro takes |
| @@ -2042,9 +2056,21 @@ extern char *lsm_names; · 9 unmodified lines | ||
| 2042 | 2056 | extern void security_add_hooks(struct security_hook_list *hooks, int count, |
| 2043 | 2057 | char *lsm); |
| 2044 | 2058 | |
| 2059 | +#define LSM_FLAG_LEGACY_MAJOR BIT(0) | |
| 2060 | +#define LSM_FLAG_EXCLUSIVE BIT(1) | |
| 2061 | + | |
| 2062 | +enum lsm_order { | |
| 2063 | + LSM_ORDER_FIRST = -1, /* This is only for capabilities. */ | |
| 2064 | + LSM_ORDER_MUTABLE = 0, | |
| 2065 | +}; | |
| 2066 | + | |
| 2045 | 2067 | struct lsm_info { |
| 2046 | 2068 | const char *name; /* Required. */ |
| 2069 | + enum lsm_order order; /* Optional: default is LSM_ORDER_MUTABLE */ | |
| 2070 | + unsigned long flags; /* Optional: flags describing LSM */ | |
| 2071 | + int *enabled; /* Optional: controlled by CONFIG_LSM */ | |
| 2047 | 2072 | int (*init)(void); /* Required. */ |
| 2073 | + struct lsm_blob_sizes *blobs; /* Optional: for blob sharing. */ | |
| 2048 | 2074 | }; |
| 2049 | 2075 | |
| 2050 | 2076 | extern struct lsm_info __start_lsm_info[], __end_lsm_info[]; |
| @@ -2084,17 +2110,6 @@ static inline void security_delete_hooks(struct security_hook_list *hooks, · 33 unmodified lines | ||
| 2084 | 2110 | #define __lsm_ro_after_init __ro_after_init |
| 2085 | 2111 | #endif /* CONFIG_SECURITY_WRITABLE_HOOKS */ |
| 2086 | 2112 | |
| 2087 | −extern int __init security_module_enable(const char *module); | |
| 2088 | −extern void __init capability_add_hooks(void); | |
| 2089 | −#ifdef CONFIG_SECURITY_YAMA | |
| 2090 | −extern void __init yama_add_hooks(void); | |
| 2091 | −#else | |
| 2092 | −static inline void __init yama_add_hooks(void) { } | |
| 2093 | −#endif | |
| 2094 | −#ifdef CONFIG_SECURITY_LOADPIN | |
| 2095 | −void __init loadpin_add_hooks(void); | |
| 2096 | −#else | |
| 2097 | −static inline void loadpin_add_hooks(void) { }; | |
| 2098 | −#endif | |
| 2113 | +extern int lsm_inode_alloc(struct inode *inode); | |
| 2099 | 2114 | |
| 2100 | 2115 | #endif /* ! __LINUX_LSM_HOOKS_H */ |
| @@ -54,9 +54,12 @@ struct xattr; · 53 unmodified lines | ||
| 54 | 54 | struct xfrm_sec_ctx; |
| 55 | 55 | struct mm_struct; |
| 56 | 56 | |
| 57 | +/* Default (no) options for the capable function */ | |
| 58 | +#define CAP_OPT_NONE 0x0 | |
| 57 | 59 | /* If capable should audit the security request */ |
| 58 | −#define SECURITY_CAP_NOAUDIT 0 | |
| 59 | −#define SECURITY_CAP_AUDIT 1 | |
| 60 | +#define CAP_OPT_NOAUDIT BIT(1) | |
| 61 | +/* If capable is being called by a setid function */ | |
| 62 | +#define CAP_OPT_INSETID BIT(2) | |
| 60 | 63 | |
| 61 | 64 | /* LSM Agnostic defines for sb_set_mnt_opts */ |
| 62 | 65 | #define SECURITY_LSM_NATIVE_LABELS 1 |
| @@ -72,7 +75,7 @@ enum lsm_event { · 9 unmodified lines | ||
| 72 | 75 | |
| 73 | 76 | /* These functions are in security/commoncap.c */ |
| 74 | 77 | extern int cap_capable(const struct cred *cred, struct user_namespace *ns, |
| 75 | − int cap, int audit); | |
| 78 | + int cap, unsigned int opts); | |
| 76 | 79 | extern int cap_settime(const struct timespec64 *ts, const struct timezone *tz); |
| 77 | 80 | extern int cap_ptrace_access_check(struct task_struct *child, unsigned int mode); |
| 78 | 81 | extern int cap_ptrace_traceme(struct task_struct *parent); |
| @@ -207,10 +210,10 @@ int security_capset(struct cred *new, const struct cred *old, · 128 unmodified lines | ||
| 207 | 210 | const kernel_cap_t *effective, |
| 208 | 211 | const kernel_cap_t *inheritable, |
| 209 | 212 | const kernel_cap_t *permitted); |
| 210 | −int security_capable(const struct cred *cred, struct user_namespace *ns, | |
| 211 | − int cap); | |
| 212 | −int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns, | |
| 213 | − int cap); | |
| 213 | +int security_capable(const struct cred *cred, | |
| 214 | + struct user_namespace *ns, | |
| 215 | + int cap, | |
| 216 | + unsigned int opts); | |
| 214 | 217 | int security_quotactl(int cmds, int type, int id, struct super_block *sb); |
| 215 | 218 | int security_quota_on(struct dentry *dentry); |
| 216 | 219 | int security_syslog(int type); |
| @@ -366,8 +369,10 @@ int security_sem_semctl(struct kern_ipc_perm *sma, int cmd); · 149 unmodified lines | ||
| 366 | 369 | int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops, |
| 367 | 370 | unsigned nsops, int alter); |
| 368 | 371 | void security_d_instantiate(struct dentry *dentry, struct inode *inode); |
| 369 | −int security_getprocattr(struct task_struct *p, char *name, char **value); | |
| 370 | −int security_setprocattr(const char *name, void *value, size_t size); | |
| 372 | +int security_getprocattr(struct task_struct *p, const char *lsm, char *name, | |
| 373 | + char **value); | |
| 374 | +int security_setprocattr(const char *lsm, const char *name, void *value, | |
| 375 | + size_t size); | |
| 371 | 376 | int security_netlink_send(struct sock *sk, struct sk_buff *skb); |
| 372 | 377 | int security_ismaclabel(const char *name); |
| 373 | 378 | int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen); |
| @@ -462,14 +467,11 @@ static inline int security_capset(struct cred *new, · 88 unmodified lines | ||
| 462 | 467 | } |
| 463 | 468 | |
| 464 | 469 | static inline int security_capable(const struct cred *cred, |
| 465 | − struct user_namespace *ns, int cap) | |
| 470 | + struct user_namespace *ns, | |
| 471 | + int cap, | |
| 472 | + unsigned int opts) | |
| 466 | 473 | { |
| 467 | − return cap_capable(cred, ns, cap, SECURITY_CAP_AUDIT); | |
| 468 | −} | |
| 469 | − | |
| 470 | −static inline int security_capable_noaudit(const struct cred *cred, | |
| 471 | − struct user_namespace *ns, int cap) { | |
| 472 | − return cap_capable(cred, ns, cap, SECURITY_CAP_NOAUDIT); | |
| 474 | + return cap_capable(cred, ns, cap, opts); | |
| 473 | 475 | } |
| 474 | 476 | |
| 475 | 477 | static inline int security_quotactl(int cmds, int type, int id, |
| @@ -1112,15 +1114,18 @@ static inline int security_sem_semop(struct kern_ipc_perm *sma, · 636 unmodified lines | ||
| 1112 | 1114 | return 0; |
| 1113 | 1115 | } |
| 1114 | 1116 | |
| 1115 | −static inline void security_d_instantiate(struct dentry *dentry, struct inode *inode) | |
| 1117 | +static inline void security_d_instantiate(struct dentry *dentry, | |
| 1118 | + struct inode *inode) | |
| 1116 | 1119 | { } |
| 1117 | 1120 | |
| 1118 | −static inline int security_getprocattr(struct task_struct *p, char *name, char **value) | |
| 1121 | +static inline int security_getprocattr(struct task_struct *p, const char *lsm, | |
| 1122 | + char *name, char **value) | |
| 1119 | 1123 | { |
| 1120 | 1124 | return -EINVAL; |
| 1121 | 1125 | } |
| 1122 | 1126 | |
| 1123 | −static inline int security_setprocattr(char *name, void *value, size_t size) | |
| 1127 | +static inline int security_setprocattr(const char *lsm, char *name, | |
| 1128 | + void *value, size_t size) | |
| 1124 | 1129 | { |
| 1125 | 1130 | return -EINVAL; |
| 1126 | 1131 | } |
| @@ -1,35 +0,0 @@ | ||
| 1 | −/* | |
| 2 | − * SELinux services exported to the rest of the kernel. | |
| 3 | − * | |
| 4 | − * Author: James Morris <jmorris@redhat.com> | |
| 5 | − * | |
| 6 | − * Copyright (C) 2005 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
| 7 | − * Copyright (C) 2006 Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> | |
| 8 | − * Copyright (C) 2006 IBM Corporation, Timothy R. Chavez <tinytim@us.ibm.com> | |
| 9 | − * | |
| 10 | − * This program is free software; you can redistribute it and/or modify | |
| 11 | − * it under the terms of the GNU General Public License version 2, | |
| 12 | − * as published by the Free Software Foundation. | |
| 13 | − */ | |
| 14 | −#ifndef _LINUX_SELINUX_H | |
| 15 | −#define _LINUX_SELINUX_H | |
| 16 | − | |
| 17 | −struct selinux_audit_rule; | |
| 18 | −struct audit_context; | |
| 19 | −struct kern_ipc_perm; | |
| 20 | − | |
| 21 | −#ifdef CONFIG_SECURITY_SELINUX | |
| 22 | − | |
| 23 | −/** | |
| 24 | − * selinux_is_enabled - is SELinux enabled? | |
| 25 | − */ | |
| 26 | −bool selinux_is_enabled(void); | |
| 27 | −#else | |
| 28 | − | |
| 29 | −static inline bool selinux_is_enabled(void) | |
| 30 | −{ | |
| 31 | − return false; | |
| 32 | −} | |
| 33 | −#endif /* CONFIG_SECURITY_SELINUX */ | |
| 34 | − | |
| 35 | −#endif /* _LINUX_SELINUX_H */ | |
| @@ -93,9 +93,7 @@ static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy) · 92 unmodified lines | ||
| 93 | 93 | break; |
| 94 | 94 | case _LINUX_CAPABILITY_VERSION_2: |
| 95 | 95 | warn_deprecated_v2(); |
| 96 | − /* | |
| 97 | − * fall through - v3 is otherwise equivalent to v2. | |
| 98 | − */ | |
| 96 | + /* fall through - v3 is otherwise equivalent to v2. */ | |
| 99 | 97 | case _LINUX_CAPABILITY_VERSION_3: |
| 100 | 98 | *tocopy = _LINUX_CAPABILITY_U32S_3; |
| 101 | 99 | break; |
| @@ -299,7 +297,7 @@ bool has_ns_capability(struct task_struct *t, · 197 unmodified lines | ||
| 299 | 297 | int ret; |
| 300 | 298 | |
| 301 | 299 | rcu_read_lock(); |
| 302 | − ret = security_capable(__task_cred(t), ns, cap); | |
| 300 | + ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE); | |
| 303 | 301 | rcu_read_unlock(); |
| 304 | 302 | |
| 305 | 303 | return (ret == 0); |
| @@ -340,7 +338,7 @@ bool has_ns_capability_noaudit(struct task_struct *t, · 34 unmodified lines | ||
| 340 | 338 | int ret; |
| 341 | 339 | |
| 342 | 340 | rcu_read_lock(); |
| 343 | − ret = security_capable_noaudit(__task_cred(t), ns, cap); | |
| 341 | + ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT); | |
| 344 | 342 | rcu_read_unlock(); |
| 345 | 343 | |
| 346 | 344 | return (ret == 0); |
| @@ -363,7 +361,9 @@ bool has_capability_noaudit(struct task_struct *t, int cap) · 16 unmodified lines | ||
| 363 | 361 | return has_ns_capability_noaudit(t, &init_user_ns, cap); |
| 364 | 362 | } |
| 365 | 363 | |
| 366 | −static bool ns_capable_common(struct user_namespace *ns, int cap, bool audit) | |
| 364 | +static bool ns_capable_common(struct user_namespace *ns, | |
| 365 | + int cap, | |
| 366 | + unsigned int opts) | |
| 367 | 367 | { |
| 368 | 368 | int capable; |
| 369 | 369 | |
| @@ -372,8 +372,7 @@ static bool ns_capable_common(struct user_namespace *ns, int cap, bool audit) · 2 unmodified lines | ||
| 372 | 372 | BUG(); |
| 373 | 373 | } |
| 374 | 374 | |
| 375 | − capable = audit ? security_capable(current_cred(), ns, cap) : | |
| 376 | − security_capable_noaudit(current_cred(), ns, cap); | |
| 375 | + capable = security_capable(current_cred(), ns, cap, opts); | |
| 377 | 376 | if (capable == 0) { |
| 378 | 377 | current->flags |= PF_SUPERPRIV; |
| 379 | 378 | return true; |
| @@ -394,7 +393,7 @@ static bool ns_capable_common(struct user_namespace *ns, int cap, bool audit) · 14 unmodified lines | ||
| 394 | 393 | */ |
| 395 | 394 | bool ns_capable(struct user_namespace *ns, int cap) |
| 396 | 395 | { |
| 397 | − return ns_capable_common(ns, cap, true); | |
| 396 | + return ns_capable_common(ns, cap, CAP_OPT_NONE); | |
| 398 | 397 | } |
| 399 | 398 | EXPORT_SYMBOL(ns_capable); |
| 400 | 399 | |
| @@ -412,10 +411,29 @@ EXPORT_SYMBOL(ns_capable); · 11 unmodified lines | ||
| 412 | 411 | */ |
| 413 | 412 | bool ns_capable_noaudit(struct user_namespace *ns, int cap) |
| 414 | 413 | { |
| 415 | − return ns_capable_common(ns, cap, false); | |
| 414 | + return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT); | |
| 416 | 415 | } |
| 417 | 416 | EXPORT_SYMBOL(ns_capable_noaudit); |
| 418 | 417 | |
| 418 | +/** | |
| 419 | + * ns_capable_setid - Determine if the current task has a superior capability | |
| 420 | + * in effect, while signalling that this check is being done from within a | |
| 421 | + * setid syscall. | |
| 422 | + * @ns: The usernamespace we want the capability in | |
| 423 | + * @cap: The capability to be tested for | |
| 424 | + * | |
| 425 | + * Return true if the current task has the given superior capability currently | |
| 426 | + * available for use, false if not. | |
| 427 | + * | |
| 428 | + * This sets PF_SUPERPRIV on the task if the capability is available on the | |
| 429 | + * assumption that it's about to be used. | |
| 430 | + */ | |
| 431 | +bool ns_capable_setid(struct user_namespace *ns, int cap) | |
| 432 | +{ | |
| 433 | + return ns_capable_common(ns, cap, CAP_OPT_INSETID); | |
| 434 | +} | |
| 435 | +EXPORT_SYMBOL(ns_capable_setid); | |
| 436 | + | |
| 419 | 437 | /** |
| 420 | 438 | * capable - Determine if the current task has a superior capability in effect |
| 421 | 439 | * @cap: The capability to be tested for |
| @@ -448,10 +466,11 @@ EXPORT_SYMBOL(capable); · 26 unmodified lines | ||
| 448 | 466 | bool file_ns_capable(const struct file *file, struct user_namespace *ns, |
| 449 | 467 | int cap) |
| 450 | 468 | { |
| 469 | + | |
| 451 | 470 | if (WARN_ON_ONCE(!cap_valid(cap))) |
| 452 | 471 | return false; |
| 453 | 472 | |
| 454 | − if (security_capable(file->f_cred, ns, cap) == 0) | |
| 473 | + if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0) | |
| 455 | 474 | return true; |
| 456 | 475 | |
| 457 | 476 | return false; |
| @@ -500,10 +519,12 @@ bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns) · 42 unmodified lines | ||
| 500 | 519 | { |
| 501 | 520 | int ret = 0; /* An absent tracer adds no restrictions */ |
| 502 | 521 | const struct cred *cred; |
| 522 | + | |
| 503 | 523 | rcu_read_lock(); |
| 504 | 524 | cred = rcu_dereference(tsk->ptracer_cred); |
| 505 | 525 | if (cred) |
| 506 | − ret = security_capable_noaudit(cred, ns, CAP_SYS_PTRACE); | |
| 526 | + ret = security_capable(cred, ns, CAP_SYS_PTRACE, | |
| 527 | + CAP_OPT_NOAUDIT); | |
| 507 | 528 | rcu_read_unlock(); |
| 508 | 529 | return (ret == 0); |
| 509 | 530 | } |
| @@ -760,19 +760,6 @@ bool creds_are_invalid(const struct cred *cred) · 759 unmodified lines | ||
| 760 | 760 | { |
| 761 | 761 | if (cred->magic != CRED_MAGIC) |
| 762 | 762 | return true; |
| 763 | −#ifdef CONFIG_SECURITY_SELINUX | |
| 764 | − /* | |
| 765 | − * cred->security == NULL if security_cred_alloc_blank() or | |
| 766 | − * security_prepare_creds() returned an error. | |
| 767 | − */ | |
| 768 | − if (selinux_is_enabled() && cred->security) { | |
| 769 | − if ((unsigned long) cred->security < PAGE_SIZE) | |
| 770 | − return true; | |
| 771 | − if ((*(u32 *)cred->security & 0xffffff00) == | |
| 772 | − (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)) | |
| 773 | − return true; | |
| 774 | − } | |
| 775 | −#endif | |
| 776 | 763 | return false; |
| 777 | 764 | } |
| 778 | 765 | EXPORT_SYMBOL(creds_are_invalid); |
| @@ -445,8 +445,8 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) · 444 unmodified lines | ||
| 445 | 445 | * behavior of privileged children. |
| 446 | 446 | */ |
| 447 | 447 | if (!task_no_new_privs(current) && |
| 448 | − security_capable_noaudit(current_cred(), current_user_ns(), | |
| 449 | − CAP_SYS_ADMIN) != 0) | |
| 448 | + security_capable(current_cred(), current_user_ns(), | |
| 449 | + CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) != 0) | |
| 450 | 450 | return ERR_PTR(-EACCES); |
| 451 | 451 | |
| 452 | 452 | /* Allocate a new seccomp_filter */ |
| @@ -516,7 +516,7 @@ long __sys_setreuid(uid_t ruid, uid_t euid) · 515 unmodified lines | ||
| 516 | 516 | new->uid = kruid; |
| 517 | 517 | if (!uid_eq(old->uid, kruid) && |
| 518 | 518 | !uid_eq(old->euid, kruid) && |
| 519 | − !ns_capable(old->user_ns, CAP_SETUID)) | |
| 519 | + !ns_capable_setid(old->user_ns, CAP_SETUID)) | |
| 520 | 520 | goto error; |
| 521 | 521 | } |
| 522 | 522 | |
| @@ -525,7 +525,7 @@ long __sys_setreuid(uid_t ruid, uid_t euid) · 2 unmodified lines | ||
| 525 | 525 | if (!uid_eq(old->uid, keuid) && |
| 526 | 526 | !uid_eq(old->euid, keuid) && |
| 527 | 527 | !uid_eq(old->suid, keuid) && |
| 528 | − !ns_capable(old->user_ns, CAP_SETUID)) | |
| 528 | + !ns_capable_setid(old->user_ns, CAP_SETUID)) | |
| 529 | 529 | goto error; |
| 530 | 530 | } |
| 531 | 531 | |
| @@ -584,7 +584,7 @@ long __sys_setuid(uid_t uid) · 52 unmodified lines | ||
| 584 | 584 | old = current_cred(); |
| 585 | 585 | |
| 586 | 586 | retval = -EPERM; |
| 587 | − if (ns_capable(old->user_ns, CAP_SETUID)) { | |
| 587 | + if (ns_capable_setid(old->user_ns, CAP_SETUID)) { | |
| 588 | 588 | new->suid = new->uid = kuid; |
| 589 | 589 | if (!uid_eq(kuid, old->uid)) { |
| 590 | 590 | retval = set_user(new); |
| @@ -646,7 +646,7 @@ long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid) · 55 unmodified lines | ||
| 646 | 646 | old = current_cred(); |
| 647 | 647 | |
| 648 | 648 | retval = -EPERM; |
| 649 | − if (!ns_capable(old->user_ns, CAP_SETUID)) { | |
| 649 | + if (!ns_capable_setid(old->user_ns, CAP_SETUID)) { | |
| 650 | 650 | if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) && |
| 651 | 651 | !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid)) |
| 652 | 652 | goto error; |
| @@ -814,7 +814,7 @@ long __sys_setfsuid(uid_t uid) · 161 unmodified lines | ||
| 814 | 814 | |
| 815 | 815 | if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) || |
| 816 | 816 | uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) || |
| 817 | − ns_capable(old->user_ns, CAP_SETUID)) { | |
| 817 | + ns_capable_setid(old->user_ns, CAP_SETUID)) { | |
| 818 | 818 | if (!uid_eq(kuid, old->fsuid)) { |
| 819 | 819 | new->fsuid = kuid; |
| 820 | 820 | if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0) |
| @@ -40,8 +40,7 @@ config SECURITYFS · 39 unmodified lines | ||
| 40 | 40 | bool "Enable the securityfs filesystem" |
| 41 | 41 | help |
| 42 | 42 | This will build the securityfs filesystem. It is currently used by |
| 43 | − the TPM bios character driver and IMA, an integrity provider. It is | |
| 44 | − not used by SELinux or SMACK. | |
| 43 | + various security modules (AppArmor, IMA, SafeSetID, TOMOYO, TPM). | |
| 45 | 44 | |
| 46 | 45 | If you are unsure how to answer this question, answer N. |
| 47 | 46 | |
| @@ -236,45 +235,19 @@ source "security/tomoyo/Kconfig" · 188 unmodified lines | ||
| 236 | 235 | source "security/apparmor/Kconfig" |
| 237 | 236 | source "security/loadpin/Kconfig" |
| 238 | 237 | source "security/yama/Kconfig" |
| 238 | +source "security/safesetid/Kconfig" | |
| 239 | 239 | |
| 240 | 240 | source "security/integrity/Kconfig" |
| 241 | 241 | |
| 242 | −choice | |
| 243 | − prompt "Default security module" | |
| 244 | − default DEFAULT_SECURITY_SELINUX if SECURITY_SELINUX | |
| 245 | − default DEFAULT_SECURITY_SMACK if SECURITY_SMACK | |
| 246 | − default DEFAULT_SECURITY_TOMOYO if SECURITY_TOMOYO | |
| 247 | − default DEFAULT_SECURITY_APPARMOR if SECURITY_APPARMOR | |
| 248 | − default DEFAULT_SECURITY_DAC | |
| 249 | − | |
| 242 | +config LSM | |
| 243 | + string "Ordered list of enabled LSMs" | |
| 244 | + default "yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor" | |
| 250 | 245 | help |
| 251 | − Select the security module that will be used by default if the | |
| 252 | − kernel parameter security= is not specified. | |
| 253 | − | |
| 254 | − config DEFAULT_SECURITY_SELINUX | |
| 255 | − bool "SELinux" if SECURITY_SELINUX=y | |
| 256 | − | |
| 257 | − config DEFAULT_SECURITY_SMACK | |
| 258 | − bool "Simplified Mandatory Access Control" if SECURITY_SMACK=y | |
| 259 | − | |
| 260 | − config DEFAULT_SECURITY_TOMOYO | |
| 261 | − bool "TOMOYO" if SECURITY_TOMOYO=y | |
| 262 | − | |
| 263 | − config DEFAULT_SECURITY_APPARMOR | |
| 264 | − bool "AppArmor" if SECURITY_APPARMOR=y | |
| 265 | − | |
| 266 | − config DEFAULT_SECURITY_DAC | |
| 267 | − bool "Unix Discretionary Access Controls" | |
| 268 | − | |
| 269 | −endchoice | |
| 246 | + A comma-separated list of LSMs, in initialization order. | |
| 247 | + Any LSMs left off this list will be ignored. This can be | |
| 248 | + controlled at boot with the "lsm=" parameter. | |
| 270 | 249 | |
| 271 | −config DEFAULT_SECURITY | |
| 272 | − string | |
| 273 | − default "selinux" if DEFAULT_SECURITY_SELINUX | |
| 274 | − default "smack" if DEFAULT_SECURITY_SMACK | |
| 275 | − default "tomoyo" if DEFAULT_SECURITY_TOMOYO | |
| 276 | − default "apparmor" if DEFAULT_SECURITY_APPARMOR | |
| 277 | − default "" if DEFAULT_SECURITY_DAC | |
| 250 | + If unsure, leave this as the default. | |
| 278 | 251 | |
| 279 | 252 | endmenu |
| 280 | 253 | |
| @@ -10,6 +10,7 @@ subdir-$(CONFIG_SECURITY_TOMOYO) += tomoyo · 9 unmodified lines | ||
| 10 | 10 | subdir-$(CONFIG_SECURITY_APPARMOR) += apparmor |
| 11 | 11 | subdir-$(CONFIG_SECURITY_YAMA) += yama |
| 12 | 12 | subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin |
| 13 | +subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid | |
| 13 | 14 | |
| 14 | 15 | # always enable default capabilities |
| 15 | 16 | obj-y += commoncap.o |
| @@ -25,6 +26,7 @@ obj-$(CONFIG_SECURITY_TOMOYO) += tomoyo/ · 9 unmodified lines | ||
| 25 | 26 | obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/ |
| 26 | 27 | obj-$(CONFIG_SECURITY_YAMA) += yama/ |
| 27 | 28 | obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ |
| 29 | +obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ | |
| 28 | 30 | obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o |
| 29 | 31 | |
| 30 | 32 | # Object integrity file lists |
| @@ -14,22 +14,6 @@ config SECURITY_APPARMOR · 13 unmodified lines | ||
| 14 | 14 | |
| 15 | 15 | If you are unsure how to answer this question, answer N. |
| 16 | 16 | |
| 17 | −config SECURITY_APPARMOR_BOOTPARAM_VALUE | |
| 18 | − int "AppArmor boot parameter default value" | |
| 19 | − depends on SECURITY_APPARMOR | |
| 20 | − range 0 1 | |
| 21 | − default 1 | |
| 22 | − help | |
| 23 | − This option sets the default value for the kernel parameter | |
| 24 | − 'apparmor', which allows AppArmor to be enabled or disabled | |
| 25 | − at boot. If this option is set to 0 (zero), the AppArmor | |
| 26 | − kernel parameter will default to 0, disabling AppArmor at | |
| 27 | − boot. If this option is set to 1 (one), the AppArmor | |
| 28 | − kernel parameter will default to 1, enabling AppArmor at | |
| 29 | − boot. | |
| 30 | − | |
| 31 | − If you are unsure how to answer this question, answer 1. | |
| 32 | − | |
| 33 | 17 | config SECURITY_APPARMOR_HASH |
| 34 | 18 | bool "Enable introspection of sha1 hashes for loaded profiles" |
| 35 | 19 | depends on SECURITY_APPARMOR |
| @@ -110,13 +110,13 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile, · 109 unmodified lines | ||
| 110 | 110 | * profile_capable - test if profile allows use of capability @cap |
| 111 | 111 | * @profile: profile being enforced (NOT NULL, NOT unconfined) |
| 112 | 112 | * @cap: capability to test if allowed |
| 113 | − * @audit: whether an audit record should be generated | |
| 113 | + * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated | |
| 114 | 114 | * @sa: audit data (MAY BE NULL indicating no auditing) |
| 115 | 115 | * |
| 116 | 116 | * Returns: 0 if allowed else -EPERM |
| 117 | 117 | */ |
| 118 | −static int profile_capable(struct aa_profile *profile, int cap, int audit, | |
| 119 | − struct common_audit_data *sa) | |
| 118 | +static int profile_capable(struct aa_profile *profile, int cap, | |
| 119 | + unsigned int opts, struct common_audit_data *sa) | |
| 120 | 120 | { |
| 121 | 121 | int error; |
| 122 | 122 | |
| @@ -126,7 +126,7 @@ static int profile_capable(struct aa_profile *profile, int cap, int audit, · 3 unmodified lines | ||
| 126 | 126 | else |
| 127 | 127 | error = -EPERM; |
| 128 | 128 | |
| 129 | − if (audit == SECURITY_CAP_NOAUDIT) { | |
| 129 | + if (opts & CAP_OPT_NOAUDIT) { | |
| 130 | 130 | if (!COMPLAIN_MODE(profile)) |
| 131 | 131 | return error; |
| 132 | 132 | /* audit the cap request in complain mode but note that it |
| @@ -142,13 +142,13 @@ static int profile_capable(struct aa_profile *profile, int cap, int audit, · 9 unmodified lines | ||
| 142 | 142 | * aa_capable - test permission to use capability |
| 143 | 143 | * @label: label being tested for capability (NOT NULL) |
| 144 | 144 | * @cap: capability to be tested |
| 145 | − * @audit: whether an audit record should be generated | |
| 145 | + * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated | |
| 146 | 146 | * |
| 147 | 147 | * Look up capability in profile capability set. |
| 148 | 148 | * |
| 149 | 149 | * Returns: 0 on success, or else an error code. |
| 150 | 150 | */ |
| 151 | −int aa_capable(struct aa_label *label, int cap, int audit) | |
| 151 | +int aa_capable(struct aa_label *label, int cap, unsigned int opts) | |
| 152 | 152 | { |
| 153 | 153 | struct aa_profile *profile; |
| 154 | 154 | int error = 0; |
| @@ -156,7 +156,7 @@ int aa_capable(struct aa_label *label, int cap, int audit) · 1 unmodified line | ||
| 156 | 156 | |
| 157 | 157 | sa.u.cap = cap; |
| 158 | 158 | error = fn_for_each_confined(label, profile, |
| 159 | − profile_capable(profile, cap, audit, &sa)); | |
| 159 | + profile_capable(profile, cap, opts, &sa)); | |
| 160 | 160 | |
| 161 | 161 | return error; |
| 162 | 162 | } |
| @@ -572,7 +572,7 @@ static struct aa_label *x_to_label(struct aa_profile *profile, · 571 unmodified lines | ||
| 572 | 572 | stack = NULL; |
| 573 | 573 | break; |
| 574 | 574 | } |
| 575 | − /* fall through to X_NAME */ | |
| 575 | + /* fall through - to X_NAME */ | |
| 576 | 576 | case AA_X_NAME: |
| 577 | 577 | if (xindex & AA_X_CHILD) |
| 578 | 578 | /* released by caller */ |
| @@ -975,7 +975,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm) · 396 unmodified lines | ||
| 975 | 975 | } |
| 976 | 976 | aa_put_label(cred_label(bprm->cred)); |
| 977 | 977 | /* transfer reference, released when cred is freed */ |
| 978 | − cred_label(bprm->cred) = new; | |
| 978 | + set_cred_label(bprm->cred, new); | |
| 979 | 979 | |
| 980 | 980 | done: |
| 981 | 981 | aa_put_label(label); |
| @@ -40,7 +40,7 @@ struct aa_caps { · 39 unmodified lines | ||
| 40 | 40 | |
| 41 | 41 | extern struct aa_sfs_entry aa_sfs_entry_caps[]; |
| 42 | 42 | |
| 43 | −int aa_capable(struct aa_label *label, int cap, int audit); | |
| 43 | +int aa_capable(struct aa_label *label, int cap, unsigned int opts); | |
| 44 | 44 | |
| 45 | 45 | static inline void aa_free_cap_rules(struct aa_caps *caps) |
| 46 | 46 | { |
| @@ -23,8 +23,22 @@ · 22 unmodified lines | ||
| 23 | 23 | #include "policy_ns.h" |
| 24 | 24 | #include "task.h" |
| 25 | 25 | |
| 26 | −#define cred_label(X) ((X)->security) | |
| 26 | +static inline struct aa_label *cred_label(const struct cred *cred) | |
| 27 | +{ | |
| 28 | + struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred; | |
| 29 | + | |
| 30 | + AA_BUG(!blob); | |
| 31 | + return *blob; | |
| 32 | +} | |
| 27 | 33 | |
| 34 | +static inline void set_cred_label(const struct cred *cred, | |
| 35 | + struct aa_label *label) | |
| 36 | +{ | |
| 37 | + struct aa_label **blob = cred->security + apparmor_blob_sizes.lbs_cred; | |
| 38 | + | |
| 39 | + AA_BUG(!blob); | |
| 40 | + *blob = label; | |
| 41 | +} | |
| 28 | 42 | |
| 29 | 43 | /** |
| 30 | 44 | * aa_cred_raw_label - obtain cred's label |
| @@ -32,7 +32,10 @@ struct path; · 31 unmodified lines | ||
| 32 | 32 | AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \ |
| 33 | 33 | AA_EXEC_MMAP | AA_MAY_LINK) |
| 34 | 34 | |
| 35 | −#define file_ctx(X) ((struct aa_file_ctx *)(X)->f_security) | |
| 35 | +static inline struct aa_file_ctx *file_ctx(struct file *file) | |
| 36 | +{ | |
| 37 | + return file->f_security + apparmor_blob_sizes.lbs_file; | |
| 38 | +} | |
| 36 | 39 | |
| 37 | 40 | /* struct aa_file_ctx - the AppArmor context the file was opened in |
| 38 | 41 | * @lock: lock to update the ctx |
| @@ -16,6 +16,7 @@ · 15 unmodified lines | ||
| 16 | 16 | |
| 17 | 17 | #include <linux/slab.h> |
| 18 | 18 | #include <linux/fs.h> |
| 19 | +#include <linux/lsm_hooks.h> | |
| 19 | 20 | |
| 20 | 21 | #include "match.h" |
| 21 | 22 | |
| @@ -55,6 +56,9 @@ const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, · 33 unmodified lines | ||
| 55 | 56 | size_t *ns_len); |
| 56 | 57 | void aa_info_message(const char *str); |
| 57 | 58 | |
| 59 | +/* Security blob offsets */ | |
| 60 | +extern struct lsm_blob_sizes apparmor_blob_sizes; | |
| 61 | + | |
| 58 | 62 | /** |
| 59 | 63 | * aa_strneq - compare null terminated @str to a non null terminated substring |
| 60 | 64 | * @str: a null terminated string |
| @@ -14,7 +14,10 @@ · 13 unmodified lines | ||
| 14 | 14 | #ifndef __AA_TASK_H |
| 15 | 15 | #define __AA_TASK_H |
| 16 | 16 | |
| 17 | −#define task_ctx(X) ((X)->security) | |
| 17 | +static inline struct aa_task_ctx *task_ctx(struct task_struct *task) | |
| 18 | +{ | |
| 19 | + return task->security + apparmor_blob_sizes.lbs_task; | |
| 20 | +} | |
| 18 | 21 | |
| 19 | 22 | /* |
| 20 | 23 | * struct aa_task_ctx - information for current task label change |
| @@ -36,17 +39,6 @@ int aa_set_current_hat(struct aa_label *label, u64 token); · 15 unmodified lines | ||
| 36 | 39 | int aa_restore_previous_label(u64 cookie); |
| 37 | 40 | struct aa_label *aa_get_task_label(struct task_struct *task); |
| 38 | 41 | |
| 39 | −/** | |
| 40 | − * aa_alloc_task_ctx - allocate a new task_ctx | |
| 41 | − * @flags: gfp flags for allocation | |
| 42 | − * | |
| 43 | − * Returns: allocated buffer or NULL on failure | |
| 44 | − */ | |
| 45 | −static inline struct aa_task_ctx *aa_alloc_task_ctx(gfp_t flags) | |
| 46 | −{ | |
| 47 | − return kzalloc(sizeof(struct aa_task_ctx), flags); | |
| 48 | −} | |
| 49 | − | |
| 50 | 42 | /** |
| 51 | 43 | * aa_free_task_ctx - free a task_ctx |
| 52 | 44 | * @ctx: task_ctx to free (MAYBE NULL) |
| @@ -57,8 +49,6 @@ static inline void aa_free_task_ctx(struct aa_task_ctx *ctx) · 4 unmodified lines | ||
| 57 | 49 | aa_put_label(ctx->nnp); |
| 58 | 50 | aa_put_label(ctx->previous); |
| 59 | 51 | aa_put_label(ctx->onexec); |
| 60 | − | |
| 61 | − kzfree(ctx); | |
| 62 | 52 | } |
| 63 | 53 | } |
| 64 | 54 | |
| @@ -107,7 +107,8 @@ static int profile_tracer_perm(struct aa_profile *tracer, · 106 unmodified lines | ||
| 107 | 107 | aad(sa)->label = &tracer->label; |
| 108 | 108 | aad(sa)->peer = tracee; |
| 109 | 109 | aad(sa)->request = 0; |
| 110 | − aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE, 1); | |
| 110 | + aad(sa)->error = aa_capable(&tracer->label, CAP_SYS_PTRACE, | |
| 111 | + CAP_OPT_NONE); | |
| 111 | 112 | |
| 112 | 113 | return aa_audit(AUDIT_APPARMOR_AUTO, tracer, sa, audit_ptrace_cb); |
| 113 | 114 | } |
| @@ -60,7 +60,7 @@ DEFINE_PER_CPU(struct aa_buffers, aa_buffers); · 59 unmodified lines | ||
| 60 | 60 | static void apparmor_cred_free(struct cred *cred) |
| 61 | 61 | { |
| 62 | 62 | aa_put_label(cred_label(cred)); |
| 63 | − cred_label(cred) = NULL; | |
| 63 | + set_cred_label(cred, NULL); | |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /* |
| @@ -68,7 +68,7 @@ static void apparmor_cred_free(struct cred *cred) · 1 unmodified line | ||
| 68 | 68 | */ |
| 69 | 69 | static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 70 | 70 | { |
| 71 | − cred_label(cred) = NULL; | |
| 71 | + set_cred_label(cred, NULL); | |
| 72 | 72 | return 0; |
| 73 | 73 | } |
| 74 | 74 | |
| @@ -78,7 +78,7 @@ static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) · 3 unmodified lines | ||
| 78 | 78 | static int apparmor_cred_prepare(struct cred *new, const struct cred *old, |
| 79 | 79 | gfp_t gfp) |
| 80 | 80 | { |
| 81 | − cred_label(new) = aa_get_newest_label(cred_label(old)); | |
| 81 | + set_cred_label(new, aa_get_newest_label(cred_label(old))); | |
| 82 | 82 | return 0; |
| 83 | 83 | } |
| 84 | 84 | |
| @@ -87,26 +87,21 @@ static int apparmor_cred_prepare(struct cred *new, const struct cred *old, · 2 unmodified lines | ||
| 87 | 87 | */ |
| 88 | 88 | static void apparmor_cred_transfer(struct cred *new, const struct cred *old) |
| 89 | 89 | { |
| 90 | − cred_label(new) = aa_get_newest_label(cred_label(old)); | |
| 90 | + set_cred_label(new, aa_get_newest_label(cred_label(old))); | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | static void apparmor_task_free(struct task_struct *task) |
| 94 | 94 | { |
| 95 | 95 | |
| 96 | 96 | aa_free_task_ctx(task_ctx(task)); |
| 97 | − task_ctx(task) = NULL; | |
| 98 | 97 | } |
| 99 | 98 | |
| 100 | 99 | static int apparmor_task_alloc(struct task_struct *task, |
| 101 | 100 | unsigned long clone_flags) |
| 102 | 101 | { |
| 103 | − struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL); | |
| 104 | − | |
| 105 | − if (!new) | |
| 106 | − return -ENOMEM; | |
| 102 | + struct aa_task_ctx *new = task_ctx(task); | |
| 107 | 103 | |
| 108 | 104 | aa_dup_task_ctx(new, task_ctx(current)); |
| 109 | − task_ctx(task) = new; | |
| 110 | 105 | |
| 111 | 106 | return 0; |
| 112 | 107 | } |
| @@ -177,14 +172,14 @@ static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, · 64 unmodified lines | ||
| 177 | 172 | } |
| 178 | 173 | |
| 179 | 174 | static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, |
| 180 | − int cap, int audit) | |
| 175 | + int cap, unsigned int opts) | |
| 181 | 176 | { |
| 182 | 177 | struct aa_label *label; |
| 183 | 178 | int error = 0; |
| 184 | 179 | |
| 185 | 180 | label = aa_get_newest_cred_label(cred); |
| 186 | 181 | if (!unconfined(label)) |
| 187 | − error = aa_capable(label, cap, audit); | |
| 182 | + error = aa_capable(label, cap, opts); | |
| 188 | 183 | aa_put_label(label); |
| 189 | 184 | |
| 190 | 185 | return error; |
| @@ -434,21 +429,21 @@ static int apparmor_file_open(struct file *file) · 243 unmodified lines | ||
| 434 | 429 | |
| 435 | 430 | static int apparmor_file_alloc_security(struct file *file) |
| 436 | 431 | { |
| 437 | − int error = 0; | |
| 438 | − | |
| 439 | − /* freed by apparmor_file_free_security */ | |
| 432 | + struct aa_file_ctx *ctx = file_ctx(file); | |
| 440 | 433 | struct aa_label *label = begin_current_label_crit_section(); |
| 441 | − file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL); | |
| 442 | − if (!file_ctx(file)) | |
| 443 | − error = -ENOMEM; | |
| 444 | − end_current_label_crit_section(label); | |
| 445 | 434 | |
| 446 | − return error; | |
| 435 | + spin_lock_init(&ctx->lock); | |
| 436 | + rcu_assign_pointer(ctx->label, aa_get_label(label)); | |
| 437 | + end_current_label_crit_section(label); | |
| 438 | + return 0; | |
| 447 | 439 | } |
| 448 | 440 | |
| 449 | 441 | static void apparmor_file_free_security(struct file *file) |
| 450 | 442 | { |
| 451 | − aa_free_file_ctx(file_ctx(file)); | |
| 443 | + struct aa_file_ctx *ctx = file_ctx(file); | |
| 444 | + | |
| 445 | + if (ctx) | |
| 446 | + aa_put_label(rcu_access_pointer(ctx->label)); | |
| 452 | 447 | } |
| 453 | 448 | |
| 454 | 449 | static int common_file_perm(const char *op, struct file *file, u32 mask) |
| @@ -1151,6 +1146,15 @@ static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb, · 696 unmodified lines | ||
| 1151 | 1146 | } |
| 1152 | 1147 | #endif |
| 1153 | 1148 | |
| 1149 | +/* | |
| 1150 | + * The cred blob is a pointer to, not an instance of, an aa_task_ctx. | |
| 1151 | + */ | |
| 1152 | +struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { | |
| 1153 | + .lbs_cred = sizeof(struct aa_task_ctx *), | |
| 1154 | + .lbs_file = sizeof(struct aa_file_ctx), | |
| 1155 | + .lbs_task = sizeof(struct aa_task_ctx), | |
| 1156 | +}; | |
| 1157 | + | |
| 1154 | 1158 | static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
| 1155 | 1159 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
| 1156 | 1160 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), |
| @@ -1333,8 +1337,8 @@ bool aa_g_paranoid_load = true; · 176 unmodified lines | ||
| 1333 | 1337 | module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); |
| 1334 | 1338 | |
| 1335 | 1339 | /* Boot time disable flag */ |
| 1336 | −static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE; | |
| 1337 | −module_param_named(enabled, apparmor_enabled, bool, S_IRUGO); | |
| 1340 | +static int apparmor_enabled __lsm_ro_after_init = 1; | |
| 1341 | +module_param_named(enabled, apparmor_enabled, int, 0444); | |
| 1338 | 1342 | |
| 1339 | 1343 | static int __init apparmor_enabled_setup(char *str) |
| 1340 | 1344 | { |
| @@ -1479,14 +1483,8 @@ static int param_set_mode(const char *val, const struct kernel_param *kp) · 138 unmodified lines | ||
| 1479 | 1483 | static int __init set_init_ctx(void) |
| 1480 | 1484 | { |
| 1481 | 1485 | struct cred *cred = (struct cred *)current->real_cred; |
| 1482 | − struct aa_task_ctx *ctx; | |
| 1483 | − | |
| 1484 | − ctx = aa_alloc_task_ctx(GFP_KERNEL); | |
| 1485 | − if (!ctx) | |
| 1486 | − return -ENOMEM; | |
| 1487 | 1486 | |
| 1488 | − cred_label(cred) = aa_get_label(ns_unconfined(root_ns)); | |
| 1489 | − task_ctx(current) = ctx; | |
| 1487 | + set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); | |
| 1490 | 1488 | |
| 1491 | 1489 | return 0; |
| 1492 | 1490 | } |
| @@ -1665,12 +1663,6 @@ static int __init apparmor_init(void) · 172 unmodified lines | ||
| 1665 | 1663 | { |
| 1666 | 1664 | int error; |
| 1667 | 1665 | |
| 1668 | − if (!apparmor_enabled || !security_module_enable("apparmor")) { | |
| 1669 | − aa_info_message("AppArmor disabled by boot time parameter"); | |
| 1670 | − apparmor_enabled = false; | |
| 1671 | − return 0; | |
| 1672 | − } | |
| 1673 | − | |
| 1674 | 1666 | aa_secids_init(); |
| 1675 | 1667 | |
| 1676 | 1668 | error = aa_setup_dfa_engine(); |
| @@ -1731,5 +1723,8 @@ alloc_out: · 54 unmodified lines | ||
| 1731 | 1723 | |
| 1732 | 1724 | DEFINE_LSM(apparmor) = { |
| 1733 | 1725 | .name = "apparmor", |
| 1726 | + .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, | |
| 1727 | + .enabled = &apparmor_enabled, | |
| 1728 | + .blobs = &apparmor_blob_sizes, | |
| 1734 | 1729 | .init = apparmor_init, |
| 1735 | 1730 | }; |
| @@ -124,7 +124,7 @@ int aa_task_setrlimit(struct aa_label *label, struct task_struct *task, · 123 unmodified lines | ||
| 124 | 124 | */ |
| 125 | 125 | |
| 126 | 126 | if (label != peer && |
| 127 | − aa_capable(label, CAP_SYS_RESOURCE, SECURITY_CAP_NOAUDIT) != 0) | |
| 127 | + aa_capable(label, CAP_SYS_RESOURCE, CAP_OPT_NOAUDIT) != 0) | |
| 128 | 128 | error = fn_for_each(label, profile, |
| 129 | 129 | audit_resource(profile, resource, |
| 130 | 130 | new_rlim->rlim_max, peer, |
| @@ -81,7 +81,7 @@ int aa_replace_current_label(struct aa_label *label) · 80 unmodified lines | ||
| 81 | 81 | */ |
| 82 | 82 | aa_get_label(label); |
| 83 | 83 | aa_put_label(cred_label(new)); |
| 84 | − cred_label(new) = label; | |
| 84 | + set_cred_label(new, label); | |
| 85 | 85 | |
| 86 | 86 | commit_creds(new); |
| 87 | 87 | return 0; |
| @@ -138,7 +138,7 @@ int aa_set_current_hat(struct aa_label *label, u64 token) · 50 unmodified lines | ||
| 138 | 138 | return -EACCES; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | − cred_label(new) = aa_get_newest_label(label); | |
| 141 | + set_cred_label(new, aa_get_newest_label(label)); | |
| 142 | 142 | /* clear exec on switching context */ |
| 143 | 143 | aa_put_label(ctx->onexec); |
| 144 | 144 | ctx->onexec = NULL; |
| @@ -172,7 +172,7 @@ int aa_restore_previous_label(u64 token) · 27 unmodified lines | ||
| 172 | 172 | return -ENOMEM; |
| 173 | 173 | |
| 174 | 174 | aa_put_label(cred_label(new)); |
| 175 | − cred_label(new) = aa_get_newest_label(ctx->previous); | |
| 175 | + set_cred_label(new, aa_get_newest_label(ctx->previous)); | |
| 176 | 176 | AA_BUG(!cred_label(new)); |
| 177 | 177 | /* clear exec && prev information when restoring to previous context */ |
| 178 | 178 | aa_clear_task_ctx_trans(ctx); |
| @@ -57,7 +57,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname) · 56 unmodified lines | ||
| 57 | 57 | * @cred: The credentials to use |
| 58 | 58 | * @ns: The user namespace in which we need the capability |
| 59 | 59 | * @cap: The capability to check for |
| 60 | − * @audit: Whether to write an audit message or not | |
| 60 | + * @opts: Bitmask of options defined in include/linux/security.h | |
| 61 | 61 | * |
| 62 | 62 | * Determine whether the nominated task has the specified capability amongst |
| 63 | 63 | * its effective set, returning 0 if it does, -ve if it does not. |
| @@ -68,7 +68,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname) · 4 unmodified lines | ||
| 68 | 68 | * kernel's capable() and has_capability() returns 1 for this case. |
| 69 | 69 | */ |
| 70 | 70 | int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, |
| 71 | − int cap, int audit) | |
| 71 | + int cap, unsigned int opts) | |
| 72 | 72 | { |
| 73 | 73 | struct user_namespace *ns = targ_ns; |
| 74 | 74 | |
| @@ -222,12 +222,11 @@ int cap_capget(struct task_struct *target, kernel_cap_t *effective, · 147 unmodified lines | ||
| 222 | 222 | */ |
| 223 | 223 | static inline int cap_inh_is_capped(void) |
| 224 | 224 | { |
| 225 | − | |
| 226 | 225 | /* they are so limited unless the current task has the CAP_SETPCAP |
| 227 | 226 | * capability |
| 228 | 227 | */ |
| 229 | 228 | if (cap_capable(current_cred(), current_cred()->user_ns, |
| 230 | − CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0) | |
| 229 | + CAP_SETPCAP, CAP_OPT_NONE) == 0) | |
| 231 | 230 | return 0; |
| 232 | 231 | return 1; |
| 233 | 232 | } |
| @@ -1208,8 +1207,9 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, · 974 unmodified lines | ||
| 1208 | 1207 | || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/ |
| 1209 | 1208 | || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/ |
| 1210 | 1209 | || (cap_capable(current_cred(), |
| 1211 | − current_cred()->user_ns, CAP_SETPCAP, | |
| 1212 | − SECURITY_CAP_AUDIT) != 0) /*[4]*/ | |
| 1210 | + current_cred()->user_ns, | |
| 1211 | + CAP_SETPCAP, | |
| 1212 | + CAP_OPT_NONE) != 0) /*[4]*/ | |
| 1213 | 1213 | /* |
| 1214 | 1214 | * [1] no changing of bits that are locked |
| 1215 | 1215 | * [2] no unlocking of locks |
| @@ -1304,9 +1304,10 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages) · 88 unmodified lines | ||
| 1304 | 1304 | { |
| 1305 | 1305 | int cap_sys_admin = 0; |
| 1306 | 1306 | |
| 1307 | − if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, | |
| 1308 | − SECURITY_CAP_NOAUDIT) == 0) | |
| 1307 | + if (cap_capable(current_cred(), &init_user_ns, | |
| 1308 | + CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) == 0) | |
| 1309 | 1309 | cap_sys_admin = 1; |
| 1310 | + | |
| 1310 | 1311 | return cap_sys_admin; |
| 1311 | 1312 | } |
| 1312 | 1313 | |
| @@ -1325,7 +1326,7 @@ int cap_mmap_addr(unsigned long addr) · 12 unmodified lines | ||
| 1325 | 1326 | |
| 1326 | 1327 | if (addr < dac_mmap_min_addr) { |
| 1327 | 1328 | ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO, |
| 1328 | − SECURITY_CAP_AUDIT); | |
| 1329 | + CAP_OPT_NONE); | |
| 1329 | 1330 | /* set PF_SUPERPRIV if it turns out we allow the low mmap */ |
| 1330 | 1331 | if (ret == 0) |
| 1331 | 1332 | current->flags |= PF_SUPERPRIV; |
| @@ -1362,10 +1363,17 @@ struct security_hook_list capability_hooks[] __lsm_ro_after_init = { · 30 unmodified lines | ||
| 1362 | 1363 | LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory), |
| 1363 | 1364 | }; |
| 1364 | 1365 | |
| 1365 | −void __init capability_add_hooks(void) | |
| 1366 | +static int __init capability_init(void) | |
| 1366 | 1367 | { |
| 1367 | 1368 | security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks), |
| 1368 | 1369 | "capability"); |
| 1370 | + return 0; | |
| 1369 | 1371 | } |
| 1370 | 1372 | |
| 1373 | +DEFINE_LSM(capability) = { | |
| 1374 | + .name = "capability", | |
| 1375 | + .order = LSM_ORDER_FIRST, | |
| 1376 | + .init = capability_init, | |
| 1377 | +}; | |
| 1378 | + | |
| 1371 | 1379 | #endif /* CONFIG_SECURITY */ |
| @@ -114,6 +114,7 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint, · 113 unmodified lines | ||
| 114 | 114 | break; |
| 115 | 115 | case CREDS_CHECK: |
| 116 | 116 | iint->ima_creds_status = status; |
| 117 | + break; | |
| 117 | 118 | case FILE_CHECK: |
| 118 | 119 | case POST_SETATTR: |
| 119 | 120 | iint->ima_file_status = status; |
| @@ -938,10 +938,12 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) · 937 unmodified lines | ||
| 938 | 938 | case Opt_uid_gt: |
| 939 | 939 | case Opt_euid_gt: |
| 940 | 940 | entry->uid_op = &uid_gt; |
| 941 | + /* fall through */ | |
| 941 | 942 | case Opt_uid_lt: |
| 942 | 943 | case Opt_euid_lt: |
| 943 | 944 | if ((token == Opt_uid_lt) || (token == Opt_euid_lt)) |
| 944 | 945 | entry->uid_op = &uid_lt; |
| 946 | + /* fall through */ | |
| 945 | 947 | case Opt_uid_eq: |
| 946 | 948 | case Opt_euid_eq: |
| 947 | 949 | uid_token = (token == Opt_uid_eq) || |
| @@ -970,9 +972,11 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) · 22 unmodified lines | ||
| 970 | 972 | break; |
| 971 | 973 | case Opt_fowner_gt: |
| 972 | 974 | entry->fowner_op = &uid_gt; |
| 975 | + /* fall through */ | |
| 973 | 976 | case Opt_fowner_lt: |
| 974 | 977 | if (token == Opt_fowner_lt) |
| 975 | 978 | entry->fowner_op = &uid_lt; |
| 979 | + /* fall through */ | |
| 976 | 980 | case Opt_fowner_eq: |
| 977 | 981 | ima_log_string_op(ab, "fowner", args[0].from, |
| 978 | 982 | entry->fowner_op); |
| @@ -83,6 +83,7 @@ static void ima_show_template_data_ascii(struct seq_file *m, · 82 unmodified lines | ||
| 83 | 83 | /* skip ':' and '\0' */ |
| 84 | 84 | buf_ptr += 2; |
| 85 | 85 | buflen -= buf_ptr - field_data->data; |
| 86 | + /* fall through */ | |
| 86 | 87 | case DATA_FMT_DIGEST: |
| 87 | 88 | case DATA_FMT_HEX: |
| 88 | 89 | if (!buflen) |
| @@ -1752,7 +1752,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, · 1751 unmodified lines | ||
| 1752 | 1752 | return -EINVAL; |
| 1753 | 1753 | return keyctl_pkey_query((key_serial_t)arg2, |
| 1754 | 1754 | (const char __user *)arg4, |
| 1755 | − (struct keyctl_pkey_query *)arg5); | |
| 1755 | + (struct keyctl_pkey_query __user *)arg5); | |
| 1756 | 1756 | |
| 1757 | 1757 | case KEYCTL_PKEY_ENCRYPT: |
| 1758 | 1758 | case KEYCTL_PKEY_DECRYPT: |
| @@ -246,6 +246,7 @@ static unsigned long keyring_get_key_chunk(const void *data, int level) · 245 unmodified lines | ||
| 246 | 246 | (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8)); |
| 247 | 247 | n--; |
| 248 | 248 | offset = 1; |
| 249 | + /* fall through */ | |
| 249 | 250 | default: |
| 250 | 251 | offset += sizeof(chunk) - 1; |
| 251 | 252 | offset += (level - 3) * sizeof(chunk); |
| @@ -380,6 +380,7 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 379 unmodified lines | ||
| 380 | 380 | case -EAGAIN: /* no key */ |
| 381 | 381 | if (ret) |
| 382 | 382 | break; |
| 383 | + /* fall through */ | |
| 383 | 384 | case -ENOKEY: /* negative key */ |
| 384 | 385 | ret = key_ref; |
| 385 | 386 | break; |
| @@ -404,6 +405,7 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 18 unmodified lines | ||
| 404 | 405 | case -EAGAIN: /* no key */ |
| 405 | 406 | if (ret) |
| 406 | 407 | break; |
| 408 | + /* fall through */ | |
| 407 | 409 | case -ENOKEY: /* negative key */ |
| 408 | 410 | ret = key_ref; |
| 409 | 411 | break; |
| @@ -424,6 +426,7 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 14 unmodified lines | ||
| 424 | 426 | case -EAGAIN: /* no key */ |
| 425 | 427 | if (ret) |
| 426 | 428 | break; |
| 429 | + /* fall through */ | |
| 427 | 430 | case -ENOKEY: /* negative key */ |
| 428 | 431 | ret = key_ref; |
| 429 | 432 | break; |
| @@ -273,16 +273,19 @@ static int construct_get_dest_keyring(struct key **_dest_keyring) · 272 unmodified lines | ||
| 273 | 273 | } |
| 274 | 274 | } |
| 275 | 275 | |
| 276 | + /* fall through */ | |
| 276 | 277 | case KEY_REQKEY_DEFL_THREAD_KEYRING: |
| 277 | 278 | dest_keyring = key_get(cred->thread_keyring); |
| 278 | 279 | if (dest_keyring) |
| 279 | 280 | break; |
| 280 | 281 | |
| 282 | + /* fall through */ | |
| 281 | 283 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: |
| 282 | 284 | dest_keyring = key_get(cred->process_keyring); |
| 283 | 285 | if (dest_keyring) |
| 284 | 286 | break; |
| 285 | 287 | |
| 288 | + /* fall through */ | |
| 286 | 289 | case KEY_REQKEY_DEFL_SESSION_KEYRING: |
| 287 | 290 | rcu_read_lock(); |
| 288 | 291 | dest_keyring = key_get( |
| @@ -292,6 +295,7 @@ static int construct_get_dest_keyring(struct key **_dest_keyring) · 3 unmodified lines | ||
| 292 | 295 | if (dest_keyring) |
| 293 | 296 | break; |
| 294 | 297 | |
| 298 | + /* fall through */ | |
| 295 | 299 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: |
| 296 | 300 | dest_keyring = |
| 297 | 301 | key_get(cred->user->session_keyring); |
| @@ -187,13 +187,19 @@ static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = { · 186 unmodified lines | ||
| 187 | 187 | LSM_HOOK_INIT(kernel_load_data, loadpin_load_data), |
| 188 | 188 | }; |
| 189 | 189 | |
| 190 | −void __init loadpin_add_hooks(void) | |
| 190 | +static int __init loadpin_init(void) | |
| 191 | 191 | { |
| 192 | 192 | pr_info("ready to pin (currently %senforcing)\n", |
| 193 | 193 | enforce ? "" : "not "); |
| 194 | 194 | security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin"); |
| 195 | + return 0; | |
| 195 | 196 | } |
| 196 | 197 | |
| 198 | +DEFINE_LSM(loadpin) = { | |
| 199 | + .name = "loadpin", | |
| 200 | + .init = loadpin_init, | |
| 201 | +}; | |
| 202 | + | |
| 197 | 203 | /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */ |
| 198 | 204 | module_param(enforce, int, 0); |
| 199 | 205 | MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning"); |
| @@ -0,0 +1,14 @@ | ||
| 1 | +config SECURITY_SAFESETID | |
| 2 | + bool "Gate setid transitions to limit CAP_SET{U/G}ID capabilities" | |
| 3 | + depends on SECURITY | |
| 4 | + select SECURITYFS | |
| 5 | + default n | |
| 6 | + help | |
| 7 | + SafeSetID is an LSM module that gates the setid family of syscalls to | |
| 8 | + restrict UID/GID transitions from a given UID/GID to only those | |
| 9 | + approved by a system-wide whitelist. These restrictions also prohibit | |
| 10 | + the given UIDs/GIDs from obtaining auxiliary privileges associated | |
| 11 | + with CAP_SET{U/G}ID, such as allowing a user to set up user namespace | |
| 12 | + UID mappings. | |
| 13 | + | |
| 14 | + If you are unsure how to answer this question, answer N. | |
| @@ -0,0 +1,7 @@ | ||
| 1 | +# SPDX-License-Identifier: GPL-2.0 | |
| 2 | +# | |
| 3 | +# Makefile for the safesetid LSM. | |
| 4 | +# | |
| 5 | + | |
| 6 | +obj-$(CONFIG_SECURITY_SAFESETID) := safesetid.o | |
| 7 | +safesetid-y := lsm.o securityfs.o | |
| @@ -0,0 +1,277 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +/* | |
| 3 | + * SafeSetID Linux Security Module | |
| 4 | + * | |
| 5 | + * Author: Micah Morton <mortonm@chromium.org> | |
| 6 | + * | |
| 7 | + * Copyright (C) 2018 The Chromium OS Authors. | |
| 8 | + * | |
| 9 | + * This program is free software; you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License version 2, as | |
| 11 | + * published by the Free Software Foundation. | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +#define pr_fmt(fmt) "SafeSetID: " fmt | |
| 16 | + | |
| 17 | +#include <linux/hashtable.h> | |
| 18 | +#include <linux/lsm_hooks.h> | |
| 19 | +#include <linux/module.h> | |
| 20 | +#include <linux/ptrace.h> | |
| 21 | +#include <linux/sched/task_stack.h> | |
| 22 | +#include <linux/security.h> | |
| 23 | + | |
| 24 | +/* Flag indicating whether initialization completed */ | |
| 25 | +int safesetid_initialized; | |
| 26 | + | |
| 27 | +#define NUM_BITS 8 /* 128 buckets in hash table */ | |
| 28 | + | |
| 29 | +static DEFINE_HASHTABLE(safesetid_whitelist_hashtable, NUM_BITS); | |
| 30 | + | |
| 31 | +/* | |
| 32 | + * Hash table entry to store safesetid policy signifying that 'parent' user | |
| 33 | + * can setid to 'child' user. | |
| 34 | + */ | |
| 35 | +struct entry { | |
| 36 | + struct hlist_node next; | |
| 37 | + struct hlist_node dlist; /* for deletion cleanup */ | |
| 38 | + uint64_t parent_kuid; | |
| 39 | + uint64_t child_kuid; | |
| 40 | +}; | |
| 41 | + | |
| 42 | +static DEFINE_SPINLOCK(safesetid_whitelist_hashtable_spinlock); | |
| 43 | + | |
| 44 | +static bool check_setuid_policy_hashtable_key(kuid_t parent) | |
| 45 | +{ | |
| 46 | + struct entry *entry; | |
| 47 | + | |
| 48 | + rcu_read_lock(); | |
| 49 | + hash_for_each_possible_rcu(safesetid_whitelist_hashtable, | |
| 50 | + entry, next, __kuid_val(parent)) { | |
| 51 | + if (entry->parent_kuid == __kuid_val(parent)) { | |
| 52 | + rcu_read_unlock(); | |
| 53 | + return true; | |
| 54 | + } | |
| 55 | + } | |
| 56 | + rcu_read_unlock(); | |
| 57 | + | |
| 58 | + return false; | |
| 59 | +} | |
| 60 | + | |
| 61 | +static bool check_setuid_policy_hashtable_key_value(kuid_t parent, | |
| 62 | + kuid_t child) | |
| 63 | +{ | |
| 64 | + struct entry *entry; | |
| 65 | + | |
| 66 | + rcu_read_lock(); | |
| 67 | + hash_for_each_possible_rcu(safesetid_whitelist_hashtable, | |
| 68 | + entry, next, __kuid_val(parent)) { | |
| 69 | + if (entry->parent_kuid == __kuid_val(parent) && | |
| 70 | + entry->child_kuid == __kuid_val(child)) { | |
| 71 | + rcu_read_unlock(); | |
| 72 | + return true; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + rcu_read_unlock(); | |
| 76 | + | |
| 77 | + return false; | |
| 78 | +} | |
| 79 | + | |
| 80 | +static int safesetid_security_capable(const struct cred *cred, | |
| 81 | + struct user_namespace *ns, | |
| 82 | + int cap, | |
| 83 | + unsigned int opts) | |
| 84 | +{ | |
| 85 | + if (cap == CAP_SETUID && | |
| 86 | + check_setuid_policy_hashtable_key(cred->uid)) { | |
| 87 | + if (!(opts & CAP_OPT_INSETID)) { | |
| 88 | + /* | |
| 89 | + * Deny if we're not in a set*uid() syscall to avoid | |
| 90 | + * giving powers gated by CAP_SETUID that are related | |
| 91 | + * to functionality other than calling set*uid() (e.g. | |
| 92 | + * allowing user to set up userns uid mappings). | |
| 93 | + */ | |
| 94 | + pr_warn("Operation requires CAP_SETUID, which is not available to UID %u for operations besides approved set*uid transitions", | |
| 95 | + __kuid_val(cred->uid)); | |
| 96 | + return -1; | |
| 97 | + } | |
| 98 | + } | |
| 99 | + return 0; | |
| 100 | +} | |
| 101 | + | |
| 102 | +static int check_uid_transition(kuid_t parent, kuid_t child) | |
| 103 | +{ | |
| 104 | + if (check_setuid_policy_hashtable_key_value(parent, child)) | |
| 105 | + return 0; | |
| 106 | + pr_warn("UID transition (%d -> %d) blocked", | |
| 107 | + __kuid_val(parent), | |
| 108 | + __kuid_val(child)); | |
| 109 | + /* | |
| 110 | + * Kill this process to avoid potential security vulnerabilities | |
| 111 | + * that could arise from a missing whitelist entry preventing a | |
| 112 | + * privileged process from dropping to a lesser-privileged one. | |
| 113 | + */ | |
| 114 | + force_sig(SIGKILL, current); | |
| 115 | + return -EACCES; | |
| 116 | +} | |
| 117 | + | |
| 118 | +/* | |
| 119 | + * Check whether there is either an exception for user under old cred struct to | |
| 120 | + * set*uid to user under new cred struct, or the UID transition is allowed (by | |
| 121 | + * Linux set*uid rules) even without CAP_SETUID. | |
| 122 | + */ | |
| 123 | +static int safesetid_task_fix_setuid(struct cred *new, | |
| 124 | + const struct cred *old, | |
| 125 | + int flags) | |
| 126 | +{ | |
| 127 | + | |
| 128 | + /* Do nothing if there are no setuid restrictions for this UID. */ | |
| 129 | + if (!check_setuid_policy_hashtable_key(old->uid)) | |
| 130 | + return 0; | |
| 131 | + | |
| 132 | + switch (flags) { | |
| 133 | + case LSM_SETID_RE: | |
| 134 | + /* | |
| 135 | + * Users for which setuid restrictions exist can only set the | |
| 136 | + * real UID to the real UID or the effective UID, unless an | |
| 137 | + * explicit whitelist policy allows the transition. | |
| 138 | + */ | |
| 139 | + if (!uid_eq(old->uid, new->uid) && | |
| 140 | + !uid_eq(old->euid, new->uid)) { | |
| 141 | + return check_uid_transition(old->uid, new->uid); | |
| 142 | + } | |
| 143 | + /* | |
| 144 | + * Users for which setuid restrictions exist can only set the | |
| 145 | + * effective UID to the real UID, the effective UID, or the | |
| 146 | + * saved set-UID, unless an explicit whitelist policy allows | |
| 147 | + * the transition. | |
| 148 | + */ | |
| 149 | + if (!uid_eq(old->uid, new->euid) && | |
| 150 | + !uid_eq(old->euid, new->euid) && | |
| 151 | + !uid_eq(old->suid, new->euid)) { | |
| 152 | + return check_uid_transition(old->euid, new->euid); | |
| 153 | + } | |
| 154 | + break; | |
| 155 | + case LSM_SETID_ID: | |
| 156 | + /* | |
| 157 | + * Users for which setuid restrictions exist cannot change the | |
| 158 | + * real UID or saved set-UID unless an explicit whitelist | |
| 159 | + * policy allows the transition. | |
| 160 | + */ | |
| 161 | + if (!uid_eq(old->uid, new->uid)) | |
| 162 | + return check_uid_transition(old->uid, new->uid); | |
| 163 | + if (!uid_eq(old->suid, new->suid)) | |
| 164 | + return check_uid_transition(old->suid, new->suid); | |
| 165 | + break; | |
| 166 | + case LSM_SETID_RES: | |
| 167 | + /* | |
| 168 | + * Users for which setuid restrictions exist cannot change the | |
| 169 | + * real UID, effective UID, or saved set-UID to anything but | |
| 170 | + * one of: the current real UID, the current effective UID or | |
| 171 | + * the current saved set-user-ID unless an explicit whitelist | |
| 172 | + * policy allows the transition. | |
| 173 | + */ | |
| 174 | + if (!uid_eq(new->uid, old->uid) && | |
| 175 | + !uid_eq(new->uid, old->euid) && | |
| 176 | + !uid_eq(new->uid, old->suid)) { | |
| 177 | + return check_uid_transition(old->uid, new->uid); | |
| 178 | + } | |
| 179 | + if (!uid_eq(new->euid, old->uid) && | |
| 180 | + !uid_eq(new->euid, old->euid) && | |
| 181 | + !uid_eq(new->euid, old->suid)) { | |
| 182 | + return check_uid_transition(old->euid, new->euid); | |
| 183 | + } | |
| 184 | + if (!uid_eq(new->suid, old->uid) && | |
| 185 | + !uid_eq(new->suid, old->euid) && | |
| 186 | + !uid_eq(new->suid, old->suid)) { | |
| 187 | + return check_uid_transition(old->suid, new->suid); | |
| 188 | + } | |
| 189 | + break; | |
| 190 | + case LSM_SETID_FS: | |
| 191 | + /* | |
| 192 | + * Users for which setuid restrictions exist cannot change the | |
| 193 | + * filesystem UID to anything but one of: the current real UID, | |
| 194 | + * the current effective UID or the current saved set-UID | |
| 195 | + * unless an explicit whitelist policy allows the transition. | |
| 196 | + */ | |
| 197 | + if (!uid_eq(new->fsuid, old->uid) && | |
| 198 | + !uid_eq(new->fsuid, old->euid) && | |
| 199 | + !uid_eq(new->fsuid, old->suid) && | |
| 200 | + !uid_eq(new->fsuid, old->fsuid)) { | |
| 201 | + return check_uid_transition(old->fsuid, new->fsuid); | |
| 202 | + } | |
| 203 | + break; | |
| 204 | + default: | |
| 205 | + pr_warn("Unknown setid state %d\n", flags); | |
| 206 | + force_sig(SIGKILL, current); | |
| 207 | + return -EINVAL; | |
| 208 | + } | |
| 209 | + return 0; | |
| 210 | +} | |
| 211 | + | |
| 212 | +int add_safesetid_whitelist_entry(kuid_t parent, kuid_t child) | |
| 213 | +{ | |
| 214 | + struct entry *new; | |
| 215 | + | |
| 216 | + /* Return if entry already exists */ | |
| 217 | + if (check_setuid_policy_hashtable_key_value(parent, child)) | |
| 218 | + return 0; | |
| 219 | + | |
| 220 | + new = kzalloc(sizeof(struct entry), GFP_KERNEL); | |
| 221 | + if (!new) | |
| 222 | + return -ENOMEM; | |
| 223 | + new->parent_kuid = __kuid_val(parent); | |
| 224 | + new->child_kuid = __kuid_val(child); | |
| 225 | + spin_lock(&safesetid_whitelist_hashtable_spinlock); | |
| 226 | + hash_add_rcu(safesetid_whitelist_hashtable, | |
| 227 | + &new->next, | |
| 228 | + __kuid_val(parent)); | |
| 229 | + spin_unlock(&safesetid_whitelist_hashtable_spinlock); | |
| 230 | + return 0; | |
| 231 | +} | |
| 232 | + | |
| 233 | +void flush_safesetid_whitelist_entries(void) | |
| 234 | +{ | |
| 235 | + struct entry *entry; | |
| 236 | + struct hlist_node *hlist_node; | |
| 237 | + unsigned int bkt_loop_cursor; | |
| 238 | + HLIST_HEAD(free_list); | |
| 239 | + | |
| 240 | + /* | |
| 241 | + * Could probably use hash_for_each_rcu here instead, but this should | |
| 242 | + * be fine as well. | |
| 243 | + */ | |
| 244 | + spin_lock(&safesetid_whitelist_hashtable_spinlock); | |
| 245 | + hash_for_each_safe(safesetid_whitelist_hashtable, bkt_loop_cursor, | |
| 246 | + hlist_node, entry, next) { | |
| 247 | + hash_del_rcu(&entry->next); | |
| 248 | + hlist_add_head(&entry->dlist, &free_list); | |
| 249 | + } | |
| 250 | + spin_unlock(&safesetid_whitelist_hashtable_spinlock); | |
| 251 | + synchronize_rcu(); | |
| 252 | + hlist_for_each_entry_safe(entry, hlist_node, &free_list, dlist) { | |
| 253 | + hlist_del(&entry->dlist); | |
| 254 | + kfree(entry); | |
| 255 | + } | |
| 256 | +} | |
| 257 | + | |
| 258 | +static struct security_hook_list safesetid_security_hooks[] = { | |
| 259 | + LSM_HOOK_INIT(task_fix_setuid, safesetid_task_fix_setuid), | |
| 260 | + LSM_HOOK_INIT(capable, safesetid_security_capable) | |
| 261 | +}; | |
| 262 | + | |
| 263 | +static int __init safesetid_security_init(void) | |
| 264 | +{ | |
| 265 | + security_add_hooks(safesetid_security_hooks, | |
| 266 | + ARRAY_SIZE(safesetid_security_hooks), "safesetid"); | |
| 267 | + | |
| 268 | + /* Report that SafeSetID successfully initialized */ | |
| 269 | + safesetid_initialized = 1; | |
| 270 | + | |
| 271 | + return 0; | |
| 272 | +} | |
| 273 | + | |
| 274 | +DEFINE_LSM(safesetid_security_init) = { | |
| 275 | + .init = safesetid_security_init, | |
| 276 | + .name = "safesetid", | |
| 277 | +}; | |
| @@ -0,0 +1,33 @@ | ||
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ | |
| 2 | +/* | |
| 3 | + * SafeSetID Linux Security Module | |
| 4 | + * | |
| 5 | + * Author: Micah Morton <mortonm@chromium.org> | |
| 6 | + * | |
| 7 | + * Copyright (C) 2018 The Chromium OS Authors. | |
| 8 | + * | |
| 9 | + * This program is free software; you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License version 2, as | |
| 11 | + * published by the Free Software Foundation. | |
| 12 | + * | |
| 13 | + */ | |
| 14 | +#ifndef _SAFESETID_H | |
| 15 | +#define _SAFESETID_H | |
| 16 | + | |
| 17 | +#include <linux/types.h> | |
| 18 | + | |
| 19 | +/* Flag indicating whether initialization completed */ | |
| 20 | +extern int safesetid_initialized; | |
| 21 | + | |
| 22 | +/* Function type. */ | |
| 23 | +enum safesetid_whitelist_file_write_type { | |
| 24 | + SAFESETID_WHITELIST_ADD, /* Add whitelist policy. */ | |
| 25 | + SAFESETID_WHITELIST_FLUSH, /* Flush whitelist policies. */ | |
| 26 | +}; | |
| 27 | + | |
| 28 | +/* Add entry to safesetid whitelist to allow 'parent' to setid to 'child'. */ | |
| 29 | +int add_safesetid_whitelist_entry(kuid_t parent, kuid_t child); | |
| 30 | + | |
| 31 | +void flush_safesetid_whitelist_entries(void); | |
| 32 | + | |
| 33 | +#endif /* _SAFESETID_H */ | |
| @@ -0,0 +1,193 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +/* | |
| 3 | + * SafeSetID Linux Security Module | |
| 4 | + * | |
| 5 | + * Author: Micah Morton <mortonm@chromium.org> | |
| 6 | + * | |
| 7 | + * Copyright (C) 2018 The Chromium OS Authors. | |
| 8 | + * | |
| 9 | + * This program is free software; you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License version 2, as | |
| 11 | + * published by the Free Software Foundation. | |
| 12 | + * | |
| 13 | + */ | |
| 14 | +#include <linux/security.h> | |
| 15 | +#include <linux/cred.h> | |
| 16 | + | |
| 17 | +#include "lsm.h" | |
| 18 | + | |
| 19 | +static struct dentry *safesetid_policy_dir; | |
| 20 | + | |
| 21 | +struct safesetid_file_entry { | |
| 22 | + const char *name; | |
| 23 | + enum safesetid_whitelist_file_write_type type; | |
| 24 | + struct dentry *dentry; | |
| 25 | +}; | |
| 26 | + | |
| 27 | +static struct safesetid_file_entry safesetid_files[] = { | |
| 28 | + {.name = "add_whitelist_policy", | |
| 29 | + .type = SAFESETID_WHITELIST_ADD}, | |
| 30 | + {.name = "flush_whitelist_policies", | |
| 31 | + .type = SAFESETID_WHITELIST_FLUSH}, | |
| 32 | +}; | |
| 33 | + | |
| 34 | +/* | |
| 35 | + * In the case the input buffer contains one or more invalid UIDs, the kuid_t | |
| 36 | + * variables pointed to by 'parent' and 'child' will get updated but this | |
| 37 | + * function will return an error. | |
| 38 | + */ | |
| 39 | +static int parse_safesetid_whitelist_policy(const char __user *buf, | |
| 40 | + size_t len, | |
| 41 | + kuid_t *parent, | |
| 42 | + kuid_t *child) | |
| 43 | +{ | |
| 44 | + char *kern_buf; | |
| 45 | + char *parent_buf; | |
| 46 | + char *child_buf; | |
| 47 | + const char separator[] = ":"; | |
| 48 | + int ret; | |
| 49 | + size_t first_substring_length; | |
| 50 | + long parsed_parent; | |
| 51 | + long parsed_child; | |
| 52 | + | |
| 53 | + /* Duplicate string from user memory and NULL-terminate */ | |
| 54 | + kern_buf = memdup_user_nul(buf, len); | |
| 55 | + if (IS_ERR(kern_buf)) | |
| 56 | + return PTR_ERR(kern_buf); | |
| 57 | + | |
| 58 | + /* | |
| 59 | + * Format of |buf| string should be <UID>:<UID>. | |
| 60 | + * Find location of ":" in kern_buf (copied from |buf|). | |
| 61 | + */ | |
| 62 | + first_substring_length = strcspn(kern_buf, separator); | |
| 63 | + if (first_substring_length == 0 || first_substring_length == len) { | |
| 64 | + ret = -EINVAL; | |
| 65 | + goto free_kern; | |
| 66 | + } | |
| 67 | + | |
| 68 | + parent_buf = kmemdup_nul(kern_buf, first_substring_length, GFP_KERNEL); | |
| 69 | + if (!parent_buf) { | |
| 70 | + ret = -ENOMEM; | |
| 71 | + goto free_kern; | |
| 72 | + } | |
| 73 | + | |
| 74 | + ret = kstrtol(parent_buf, 0, &parsed_parent); | |
| 75 | + if (ret) | |
| 76 | + goto free_both; | |
| 77 | + | |
| 78 | + child_buf = kern_buf + first_substring_length + 1; | |
| 79 | + ret = kstrtol(child_buf, 0, &parsed_child); | |
| 80 | + if (ret) | |
| 81 | + goto free_both; | |
| 82 | + | |
| 83 | + *parent = make_kuid(current_user_ns(), parsed_parent); | |
| 84 | + if (!uid_valid(*parent)) { | |
| 85 | + ret = -EINVAL; | |
| 86 | + goto free_both; | |
| 87 | + } | |
| 88 | + | |
| 89 | + *child = make_kuid(current_user_ns(), parsed_child); | |
| 90 | + if (!uid_valid(*child)) { | |
| 91 | + ret = -EINVAL; | |
| 92 | + goto free_both; | |
| 93 | + } | |
| 94 | + | |
| 95 | +free_both: | |
| 96 | + kfree(parent_buf); | |
| 97 | +free_kern: | |
| 98 | + kfree(kern_buf); | |
| 99 | + return ret; | |
| 100 | +} | |
| 101 | + | |
| 102 | +static ssize_t safesetid_file_write(struct file *file, | |
| 103 | + const char __user *buf, | |
| 104 | + size_t len, | |
| 105 | + loff_t *ppos) | |
| 106 | +{ | |
| 107 | + struct safesetid_file_entry *file_entry = | |
| 108 | + file->f_inode->i_private; | |
| 109 | + kuid_t parent; | |
| 110 | + kuid_t child; | |
| 111 | + int ret; | |
| 112 | + | |
| 113 | + if (!ns_capable(current_user_ns(), CAP_MAC_ADMIN)) | |
| 114 | + return -EPERM; | |
| 115 | + | |
| 116 | + if (*ppos != 0) | |
| 117 | + return -EINVAL; | |
| 118 | + | |
| 119 | + switch (file_entry->type) { | |
| 120 | + case SAFESETID_WHITELIST_FLUSH: | |
| 121 | + flush_safesetid_whitelist_entries(); | |
| 122 | + break; | |
| 123 | + case SAFESETID_WHITELIST_ADD: | |
| 124 | + ret = parse_safesetid_whitelist_policy(buf, len, &parent, | |
| 125 | + &child); | |
| 126 | + if (ret) | |
| 127 | + return ret; | |
| 128 | + | |
| 129 | + ret = add_safesetid_whitelist_entry(parent, child); | |
| 130 | + if (ret) | |
| 131 | + return ret; | |
| 132 | + break; | |
| 133 | + default: | |
| 134 | + pr_warn("Unknown securityfs file %d\n", file_entry->type); | |
| 135 | + break; | |
| 136 | + } | |
| 137 | + | |
| 138 | + /* Return len on success so caller won't keep trying to write */ | |
| 139 | + return len; | |
| 140 | +} | |
| 141 | + | |
| 142 | +static const struct file_operations safesetid_file_fops = { | |
| 143 | + .write = safesetid_file_write, | |
| 144 | +}; | |
| 145 | + | |
| 146 | +static void safesetid_shutdown_securityfs(void) | |
| 147 | +{ | |
| 148 | + int i; | |
| 149 | + | |
| 150 | + for (i = 0; i < ARRAY_SIZE(safesetid_files); ++i) { | |
| 151 | + struct safesetid_file_entry *entry = | |
| 152 | + &safesetid_files[i]; | |
| 153 | + securityfs_remove(entry->dentry); | |
| 154 | + entry->dentry = NULL; | |
| 155 | + } | |
| 156 | + | |
| 157 | + securityfs_remove(safesetid_policy_dir); | |
| 158 | + safesetid_policy_dir = NULL; | |
| 159 | +} | |
| 160 | + | |
| 161 | +static int __init safesetid_init_securityfs(void) | |
| 162 | +{ | |
| 163 | + int i; | |
| 164 | + int ret; | |
| 165 | + | |
| 166 | + if (!safesetid_initialized) | |
| 167 | + return 0; | |
| 168 | + | |
| 169 | + safesetid_policy_dir = securityfs_create_dir("safesetid", NULL); | |
| 170 | + if (IS_ERR(safesetid_policy_dir)) { | |
| 171 | + ret = PTR_ERR(safesetid_policy_dir); | |
| 172 | + goto error; | |
| 173 | + } | |
| 174 | + | |
| 175 | + for (i = 0; i < ARRAY_SIZE(safesetid_files); ++i) { | |
| 176 | + struct safesetid_file_entry *entry = | |
| 177 | + &safesetid_files[i]; | |
| 178 | + entry->dentry = securityfs_create_file( | |
| 179 | + entry->name, 0200, safesetid_policy_dir, | |
| 180 | + entry, &safesetid_file_fops); | |
| 181 | + if (IS_ERR(entry->dentry)) { | |
| 182 | + ret = PTR_ERR(entry->dentry); | |
| 183 | + goto error; | |
| 184 | + } | |
| 185 | + } | |
| 186 | + | |
| 187 | + return 0; | |
| 188 | + | |
| 189 | +error: | |
| 190 | + safesetid_shutdown_securityfs(); | |
| 191 | + return ret; | |
| 192 | +} | |
| 193 | +fs_initcall(safesetid_init_securityfs); | |
| @@ -30,20 +30,32 @@ · 29 unmodified lines | ||
| 30 | 30 | #include <linux/personality.h> |
| 31 | 31 | #include <linux/backing-dev.h> |
| 32 | 32 | #include <linux/string.h> |
| 33 | +#include <linux/msg.h> | |
| 33 | 34 | #include <net/flow.h> |
| 34 | 35 | |
| 35 | 36 | #define MAX_LSM_EVM_XATTR 2 |
| 36 | 37 | |
| 37 | −/* Maximum number of letters for an LSM name string */ | |
| 38 | −#define SECURITY_NAME_MAX 10 | |
| 38 | +/* How many LSMs were built into the kernel? */ | |
| 39 | +#define LSM_COUNT (__end_lsm_info - __start_lsm_info) | |
| 39 | 40 | |
| 40 | 41 | struct security_hook_heads security_hook_heads __lsm_ro_after_init; |
| 41 | 42 | static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain); |
| 42 | 43 | |
| 44 | +static struct kmem_cache *lsm_file_cache; | |
| 45 | +static struct kmem_cache *lsm_inode_cache; | |
| 46 | + | |
| 43 | 47 | char *lsm_names; |
| 48 | +static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init; | |
| 49 | + | |
| 44 | 50 | /* Boot-time LSM user choice */ |
| 45 | −static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] = | |
| 46 | − CONFIG_DEFAULT_SECURITY; | |
| 51 | +static __initdata const char *chosen_lsm_order; | |
| 52 | +static __initdata const char *chosen_major_lsm; | |
| 53 | + | |
| 54 | +static __initconst const char * const builtin_lsm_order = CONFIG_LSM; | |
| 55 | + | |
| 56 | +/* Ordered list of LSMs to initialize. */ | |
| 57 | +static __initdata struct lsm_info **ordered_lsms; | |
| 58 | +static __initdata struct lsm_info *exclusive; | |
| 47 | 59 | |
| 48 | 60 | static __initdata bool debug; |
| 49 | 61 | #define init_debug(...) \ |
| @@ -52,18 +64,269 @@ static __initdata bool debug; · 2 unmodified lines | ||
| 52 | 64 | pr_info(__VA_ARGS__); \ |
| 53 | 65 | } while (0) |
| 54 | 66 | |
| 55 | −static void __init major_lsm_init(void) | |
| 67 | +static bool __init is_enabled(struct lsm_info *lsm) | |
| 56 | 68 | { |
| 57 | − struct lsm_info *lsm; | |
| 58 | − int ret; | |
| 69 | + if (!lsm->enabled) | |
| 70 | + return false; | |
| 71 | + | |
| 72 | + return *lsm->enabled; | |
| 73 | +} | |
| 74 | + | |
| 75 | +/* Mark an LSM's enabled flag. */ | |
| 76 | +static int lsm_enabled_true __initdata = 1; | |
| 77 | +static int lsm_enabled_false __initdata = 0; | |
| 78 | +static void __init set_enabled(struct lsm_info *lsm, bool enabled) | |
| 79 | +{ | |
| 80 | + /* | |
| 81 | + * When an LSM hasn't configured an enable variable, we can use | |
| 82 | + * a hard-coded location for storing the default enabled state. | |
| 83 | + */ | |
| 84 | + if (!lsm->enabled) { | |
| 85 | + if (enabled) | |
| 86 | + lsm->enabled = &lsm_enabled_true; | |
| 87 | + else | |
| 88 | + lsm->enabled = &lsm_enabled_false; | |
| 89 | + } else if (lsm->enabled == &lsm_enabled_true) { | |
| 90 | + if (!enabled) | |
| 91 | + lsm->enabled = &lsm_enabled_false; | |
| 92 | + } else if (lsm->enabled == &lsm_enabled_false) { | |
| 93 | + if (enabled) | |
| 94 | + lsm->enabled = &lsm_enabled_true; | |
| 95 | + } else { | |
| 96 | + *lsm->enabled = enabled; | |
| 97 | + } | |
| 98 | +} | |
| 99 | + | |
| 100 | +/* Is an LSM already listed in the ordered LSMs list? */ | |
| 101 | +static bool __init exists_ordered_lsm(struct lsm_info *lsm) | |
| 102 | +{ | |
| 103 | + struct lsm_info **check; | |
| 104 | + | |
| 105 | + for (check = ordered_lsms; *check; check++) | |
| 106 | + if (*check == lsm) | |
| 107 | + return true; | |
| 108 | + | |
| 109 | + return false; | |
| 110 | +} | |
| 111 | + | |
| 112 | +/* Append an LSM to the list of ordered LSMs to initialize. */ | |
| 113 | +static int last_lsm __initdata; | |
| 114 | +static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from) | |
| 115 | +{ | |
| 116 | + /* Ignore duplicate selections. */ | |
| 117 | + if (exists_ordered_lsm(lsm)) | |
| 118 | + return; | |
| 119 | + | |
| 120 | + if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from)) | |
| 121 | + return; | |
| 122 | + | |
| 123 | + /* Enable this LSM, if it is not already set. */ | |
| 124 | + if (!lsm->enabled) | |
| 125 | + lsm->enabled = &lsm_enabled_true; | |
| 126 | + ordered_lsms[last_lsm++] = lsm; | |
| 127 | + | |
| 128 | + init_debug("%s ordering: %s (%sabled)\n", from, lsm->name, | |
| 129 | + is_enabled(lsm) ? "en" : "dis"); | |
| 130 | +} | |
| 131 | + | |
| 132 | +/* Is an LSM allowed to be initialized? */ | |
| 133 | +static bool __init lsm_allowed(struct lsm_info *lsm) | |
| 134 | +{ | |
| 135 | + /* Skip if the LSM is disabled. */ | |
| 136 | + if (!is_enabled(lsm)) | |
| 137 | + return false; | |
| 138 | + | |
| 139 | + /* Not allowed if another exclusive LSM already initialized. */ | |
| 140 | + if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) { | |
| 141 | + init_debug("exclusive disabled: %s\n", lsm->name); | |
| 142 | + return false; | |
| 143 | + } | |
| 144 | + | |
| 145 | + return true; | |
| 146 | +} | |
| 147 | + | |
| 148 | +static void __init lsm_set_blob_size(int *need, int *lbs) | |
| 149 | +{ | |
| 150 | + int offset; | |
| 151 | + | |
| 152 | + if (*need > 0) { | |
| 153 | + offset = *lbs; | |
| 154 | + *lbs += *need; | |
| 155 | + *need = offset; | |
| 156 | + } | |
| 157 | +} | |
| 158 | + | |
| 159 | +static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed) | |
| 160 | +{ | |
| 161 | + if (!needed) | |
| 162 | + return; | |
| 163 | + | |
| 164 | + lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred); | |
| 165 | + lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file); | |
| 166 | + /* | |
| 167 | + * The inode blob gets an rcu_head in addition to | |
| 168 | + * what the modules might need. | |
| 169 | + */ | |
| 170 | + if (needed->lbs_inode && blob_sizes.lbs_inode == 0) | |
| 171 | + blob_sizes.lbs_inode = sizeof(struct rcu_head); | |
| 172 | + lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode); | |
| 173 | + lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc); | |
| 174 | + lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); | |
| 175 | + lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task); | |
| 176 | +} | |
| 177 | + | |
| 178 | +/* Prepare LSM for initialization. */ | |
| 179 | +static void __init prepare_lsm(struct lsm_info *lsm) | |
| 180 | +{ | |
| 181 | + int enabled = lsm_allowed(lsm); | |
| 182 | + | |
| 183 | + /* Record enablement (to handle any following exclusive LSMs). */ | |
| 184 | + set_enabled(lsm, enabled); | |
| 185 | + | |
| 186 | + /* If enabled, do pre-initialization work. */ | |
| 187 | + if (enabled) { | |
| 188 | + if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) { | |
| 189 | + exclusive = lsm; | |
| 190 | + init_debug("exclusive chosen: %s\n", lsm->name); | |
| 191 | + } | |
| 192 | + | |
| 193 | + lsm_set_blob_sizes(lsm->blobs); | |
| 194 | + } | |
| 195 | +} | |
| 196 | + | |
| 197 | +/* Initialize a given LSM, if it is enabled. */ | |
| 198 | +static void __init initialize_lsm(struct lsm_info *lsm) | |
| 199 | +{ | |
| 200 | + if (is_enabled(lsm)) { | |
| 201 | + int ret; | |
| 59 | 202 | |
| 60 | − for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { | |
| 61 | 203 | init_debug("initializing %s\n", lsm->name); |
| 62 | 204 | ret = lsm->init(); |
| 63 | 205 | WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret); |
| 64 | 206 | } |
| 65 | 207 | } |
| 66 | 208 | |
| 209 | +/* Populate ordered LSMs list from comma-separated LSM name list. */ | |
| 210 | +static void __init ordered_lsm_parse(const char *order, const char *origin) | |
| 211 | +{ | |
| 212 | + struct lsm_info *lsm; | |
| 213 | + char *sep, *name, *next; | |
| 214 | + | |
| 215 | + /* LSM_ORDER_FIRST is always first. */ | |
| 216 | + for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { | |
| 217 | + if (lsm->order == LSM_ORDER_FIRST) | |
| 218 | + append_ordered_lsm(lsm, "first"); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /* Process "security=", if given. */ | |
| 222 | + if (chosen_major_lsm) { | |
| 223 | + struct lsm_info *major; | |
| 224 | + | |
| 225 | + /* | |
| 226 | + * To match the original "security=" behavior, this | |
| 227 | + * explicitly does NOT fallback to another Legacy Major | |
| 228 | + * if the selected one was separately disabled: disable | |
| 229 | + * all non-matching Legacy Major LSMs. | |
| 230 | + */ | |
| 231 | + for (major = __start_lsm_info; major < __end_lsm_info; | |
| 232 | + major++) { | |
| 233 | + if ((major->flags & LSM_FLAG_LEGACY_MAJOR) && | |
| 234 | + strcmp(major->name, chosen_major_lsm) != 0) { | |
| 235 | + set_enabled(major, false); | |
| 236 | + init_debug("security=%s disabled: %s\n", | |
| 237 | + chosen_major_lsm, major->name); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + } | |
| 241 | + | |
| 242 | + sep = kstrdup(order, GFP_KERNEL); | |
| 243 | + next = sep; | |
| 244 | + /* Walk the list, looking for matching LSMs. */ | |
| 245 | + while ((name = strsep(&next, ",")) != NULL) { | |
| 246 | + bool found = false; | |
| 247 | + | |
| 248 | + for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { | |
| 249 | + if (lsm->order == LSM_ORDER_MUTABLE && | |
| 250 | + strcmp(lsm->name, name) == 0) { | |
| 251 | + append_ordered_lsm(lsm, origin); | |
| 252 | + found = true; | |
| 253 | + } | |
| 254 | + } | |
| 255 | + | |
| 256 | + if (!found) | |
| 257 | + init_debug("%s ignored: %s\n", origin, name); | |
| 258 | + } | |
| 259 | + | |
| 260 | + /* Process "security=", if given. */ | |
| 261 | + if (chosen_major_lsm) { | |
| 262 | + for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { | |
| 263 | + if (exists_ordered_lsm(lsm)) | |
| 264 | + continue; | |
| 265 | + if (strcmp(lsm->name, chosen_major_lsm) == 0) | |
| 266 | + append_ordered_lsm(lsm, "security="); | |
| 267 | + } | |
| 268 | + } | |
| 269 | + | |
| 270 | + /* Disable all LSMs not in the ordered list. */ | |
| 271 | + for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { | |
| 272 | + if (exists_ordered_lsm(lsm)) | |
| 273 | + continue; | |
| 274 | + set_enabled(lsm, false); | |
| 275 | + init_debug("%s disabled: %s\n", origin, lsm->name); | |
| 276 | + } | |
| 277 | + | |
| 278 | + kfree(sep); | |
| 279 | +} | |
| 280 | + | |
| 281 | +static void __init lsm_early_cred(struct cred *cred); | |
| 282 | +static void __init lsm_early_task(struct task_struct *task); | |
| 283 | + | |
| 284 | +static void __init ordered_lsm_init(void) | |
| 285 | +{ | |
| 286 | + struct lsm_info **lsm; | |
| 287 | + | |
| 288 | + ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms), | |
| 289 | + GFP_KERNEL); | |
| 290 | + | |
| 291 | + if (chosen_lsm_order) { | |
| 292 | + if (chosen_major_lsm) { | |
| 293 | + pr_info("security= is ignored because it is superseded by lsm=\n"); | |
| 294 | + chosen_major_lsm = NULL; | |
| 295 | + } | |
| 296 | + ordered_lsm_parse(chosen_lsm_order, "cmdline"); | |
| 297 | + } else | |
| 298 | + ordered_lsm_parse(builtin_lsm_order, "builtin"); | |
| 299 | + | |
| 300 | + for (lsm = ordered_lsms; *lsm; lsm++) | |
| 301 | + prepare_lsm(*lsm); | |
| 302 | + | |
| 303 | + init_debug("cred blob size = %d\n", blob_sizes.lbs_cred); | |
| 304 | + init_debug("file blob size = %d\n", blob_sizes.lbs_file); | |
| 305 | + init_debug("inode blob size = %d\n", blob_sizes.lbs_inode); | |
| 306 | + init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc); | |
| 307 | + init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg); | |
| 308 | + init_debug("task blob size = %d\n", blob_sizes.lbs_task); | |
| 309 | + | |
| 310 | + /* | |
| 311 | + * Create any kmem_caches needed for blobs | |
| 312 | + */ | |
| 313 | + if (blob_sizes.lbs_file) | |
| 314 | + lsm_file_cache = kmem_cache_create("lsm_file_cache", | |
| 315 | + blob_sizes.lbs_file, 0, | |
| 316 | + SLAB_PANIC, NULL); | |
| 317 | + if (blob_sizes.lbs_inode) | |
| 318 | + lsm_inode_cache = kmem_cache_create("lsm_inode_cache", | |
| 319 | + blob_sizes.lbs_inode, 0, | |
| 320 | + SLAB_PANIC, NULL); | |
| 321 | + | |
| 322 | + lsm_early_cred((struct cred *) current->cred); | |
| 323 | + lsm_early_task(current); | |
| 324 | + for (lsm = ordered_lsms; *lsm; lsm++) | |
| 325 | + initialize_lsm(*lsm); | |
| 326 | + | |
| 327 | + kfree(ordered_lsms); | |
| 328 | +} | |
| 329 | + | |
| 67 | 330 | /** |
| 68 | 331 | * security_init - initializes the security framework |
| 69 | 332 | * |
| @@ -80,28 +343,27 @@ int __init security_init(void) · 10 unmodified lines | ||
| 80 | 343 | i++) |
| 81 | 344 | INIT_HLIST_HEAD(&list[i]); |
| 82 | 345 | |
| 83 | − /* | |
| 84 | − * Load minor LSMs, with the capability module always first. | |
| 85 | − */ | |
| 86 | − capability_add_hooks(); | |
| 87 | − yama_add_hooks(); | |
| 88 | − loadpin_add_hooks(); | |
| 89 | − | |
| 90 | − /* | |
| 91 | − * Load all the remaining security modules. | |
| 92 | − */ | |
| 93 | − major_lsm_init(); | |
| 346 | + /* Load LSMs in specified order. */ | |
| 347 | + ordered_lsm_init(); | |
| 94 | 348 | |
| 95 | 349 | return 0; |
| 96 | 350 | } |
| 97 | 351 | |
| 98 | 352 | /* Save user chosen LSM */ |
| 99 | −static int __init choose_lsm(char *str) | |
| 353 | +static int __init choose_major_lsm(char *str) | |
| 354 | +{ | |
| 355 | + chosen_major_lsm = str; | |
| 356 | + return 1; | |
| 357 | +} | |
| 358 | +__setup("security=", choose_major_lsm); | |
| 359 | + | |
| 360 | +/* Explicitly choose LSM initialization order. */ | |
| 361 | +static int __init choose_lsm_order(char *str) | |
| 100 | 362 | { |
| 101 | − strncpy(chosen_lsm, str, SECURITY_NAME_MAX); | |
| 363 | + chosen_lsm_order = str; | |
| 102 | 364 | return 1; |
| 103 | 365 | } |
| 104 | −__setup("security=", choose_lsm); | |
| 366 | +__setup("lsm=", choose_lsm_order); | |
| 105 | 367 | |
| 106 | 368 | /* Enable LSM order debugging. */ |
| 107 | 369 | static int __init enable_debug(char *str) |
| @@ -147,29 +409,6 @@ static int lsm_append(char *new, char **result) · 39 unmodified lines | ||
| 147 | 409 | return 0; |
| 148 | 410 | } |
| 149 | 411 | |
| 150 | −/** | |
| 151 | − * security_module_enable - Load given security module on boot ? | |
| 152 | − * @module: the name of the module | |
| 153 | − * | |
| 154 | − * Each LSM must pass this method before registering its own operations | |
| 155 | − * to avoid security registration races. This method may also be used | |
| 156 | − * to check if your LSM is currently loaded during kernel initialization. | |
| 157 | − * | |
| 158 | − * Returns: | |
| 159 | − * | |
| 160 | − * true if: | |
| 161 | − * | |
| 162 | − * - The passed LSM is the one chosen by user at boot time, | |
| 163 | − * - or the passed LSM is configured as the default and the user did not | |
| 164 | − * choose an alternate LSM at boot time. | |
| 165 | − * | |
| 166 | − * Otherwise, return false. | |
| 167 | − */ | |
| 168 | −int __init security_module_enable(const char *module) | |
| 169 | −{ | |
| 170 | − return !strcmp(module, chosen_lsm); | |
| 171 | −} | |
| 172 | − | |
| 173 | 412 | /** |
| 174 | 413 | * security_add_hooks - Add a modules hooks to the hook lists. |
| 175 | 414 | * @hooks: the hooks to add |
| @@ -209,6 +448,161 @@ int unregister_lsm_notifier(struct notifier_block *nb) · 33 unmodified lines | ||
| 209 | 448 | } |
| 210 | 449 | EXPORT_SYMBOL(unregister_lsm_notifier); |
| 211 | 450 | |
| 451 | +/** | |
| 452 | + * lsm_cred_alloc - allocate a composite cred blob | |
| 453 | + * @cred: the cred that needs a blob | |
| 454 | + * @gfp: allocation type | |
| 455 | + * | |
| 456 | + * Allocate the cred blob for all the modules | |
| 457 | + * | |
| 458 | + * Returns 0, or -ENOMEM if memory can't be allocated. | |
| 459 | + */ | |
| 460 | +static int lsm_cred_alloc(struct cred *cred, gfp_t gfp) | |
| 461 | +{ | |
| 462 | + if (blob_sizes.lbs_cred == 0) { | |
| 463 | + cred->security = NULL; | |
| 464 | + return 0; | |
| 465 | + } | |
| 466 | + | |
| 467 | + cred->security = kzalloc(blob_sizes.lbs_cred, gfp); | |
| 468 | + if (cred->security == NULL) | |
| 469 | + return -ENOMEM; | |
| 470 | + return 0; | |
| 471 | +} | |
| 472 | + | |
| 473 | +/** | |
| 474 | + * lsm_early_cred - during initialization allocate a composite cred blob | |
| 475 | + * @cred: the cred that needs a blob | |
| 476 | + * | |
| 477 | + * Allocate the cred blob for all the modules | |
| 478 | + */ | |
| 479 | +static void __init lsm_early_cred(struct cred *cred) | |
| 480 | +{ | |
| 481 | + int rc = lsm_cred_alloc(cred, GFP_KERNEL); | |
| 482 | + | |
| 483 | + if (rc) | |
| 484 | + panic("%s: Early cred alloc failed.\n", __func__); | |
| 485 | +} | |
| 486 | + | |
| 487 | +/** | |
| 488 | + * lsm_file_alloc - allocate a composite file blob | |
| 489 | + * @file: the file that needs a blob | |
| 490 | + * | |
| 491 | + * Allocate the file blob for all the modules | |
| 492 | + * | |
| 493 | + * Returns 0, or -ENOMEM if memory can't be allocated. | |
| 494 | + */ | |
| 495 | +static int lsm_file_alloc(struct file *file) | |
| 496 | +{ | |
| 497 | + if (!lsm_file_cache) { | |
| 498 | + file->f_security = NULL; | |
| 499 | + return 0; | |
| 500 | + } | |
| 501 | + | |
| 502 | + file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL); | |
| 503 | + if (file->f_security == NULL) | |
| 504 | + return -ENOMEM; | |
| 505 | + return 0; | |
| 506 | +} | |
| 507 | + | |
| 508 | +/** | |
| 509 | + * lsm_inode_alloc - allocate a composite inode blob | |
| 510 | + * @inode: the inode that needs a blob | |
| 511 | + * | |
| 512 | + * Allocate the inode blob for all the modules | |
| 513 | + * | |
| 514 | + * Returns 0, or -ENOMEM if memory can't be allocated. | |
| 515 | + */ | |
| 516 | +int lsm_inode_alloc(struct inode *inode) | |
| 517 | +{ | |
| 518 | + if (!lsm_inode_cache) { | |
| 519 | + inode->i_security = NULL; | |
| 520 | + return 0; | |
| 521 | + } | |
| 522 | + | |
| 523 | + inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS); | |
| 524 | + if (inode->i_security == NULL) | |
| 525 | + return -ENOMEM; | |
| 526 | + return 0; | |
| 527 | +} | |
| 528 | + | |
| 529 | +/** | |
| 530 | + * lsm_task_alloc - allocate a composite task blob | |
| 531 | + * @task: the task that needs a blob | |
| 532 | + * | |
| 533 | + * Allocate the task blob for all the modules | |
| 534 | + * | |
| 535 | + * Returns 0, or -ENOMEM if memory can't be allocated. | |
| 536 | + */ | |
| 537 | +static int lsm_task_alloc(struct task_struct *task) | |
| 538 | +{ | |
| 539 | + if (blob_sizes.lbs_task == 0) { | |
| 540 | + task->security = NULL; | |
| 541 | + return 0; | |
| 542 | + } | |
| 543 | + | |
| 544 | + task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL); | |
| 545 | + if (task->security == NULL) | |
| 546 | + return -ENOMEM; | |
| 547 | + return 0; | |
| 548 | +} | |
| 549 | + | |
| 550 | +/** | |
| 551 | + * lsm_ipc_alloc - allocate a composite ipc blob | |
| 552 | + * @kip: the ipc that needs a blob | |
| 553 | + * | |
| 554 | + * Allocate the ipc blob for all the modules | |
| 555 | + * | |
| 556 | + * Returns 0, or -ENOMEM if memory can't be allocated. | |
| 557 | + */ | |
| 558 | +static int lsm_ipc_alloc(struct kern_ipc_perm *kip) | |
| 559 | +{ | |
| 560 | + if (blob_sizes.lbs_ipc == 0) { | |
| 561 | + kip->security = NULL; | |
| 562 | + return 0; | |
| 563 | + } | |
| 564 | + | |
| 565 | + kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL); | |
| 566 | + if (kip->security == NULL) | |
| 567 | + return -ENOMEM; | |
| 568 | + return 0; | |
| 569 | +} | |
| 570 | + | |
| 571 | +/** | |
| 572 | + * lsm_msg_msg_alloc - allocate a composite msg_msg blob | |
| 573 | + * @mp: the msg_msg that needs a blob | |
| 574 | + * | |
| 575 | + * Allocate the ipc blob for all the modules | |
| 576 | + * | |
| 577 | + * Returns 0, or -ENOMEM if memory can't be allocated. | |
| 578 | + */ | |
| 579 | +static int lsm_msg_msg_alloc(struct msg_msg *mp) | |
| 580 | +{ | |
| 581 | + if (blob_sizes.lbs_msg_msg == 0) { | |
| 582 | + mp->security = NULL; | |
| 583 | + return 0; | |
| 584 | + } | |
| 585 | + | |
| 586 | + mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL); | |
| 587 | + if (mp->security == NULL) | |
| 588 | + return -ENOMEM; | |
| 589 | + return 0; | |
| 590 | +} | |
| 591 | + | |
| 592 | +/** | |
| 593 | + * lsm_early_task - during initialization allocate a composite task blob | |
| 594 | + * @task: the task that needs a blob | |
| 595 | + * | |
| 596 | + * Allocate the task blob for all the modules | |
| 597 | + */ | |
| 598 | +static void __init lsm_early_task(struct task_struct *task) | |
| 599 | +{ | |
| 600 | + int rc = lsm_task_alloc(task); | |
| 601 | + | |
| 602 | + if (rc) | |
| 603 | + panic("%s: Early task alloc failed.\n", __func__); | |
| 604 | +} | |
| 605 | + | |
| 212 | 606 | /* |
| 213 | 607 | * Hook list operation macros. |
| 214 | 608 | * |
| @@ -294,16 +688,12 @@ int security_capset(struct cred *new, const struct cred *old, · 79 unmodified lines | ||
| 294 | 688 | effective, inheritable, permitted); |
| 295 | 689 | } |
| 296 | 690 | |
| 297 | −int security_capable(const struct cred *cred, struct user_namespace *ns, | |
| 298 | − int cap) | |
| 691 | +int security_capable(const struct cred *cred, | |
| 692 | + struct user_namespace *ns, | |
| 693 | + int cap, | |
| 694 | + unsigned int opts) | |
| 299 | 695 | { |
| 300 | − return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT); | |
| 301 | −} | |
| 302 | − | |
| 303 | −int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns, | |
| 304 | − int cap) | |
| 305 | −{ | |
| 306 | − return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_NOAUDIT); | |
| 696 | + return call_int_hook(capable, 0, cred, ns, cap, opts); | |
| 307 | 697 | } |
| 308 | 698 | |
| 309 | 699 | int security_quotactl(int cmds, int type, int id, struct super_block *sb) |
| @@ -468,14 +858,40 @@ EXPORT_SYMBOL(security_add_mnt_opt); · 158 unmodified lines | ||
| 468 | 858 | |
| 469 | 859 | int security_inode_alloc(struct inode *inode) |
| 470 | 860 | { |
| 471 | − inode->i_security = NULL; | |
| 472 | − return call_int_hook(inode_alloc_security, 0, inode); | |
| 861 | + int rc = lsm_inode_alloc(inode); | |
| 862 | + | |
| 863 | + if (unlikely(rc)) | |
| 864 | + return rc; | |
| 865 | + rc = call_int_hook(inode_alloc_security, 0, inode); | |
| 866 | + if (unlikely(rc)) | |
| 867 | + security_inode_free(inode); | |
| 868 | + return rc; | |
| 869 | +} | |
| 870 | + | |
| 871 | +static void inode_free_by_rcu(struct rcu_head *head) | |
| 872 | +{ | |
| 873 | + /* | |
| 874 | + * The rcu head is at the start of the inode blob | |
| 875 | + */ | |
| 876 | + kmem_cache_free(lsm_inode_cache, head); | |
| 473 | 877 | } |
| 474 | 878 | |
| 475 | 879 | void security_inode_free(struct inode *inode) |
| 476 | 880 | { |
| 477 | 881 | integrity_inode_free(inode); |
| 478 | 882 | call_void_hook(inode_free_security, inode); |
| 883 | + /* | |
| 884 | + * The inode may still be referenced in a path walk and | |
| 885 | + * a call to security_inode_permission() can be made | |
| 886 | + * after inode_free_security() is called. Ideally, the VFS | |
| 887 | + * wouldn't do this, but fixing that is a much harder | |
| 888 | + * job. For now, simply free the i_security via RCU, and | |
| 889 | + * leave the current inode->i_security pointer intact. | |
| 890 | + * The inode will be freed after the RCU grace period too. | |
| 891 | + */ | |
| 892 | + if (inode->i_security) | |
| 893 | + call_rcu((struct rcu_head *)inode->i_security, | |
| 894 | + inode_free_by_rcu); | |
| 479 | 895 | } |
| 480 | 896 | |
| 481 | 897 | int security_dentry_init_security(struct dentry *dentry, int mode, |
| @@ -905,12 +1321,27 @@ int security_file_permission(struct file *file, int mask) · 423 unmodified lines | ||
| 905 | 1321 | |
| 906 | 1322 | int security_file_alloc(struct file *file) |
| 907 | 1323 | { |
| 908 | − return call_int_hook(file_alloc_security, 0, file); | |
| 1324 | + int rc = lsm_file_alloc(file); | |
| 1325 | + | |
| 1326 | + if (rc) | |
| 1327 | + return rc; | |
| 1328 | + rc = call_int_hook(file_alloc_security, 0, file); | |
| 1329 | + if (unlikely(rc)) | |
| 1330 | + security_file_free(file); | |
| 1331 | + return rc; | |
| 909 | 1332 | } |
| 910 | 1333 | |
| 911 | 1334 | void security_file_free(struct file *file) |
| 912 | 1335 | { |
| 1336 | + void *blob; | |
| 1337 | + | |
| 913 | 1338 | call_void_hook(file_free_security, file); |
| 1339 | + | |
| 1340 | + blob = file->f_security; | |
| 1341 | + if (blob) { | |
| 1342 | + file->f_security = NULL; | |
| 1343 | + kmem_cache_free(lsm_file_cache, blob); | |
| 1344 | + } | |
| 914 | 1345 | } |
| 915 | 1346 | |
| 916 | 1347 | int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| @@ -1012,17 +1443,35 @@ int security_file_open(struct file *file) · 95 unmodified lines | ||
| 1012 | 1443 | |
| 1013 | 1444 | int security_task_alloc(struct task_struct *task, unsigned long clone_flags) |
| 1014 | 1445 | { |
| 1015 | − return call_int_hook(task_alloc, 0, task, clone_flags); | |
| 1446 | + int rc = lsm_task_alloc(task); | |
| 1447 | + | |
| 1448 | + if (rc) | |
| 1449 | + return rc; | |
| 1450 | + rc = call_int_hook(task_alloc, 0, task, clone_flags); | |
| 1451 | + if (unlikely(rc)) | |
| 1452 | + security_task_free(task); | |
| 1453 | + return rc; | |
| 1016 | 1454 | } |
| 1017 | 1455 | |
| 1018 | 1456 | void security_task_free(struct task_struct *task) |
| 1019 | 1457 | { |
| 1020 | 1458 | call_void_hook(task_free, task); |
| 1459 | + | |
| 1460 | + kfree(task->security); | |
| 1461 | + task->security = NULL; | |
| 1021 | 1462 | } |
| 1022 | 1463 | |
| 1023 | 1464 | int security_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 1024 | 1465 | { |
| 1025 | − return call_int_hook(cred_alloc_blank, 0, cred, gfp); | |
| 1466 | + int rc = lsm_cred_alloc(cred, gfp); | |
| 1467 | + | |
| 1468 | + if (rc) | |
| 1469 | + return rc; | |
| 1470 | + | |
| 1471 | + rc = call_int_hook(cred_alloc_blank, 0, cred, gfp); | |
| 1472 | + if (unlikely(rc)) | |
| 1473 | + security_cred_free(cred); | |
| 1474 | + return rc; | |
| 1026 | 1475 | } |
| 1027 | 1476 | |
| 1028 | 1477 | void security_cred_free(struct cred *cred) |
| @@ -1035,11 +1484,22 @@ void security_cred_free(struct cred *cred) · 6 unmodified lines | ||
| 1035 | 1484 | return; |
| 1036 | 1485 | |
| 1037 | 1486 | call_void_hook(cred_free, cred); |
| 1487 | + | |
| 1488 | + kfree(cred->security); | |
| 1489 | + cred->security = NULL; | |
| 1038 | 1490 | } |
| 1039 | 1491 | |
| 1040 | 1492 | int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp) |
| 1041 | 1493 | { |
| 1042 | − return call_int_hook(cred_prepare, 0, new, old, gfp); | |
| 1494 | + int rc = lsm_cred_alloc(new, gfp); | |
| 1495 | + | |
| 1496 | + if (rc) | |
| 1497 | + return rc; | |
| 1498 | + | |
| 1499 | + rc = call_int_hook(cred_prepare, 0, new, old, gfp); | |
| 1500 | + if (unlikely(rc)) | |
| 1501 | + security_cred_free(new); | |
| 1502 | + return rc; | |
| 1043 | 1503 | } |
| 1044 | 1504 | |
| 1045 | 1505 | void security_transfer_creds(struct cred *new, const struct cred *old) |
| @@ -1220,22 +1680,40 @@ void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) · 174 unmodified lines | ||
| 1220 | 1680 | |
| 1221 | 1681 | int security_msg_msg_alloc(struct msg_msg *msg) |
| 1222 | 1682 | { |
| 1223 | − return call_int_hook(msg_msg_alloc_security, 0, msg); | |
| 1683 | + int rc = lsm_msg_msg_alloc(msg); | |
| 1684 | + | |
| 1685 | + if (unlikely(rc)) | |
| 1686 | + return rc; | |
| 1687 | + rc = call_int_hook(msg_msg_alloc_security, 0, msg); | |
| 1688 | + if (unlikely(rc)) | |
| 1689 | + security_msg_msg_free(msg); | |
| 1690 | + return rc; | |
| 1224 | 1691 | } |
| 1225 | 1692 | |
| 1226 | 1693 | void security_msg_msg_free(struct msg_msg *msg) |
| 1227 | 1694 | { |
| 1228 | 1695 | call_void_hook(msg_msg_free_security, msg); |
| 1696 | + kfree(msg->security); | |
| 1697 | + msg->security = NULL; | |
| 1229 | 1698 | } |
| 1230 | 1699 | |
| 1231 | 1700 | int security_msg_queue_alloc(struct kern_ipc_perm *msq) |
| 1232 | 1701 | { |
| 1233 | − return call_int_hook(msg_queue_alloc_security, 0, msq); | |
| 1702 | + int rc = lsm_ipc_alloc(msq); | |
| 1703 | + | |
| 1704 | + if (unlikely(rc)) | |
| 1705 | + return rc; | |
| 1706 | + rc = call_int_hook(msg_queue_alloc_security, 0, msq); | |
| 1707 | + if (unlikely(rc)) | |
| 1708 | + security_msg_queue_free(msq); | |
| 1709 | + return rc; | |
| 1234 | 1710 | } |
| 1235 | 1711 | |
| 1236 | 1712 | void security_msg_queue_free(struct kern_ipc_perm *msq) |
| 1237 | 1713 | { |
| 1238 | 1714 | call_void_hook(msg_queue_free_security, msq); |
| 1715 | + kfree(msq->security); | |
| 1716 | + msq->security = NULL; | |
| 1239 | 1717 | } |
| 1240 | 1718 | |
| 1241 | 1719 | int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) |
| @@ -1262,12 +1740,21 @@ int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, · 20 unmodified lines | ||
| 1262 | 1740 | |
| 1263 | 1741 | int security_shm_alloc(struct kern_ipc_perm *shp) |
| 1264 | 1742 | { |
| 1265 | − return call_int_hook(shm_alloc_security, 0, shp); | |
| 1743 | + int rc = lsm_ipc_alloc(shp); | |
| 1744 | + | |
| 1745 | + if (unlikely(rc)) | |
| 1746 | + return rc; | |
| 1747 | + rc = call_int_hook(shm_alloc_security, 0, shp); | |
| 1748 | + if (unlikely(rc)) | |
| 1749 | + security_shm_free(shp); | |
| 1750 | + return rc; | |
| 1266 | 1751 | } |
| 1267 | 1752 | |
| 1268 | 1753 | void security_shm_free(struct kern_ipc_perm *shp) |
| 1269 | 1754 | { |
| 1270 | 1755 | call_void_hook(shm_free_security, shp); |
| 1756 | + kfree(shp->security); | |
| 1757 | + shp->security = NULL; | |
| 1271 | 1758 | } |
| 1272 | 1759 | |
| 1273 | 1760 | int security_shm_associate(struct kern_ipc_perm *shp, int shmflg) |
| @@ -1287,12 +1774,21 @@ int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmf · 13 unmodified lines | ||
| 1287 | 1774 | |
| 1288 | 1775 | int security_sem_alloc(struct kern_ipc_perm *sma) |
| 1289 | 1776 | { |
| 1290 | − return call_int_hook(sem_alloc_security, 0, sma); | |
| 1777 | + int rc = lsm_ipc_alloc(sma); | |
| 1778 | + | |
| 1779 | + if (unlikely(rc)) | |
| 1780 | + return rc; | |
| 1781 | + rc = call_int_hook(sem_alloc_security, 0, sma); | |
| 1782 | + if (unlikely(rc)) | |
| 1783 | + security_sem_free(sma); | |
| 1784 | + return rc; | |
| 1291 | 1785 | } |
| 1292 | 1786 | |
| 1293 | 1787 | void security_sem_free(struct kern_ipc_perm *sma) |
| 1294 | 1788 | { |
| 1295 | 1789 | call_void_hook(sem_free_security, sma); |
| 1790 | + kfree(sma->security); | |
| 1791 | + sma->security = NULL; | |
| 1296 | 1792 | } |
| 1297 | 1793 | |
| 1298 | 1794 | int security_sem_associate(struct kern_ipc_perm *sma, int semflg) |
| @@ -1319,14 +1815,30 @@ void security_d_instantiate(struct dentry *dentry, struct inode *inode) · 20 unmodified lines | ||
| 1319 | 1815 | } |
| 1320 | 1816 | EXPORT_SYMBOL(security_d_instantiate); |
| 1321 | 1817 | |
| 1322 | −int security_getprocattr(struct task_struct *p, char *name, char **value) | |
| 1818 | +int security_getprocattr(struct task_struct *p, const char *lsm, char *name, | |
| 1819 | + char **value) | |
| 1323 | 1820 | { |
| 1324 | − return call_int_hook(getprocattr, -EINVAL, p, name, value); | |
| 1821 | + struct security_hook_list *hp; | |
| 1822 | + | |
| 1823 | + hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) { | |
| 1824 | + if (lsm != NULL && strcmp(lsm, hp->lsm)) | |
| 1825 | + continue; | |
| 1826 | + return hp->hook.getprocattr(p, name, value); | |
| 1827 | + } | |
| 1828 | + return -EINVAL; | |
| 1325 | 1829 | } |
| 1326 | 1830 | |
| 1327 | −int security_setprocattr(const char *name, void *value, size_t size) | |
| 1831 | +int security_setprocattr(const char *lsm, const char *name, void *value, | |
| 1832 | + size_t size) | |
| 1328 | 1833 | { |
| 1329 | − return call_int_hook(setprocattr, -EINVAL, name, value, size); | |
| 1834 | + struct security_hook_list *hp; | |
| 1835 | + | |
| 1836 | + hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) { | |
| 1837 | + if (lsm != NULL && strcmp(lsm, hp->lsm)) | |
| 1838 | + continue; | |
| 1839 | + return hp->hook.setprocattr(name, value, size); | |
| 1840 | + } | |
| 1841 | + return -EINVAL; | |
| 1330 | 1842 | } |
| 1331 | 1843 | |
| 1332 | 1844 | int security_netlink_send(struct sock *sk, struct sk_buff *skb) |
| @@ -22,21 +22,6 @@ config SECURITY_SELINUX_BOOTPARAM · 21 unmodified lines | ||
| 22 | 22 | |
| 23 | 23 | If you are unsure how to answer this question, answer N. |
| 24 | 24 | |
| 25 | −config SECURITY_SELINUX_BOOTPARAM_VALUE | |
| 26 | − int "NSA SELinux boot parameter default value" | |
| 27 | − depends on SECURITY_SELINUX_BOOTPARAM | |
| 28 | − range 0 1 | |
| 29 | − default 1 | |
| 30 | − help | |
| 31 | − This option sets the default value for the kernel parameter | |
| 32 | − 'selinux', which allows SELinux to be disabled at boot. If this | |
| 33 | − option is set to 0 (zero), the SELinux kernel parameter will | |
| 34 | − default to 0, disabling SELinux at bootup. If this option is | |
| 35 | − set to 1 (one), the SELinux kernel parameter will default to 1, | |
| 36 | − enabling SELinux at bootup. | |
| 37 | − | |
| 38 | − If you are unsure how to answer this question, answer 1. | |
| 39 | − | |
| 40 | 25 | config SECURITY_SELINUX_DISABLE |
| 41 | 26 | bool "NSA SELinux runtime disable" |
| 42 | 27 | depends on SECURITY_SELINUX |
| @@ -6,7 +6,7 @@ · 5 unmodified lines | ||
| 6 | 6 | obj-$(CONFIG_SECURITY_SELINUX) := selinux.o |
| 7 | 7 | |
| 8 | 8 | selinux-y := avc.o hooks.o selinuxfs.o netlink.o nlmsgtab.o netif.o \ |
| 9 | − netnode.o netport.o ibpkey.o exports.o \ | |
| 9 | + netnode.o netport.o ibpkey.o \ | |
| 10 | 10 | ss/ebitmap.o ss/hashtab.o ss/symtab.o ss/sidtab.o ss/avtab.o \ |
| 11 | 11 | ss/policydb.o ss/services.o ss/conditional.o ss/mls.o ss/status.o |
| 12 | 12 | |
| @@ -1,23 +0,0 @@ | ||
| 1 | −/* | |
| 2 | − * SELinux services exported to the rest of the kernel. | |
| 3 | − * | |
| 4 | − * Author: James Morris <jmorris@redhat.com> | |
| 5 | − * | |
| 6 | − * Copyright (C) 2005 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
| 7 | − * Copyright (C) 2006 Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> | |
| 8 | − * Copyright (C) 2006 IBM Corporation, Timothy R. Chavez <tinytim@us.ibm.com> | |
| 9 | − * | |
| 10 | − * This program is free software; you can redistribute it and/or modify | |
| 11 | − * it under the terms of the GNU General Public License version 2, | |
| 12 | − * as published by the Free Software Foundation. | |
| 13 | − */ | |
| 14 | −#include <linux/module.h> | |
| 15 | −#include <linux/selinux.h> | |
| 16 | − | |
| 17 | −#include "security.h" | |
| 18 | − | |
| 19 | −bool selinux_is_enabled(void) | |
| 20 | −{ | |
| 21 | − return selinux_enabled; | |
| 22 | −} | |
| 23 | −EXPORT_SYMBOL_GPL(selinux_is_enabled); | |
| @@ -79,7 +79,6 @@ · 78 unmodified lines | ||
| 79 | 79 | #include <linux/personality.h> |
| 80 | 80 | #include <linux/audit.h> |
| 81 | 81 | #include <linux/string.h> |
| 82 | −#include <linux/selinux.h> | |
| 83 | 82 | #include <linux/mutex.h> |
| 84 | 83 | #include <linux/posix-timers.h> |
| 85 | 84 | #include <linux/syslog.h> |
| @@ -121,9 +120,8 @@ __setup("enforcing=", enforcing_setup); · 35 unmodified lines | ||
| 121 | 120 | #define selinux_enforcing_boot 1 |
| 122 | 121 | #endif |
| 123 | 122 | |
| 123 | +int selinux_enabled __lsm_ro_after_init = 1; | |
| 124 | 124 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM |
| 125 | −int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; | |
| 126 | − | |
| 127 | 125 | static int __init selinux_enabled_setup(char *str) |
| 128 | 126 | { |
| 129 | 127 | unsigned long enabled; |
| @@ -132,8 +130,6 @@ static int __init selinux_enabled_setup(char *str) · 2 unmodified lines | ||
| 132 | 130 | return 1; |
| 133 | 131 | } |
| 134 | 132 | __setup("selinux=", selinux_enabled_setup); |
| 135 | −#else | |
| 136 | −int selinux_enabled = 1; | |
| 137 | 133 | #endif |
| 138 | 134 | |
| 139 | 135 | static unsigned int selinux_checkreqprot_boot = |
| @@ -149,9 +145,6 @@ static int __init checkreqprot_setup(char *str) · 9 unmodified lines | ||
| 149 | 145 | } |
| 150 | 146 | __setup("checkreqprot=", checkreqprot_setup); |
| 151 | 147 | |
| 152 | −static struct kmem_cache *sel_inode_cache; | |
| 153 | −static struct kmem_cache *file_security_cache; | |
| 154 | − | |
| 155 | 148 | /** |
| 156 | 149 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled |
| 157 | 150 | * |
| @@ -214,12 +207,8 @@ static void cred_init_security(void) · 56 unmodified lines | ||
| 214 | 207 | struct cred *cred = (struct cred *) current->real_cred; |
| 215 | 208 | struct task_security_struct *tsec; |
| 216 | 209 | |
| 217 | − tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); | |
| 218 | − if (!tsec) | |
| 219 | − panic("SELinux: Failed to initialize initial task.\n"); | |
| 220 | − | |
| 210 | + tsec = selinux_cred(cred); | |
| 221 | 211 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
| 222 | − cred->security = tsec; | |
| 223 | 212 | } |
| 224 | 213 | |
| 225 | 214 | /* |
| @@ -229,7 +218,7 @@ static inline u32 cred_sid(const struct cred *cred) · 3 unmodified lines | ||
| 229 | 218 | { |
| 230 | 219 | const struct task_security_struct *tsec; |
| 231 | 220 | |
| 232 | − tsec = cred->security; | |
| 221 | + tsec = selinux_cred(cred); | |
| 233 | 222 | return tsec->sid; |
| 234 | 223 | } |
| 235 | 224 | |
| @@ -250,13 +239,9 @@ static inline u32 task_sid(const struct task_struct *task) · 14 unmodified lines | ||
| 250 | 239 | |
| 251 | 240 | static int inode_alloc_security(struct inode *inode) |
| 252 | 241 | { |
| 253 | − struct inode_security_struct *isec; | |
| 242 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 254 | 243 | u32 sid = current_sid(); |
| 255 | 244 | |
| 256 | − isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); | |
| 257 | − if (!isec) | |
| 258 | − return -ENOMEM; | |
| 259 | − | |
| 260 | 245 | spin_lock_init(&isec->lock); |
| 261 | 246 | INIT_LIST_HEAD(&isec->list); |
| 262 | 247 | isec->inode = inode; |
| @@ -264,7 +249,6 @@ static int inode_alloc_security(struct inode *inode) · 1 unmodified line | ||
| 264 | 249 | isec->sclass = SECCLASS_FILE; |
| 265 | 250 | isec->task_sid = sid; |
| 266 | 251 | isec->initialized = LABEL_INVALID; |
| 267 | − inode->i_security = isec; | |
| 268 | 252 | |
| 269 | 253 | return 0; |
| 270 | 254 | } |
| @@ -281,7 +265,7 @@ static int __inode_security_revalidate(struct inode *inode, · 10 unmodified lines | ||
| 281 | 265 | struct dentry *dentry, |
| 282 | 266 | bool may_sleep) |
| 283 | 267 | { |
| 284 | − struct inode_security_struct *isec = inode->i_security; | |
| 268 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 285 | 269 | |
| 286 | 270 | might_sleep_if(may_sleep); |
| 287 | 271 | |
| @@ -302,7 +286,7 @@ static int __inode_security_revalidate(struct inode *inode, · 14 unmodified lines | ||
| 302 | 286 | |
| 303 | 287 | static struct inode_security_struct *inode_security_novalidate(struct inode *inode) |
| 304 | 288 | { |
| 305 | − return inode->i_security; | |
| 289 | + return selinux_inode(inode); | |
| 306 | 290 | } |
| 307 | 291 | |
| 308 | 292 | static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) |
| @@ -312,7 +296,7 @@ static struct inode_security_struct *inode_security_rcu(struct inode *inode, boo · 3 unmodified lines | ||
| 312 | 296 | error = __inode_security_revalidate(inode, NULL, !rcu); |
| 313 | 297 | if (error) |
| 314 | 298 | return ERR_PTR(error); |
| 315 | − return inode->i_security; | |
| 299 | + return selinux_inode(inode); | |
| 316 | 300 | } |
| 317 | 301 | |
| 318 | 302 | /* |
| @@ -321,14 +305,14 @@ static struct inode_security_struct *inode_security_rcu(struct inode *inode, boo · 2 unmodified lines | ||
| 321 | 305 | static struct inode_security_struct *inode_security(struct inode *inode) |
| 322 | 306 | { |
| 323 | 307 | __inode_security_revalidate(inode, NULL, true); |
| 324 | − return inode->i_security; | |
| 308 | + return selinux_inode(inode); | |
| 325 | 309 | } |
| 326 | 310 | |
| 327 | 311 | static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) |
| 328 | 312 | { |
| 329 | 313 | struct inode *inode = d_backing_inode(dentry); |
| 330 | 314 | |
| 331 | − return inode->i_security; | |
| 315 | + return selinux_inode(inode); | |
| 332 | 316 | } |
| 333 | 317 | |
| 334 | 318 | /* |
| @@ -339,22 +323,17 @@ static struct inode_security_struct *backing_inode_security(struct dentry *dentr · 4 unmodified lines | ||
| 339 | 323 | struct inode *inode = d_backing_inode(dentry); |
| 340 | 324 | |
| 341 | 325 | __inode_security_revalidate(inode, dentry, true); |
| 342 | − return inode->i_security; | |
| 343 | −} | |
| 344 | − | |
| 345 | −static void inode_free_rcu(struct rcu_head *head) | |
| 346 | −{ | |
| 347 | − struct inode_security_struct *isec; | |
| 348 | − | |
| 349 | − isec = container_of(head, struct inode_security_struct, rcu); | |
| 350 | − kmem_cache_free(sel_inode_cache, isec); | |
| 326 | + return selinux_inode(inode); | |
| 351 | 327 | } |
| 352 | 328 | |
| 353 | 329 | static void inode_free_security(struct inode *inode) |
| 354 | 330 | { |
| 355 | − struct inode_security_struct *isec = inode->i_security; | |
| 356 | − struct superblock_security_struct *sbsec = inode->i_sb->s_security; | |
| 331 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 332 | + struct superblock_security_struct *sbsec; | |
| 357 | 333 | |
| 334 | + if (!isec) | |
| 335 | + return; | |
| 336 | + sbsec = inode->i_sb->s_security; | |
| 358 | 337 | /* |
| 359 | 338 | * As not all inode security structures are in a list, we check for |
| 360 | 339 | * empty list outside of the lock to make sure that we won't waste |
| @@ -370,42 +349,19 @@ static void inode_free_security(struct inode *inode) · 9 unmodified lines | ||
| 370 | 349 | list_del_init(&isec->list); |
| 371 | 350 | spin_unlock(&sbsec->isec_lock); |
| 372 | 351 | } |
| 373 | − | |
| 374 | − /* | |
| 375 | − * The inode may still be referenced in a path walk and | |
| 376 | − * a call to selinux_inode_permission() can be made | |
| 377 | − * after inode_free_security() is called. Ideally, the VFS | |
| 378 | − * wouldn't do this, but fixing that is a much harder | |
| 379 | − * job. For now, simply free the i_security via RCU, and | |
| 380 | − * leave the current inode->i_security pointer intact. | |
| 381 | − * The inode will be freed after the RCU grace period too. | |
| 382 | − */ | |
| 383 | − call_rcu(&isec->rcu, inode_free_rcu); | |
| 384 | 352 | } |
| 385 | 353 | |
| 386 | 354 | static int file_alloc_security(struct file *file) |
| 387 | 355 | { |
| 388 | − struct file_security_struct *fsec; | |
| 356 | + struct file_security_struct *fsec = selinux_file(file); | |
| 389 | 357 | u32 sid = current_sid(); |
| 390 | 358 | |
| 391 | − fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); | |
| 392 | − if (!fsec) | |
| 393 | − return -ENOMEM; | |
| 394 | − | |
| 395 | 359 | fsec->sid = sid; |
| 396 | 360 | fsec->fown_sid = sid; |
| 397 | − file->f_security = fsec; | |
| 398 | 361 | |
| 399 | 362 | return 0; |
| 400 | 363 | } |
| 401 | 364 | |
| 402 | −static void file_free_security(struct file *file) | |
| 403 | −{ | |
| 404 | − struct file_security_struct *fsec = file->f_security; | |
| 405 | − file->f_security = NULL; | |
| 406 | − kmem_cache_free(file_security_cache, fsec); | |
| 407 | −} | |
| 408 | − | |
| 409 | 365 | static int superblock_alloc_security(struct super_block *sb) |
| 410 | 366 | { |
| 411 | 367 | struct superblock_security_struct *sbsec; |
| @@ -501,7 +457,7 @@ static int may_context_mount_sb_relabel(u32 sid, · 89 unmodified lines | ||
| 501 | 457 | struct superblock_security_struct *sbsec, |
| 502 | 458 | const struct cred *cred) |
| 503 | 459 | { |
| 504 | − const struct task_security_struct *tsec = cred->security; | |
| 460 | + const struct task_security_struct *tsec = selinux_cred(cred); | |
| 505 | 461 | int rc; |
| 506 | 462 | |
| 507 | 463 | rc = avc_has_perm(&selinux_state, |
| @@ -520,7 +476,7 @@ static int may_context_mount_inode_relabel(u32 sid, · 12 unmodified lines | ||
| 520 | 476 | struct superblock_security_struct *sbsec, |
| 521 | 477 | const struct cred *cred) |
| 522 | 478 | { |
| 523 | − const struct task_security_struct *tsec = cred->security; | |
| 479 | + const struct task_security_struct *tsec = selinux_cred(cred); | |
| 524 | 480 | int rc; |
| 525 | 481 | rc = avc_has_perm(&selinux_state, |
| 526 | 482 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
| @@ -1374,7 +1330,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry, · 847 unmodified lines | ||
| 1374 | 1330 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1375 | 1331 | { |
| 1376 | 1332 | struct superblock_security_struct *sbsec = NULL; |
| 1377 | − struct inode_security_struct *isec = inode->i_security; | |
| 1333 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 1378 | 1334 | u32 task_sid, sid = 0; |
| 1379 | 1335 | u16 sclass; |
| 1380 | 1336 | struct dentry *dentry; |
| @@ -1621,7 +1577,7 @@ static inline u32 signal_to_av(int sig) · 240 unmodified lines | ||
| 1621 | 1577 | |
| 1622 | 1578 | /* Check whether a task is allowed to use a capability. */ |
| 1623 | 1579 | static int cred_has_capability(const struct cred *cred, |
| 1624 | − int cap, int audit, bool initns) | |
| 1580 | + int cap, unsigned int opts, bool initns) | |
| 1625 | 1581 | { |
| 1626 | 1582 | struct common_audit_data ad; |
| 1627 | 1583 | struct av_decision avd; |
| @@ -1648,7 +1604,7 @@ static int cred_has_capability(const struct cred *cred, · 20 unmodified lines | ||
| 1648 | 1604 | |
| 1649 | 1605 | rc = avc_has_perm_noaudit(&selinux_state, |
| 1650 | 1606 | sid, sid, sclass, av, 0, &avd); |
| 1651 | − if (audit == SECURITY_CAP_AUDIT) { | |
| 1607 | + if (!(opts & CAP_OPT_NOAUDIT)) { | |
| 1652 | 1608 | int rc2 = avc_audit(&selinux_state, |
| 1653 | 1609 | sid, sid, sclass, av, &avd, rc, &ad, 0); |
| 1654 | 1610 | if (rc2) |
| @@ -1674,7 +1630,7 @@ static int inode_has_perm(const struct cred *cred, · 19 unmodified lines | ||
| 1674 | 1630 | return 0; |
| 1675 | 1631 | |
| 1676 | 1632 | sid = cred_sid(cred); |
| 1677 | − isec = inode->i_security; | |
| 1633 | + isec = selinux_inode(inode); | |
| 1678 | 1634 | |
| 1679 | 1635 | return avc_has_perm(&selinux_state, |
| 1680 | 1636 | sid, isec->sid, isec->sclass, perms, adp); |
| @@ -1740,7 +1696,7 @@ static int file_has_perm(const struct cred *cred, · 59 unmodified lines | ||
| 1740 | 1696 | struct file *file, |
| 1741 | 1697 | u32 av) |
| 1742 | 1698 | { |
| 1743 | − struct file_security_struct *fsec = file->f_security; | |
| 1699 | + struct file_security_struct *fsec = selinux_file(file); | |
| 1744 | 1700 | struct inode *inode = file_inode(file); |
| 1745 | 1701 | struct common_audit_data ad; |
| 1746 | 1702 | u32 sid = cred_sid(cred); |
| @@ -1806,7 +1762,7 @@ static int may_create(struct inode *dir, · 59 unmodified lines | ||
| 1806 | 1762 | struct dentry *dentry, |
| 1807 | 1763 | u16 tclass) |
| 1808 | 1764 | { |
| 1809 | − const struct task_security_struct *tsec = current_security(); | |
| 1765 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 1810 | 1766 | struct inode_security_struct *dsec; |
| 1811 | 1767 | struct superblock_security_struct *sbsec; |
| 1812 | 1768 | u32 sid, newsid; |
| @@ -1828,7 +1784,7 @@ static int may_create(struct inode *dir, · 15 unmodified lines | ||
| 1828 | 1784 | if (rc) |
| 1829 | 1785 | return rc; |
| 1830 | 1786 | |
| 1831 | − rc = selinux_determine_inode_label(current_security(), dir, | |
| 1787 | + rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir, | |
| 1832 | 1788 | &dentry->d_name, tclass, &newsid); |
| 1833 | 1789 | if (rc) |
| 1834 | 1790 | return rc; |
| @@ -2084,7 +2040,7 @@ static int selinux_binder_transfer_file(struct task_struct *from, · 249 unmodified lines | ||
| 2084 | 2040 | struct file *file) |
| 2085 | 2041 | { |
| 2086 | 2042 | u32 sid = task_sid(to); |
| 2087 | − struct file_security_struct *fsec = file->f_security; | |
| 2043 | + struct file_security_struct *fsec = selinux_file(file); | |
| 2088 | 2044 | struct dentry *dentry = file->f_path.dentry; |
| 2089 | 2045 | struct inode_security_struct *isec; |
| 2090 | 2046 | struct common_audit_data ad; |
| @@ -2168,9 +2124,9 @@ static int selinux_capset(struct cred *new, const struct cred *old, · 77 unmodified lines | ||
| 2168 | 2124 | */ |
| 2169 | 2125 | |
| 2170 | 2126 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
| 2171 | − int cap, int audit) | |
| 2127 | + int cap, unsigned int opts) | |
| 2172 | 2128 | { |
| 2173 | − return cred_has_capability(cred, cap, audit, ns == &init_user_ns); | |
| 2129 | + return cred_has_capability(cred, cap, opts, ns == &init_user_ns); | |
| 2174 | 2130 | } |
| 2175 | 2131 | |
| 2176 | 2132 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| @@ -2244,7 +2200,7 @@ static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) · 67 unmodified lines | ||
| 2244 | 2200 | int rc, cap_sys_admin = 0; |
| 2245 | 2201 | |
| 2246 | 2202 | rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, |
| 2247 | − SECURITY_CAP_NOAUDIT, true); | |
| 2203 | + CAP_OPT_NOAUDIT, true); | |
| 2248 | 2204 | if (rc == 0) |
| 2249 | 2205 | cap_sys_admin = 1; |
| 2250 | 2206 | |
| @@ -2335,8 +2291,8 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm) · 84 unmodified lines | ||
| 2335 | 2291 | if (bprm->called_set_creds) |
| 2336 | 2292 | return 0; |
| 2337 | 2293 | |
| 2338 | − old_tsec = current_security(); | |
| 2339 | − new_tsec = bprm->cred->security; | |
| 2294 | + old_tsec = selinux_cred(current_cred()); | |
| 2295 | + new_tsec = selinux_cred(bprm->cred); | |
| 2340 | 2296 | isec = inode_security(inode); |
| 2341 | 2297 | |
| 2342 | 2298 | /* Default to the current task SID. */ |
| @@ -2500,7 +2456,7 @@ static void selinux_bprm_committing_creds(struct linux_binprm *bprm) · 157 unmodified lines | ||
| 2500 | 2456 | struct rlimit *rlim, *initrlim; |
| 2501 | 2457 | int rc, i; |
| 2502 | 2458 | |
| 2503 | − new_tsec = bprm->cred->security; | |
| 2459 | + new_tsec = selinux_cred(bprm->cred); | |
| 2504 | 2460 | if (new_tsec->sid == new_tsec->osid) |
| 2505 | 2461 | return; |
| 2506 | 2462 | |
| @@ -2543,7 +2499,7 @@ static void selinux_bprm_committing_creds(struct linux_binprm *bprm) · 36 unmodified lines | ||
| 2543 | 2499 | */ |
| 2544 | 2500 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2545 | 2501 | { |
| 2546 | − const struct task_security_struct *tsec = current_security(); | |
| 2502 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 2547 | 2503 | struct itimerval itimer; |
| 2548 | 2504 | u32 osid, sid; |
| 2549 | 2505 | int rc, i; |
| @@ -2780,7 +2736,7 @@ static int selinux_dentry_init_security(struct dentry *dentry, int mode, · 230 unmodified lines | ||
| 2780 | 2736 | u32 newsid; |
| 2781 | 2737 | int rc; |
| 2782 | 2738 | |
| 2783 | − rc = selinux_determine_inode_label(current_security(), | |
| 2739 | + rc = selinux_determine_inode_label(selinux_cred(current_cred()), | |
| 2784 | 2740 | d_inode(dentry->d_parent), name, |
| 2785 | 2741 | inode_mode_to_security_class(mode), |
| 2786 | 2742 | &newsid); |
| @@ -2800,14 +2756,14 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, · 13 unmodified lines | ||
| 2800 | 2756 | int rc; |
| 2801 | 2757 | struct task_security_struct *tsec; |
| 2802 | 2758 | |
| 2803 | − rc = selinux_determine_inode_label(old->security, | |
| 2759 | + rc = selinux_determine_inode_label(selinux_cred(old), | |
| 2804 | 2760 | d_inode(dentry->d_parent), name, |
| 2805 | 2761 | inode_mode_to_security_class(mode), |
| 2806 | 2762 | &newsid); |
| 2807 | 2763 | if (rc) |
| 2808 | 2764 | return rc; |
| 2809 | 2765 | |
| 2810 | − tsec = new->security; | |
| 2766 | + tsec = selinux_cred(new); | |
| 2811 | 2767 | tsec->create_sid = newsid; |
| 2812 | 2768 | return 0; |
| 2813 | 2769 | } |
| @@ -2817,7 +2773,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 3 unmodified lines | ||
| 2817 | 2773 | const char **name, |
| 2818 | 2774 | void **value, size_t *len) |
| 2819 | 2775 | { |
| 2820 | − const struct task_security_struct *tsec = current_security(); | |
| 2776 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 2821 | 2777 | struct superblock_security_struct *sbsec; |
| 2822 | 2778 | u32 newsid, clen; |
| 2823 | 2779 | int rc; |
| @@ -2827,7 +2783,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 3 unmodified lines | ||
| 2827 | 2783 | |
| 2828 | 2784 | newsid = tsec->create_sid; |
| 2829 | 2785 | |
| 2830 | − rc = selinux_determine_inode_label(current_security(), | |
| 2786 | + rc = selinux_determine_inode_label(selinux_cred(current_cred()), | |
| 2831 | 2787 | dir, qstr, |
| 2832 | 2788 | inode_mode_to_security_class(inode->i_mode), |
| 2833 | 2789 | &newsid); |
| @@ -2836,7 +2792,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 2 unmodified lines | ||
| 2836 | 2792 | |
| 2837 | 2793 | /* Possibly defer initialization to selinux_complete_init. */ |
| 2838 | 2794 | if (sbsec->flags & SE_SBINITIALIZED) { |
| 2839 | − struct inode_security_struct *isec = inode->i_security; | |
| 2795 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 2840 | 2796 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2841 | 2797 | isec->sid = newsid; |
| 2842 | 2798 | isec->initialized = LABEL_INITIALIZED; |
| @@ -2936,7 +2892,7 @@ static noinline int audit_inode_permission(struct inode *inode, · 93 unmodified lines | ||
| 2936 | 2892 | unsigned flags) |
| 2937 | 2893 | { |
| 2938 | 2894 | struct common_audit_data ad; |
| 2939 | − struct inode_security_struct *isec = inode->i_security; | |
| 2895 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 2940 | 2896 | int rc; |
| 2941 | 2897 | |
| 2942 | 2898 | ad.type = LSM_AUDIT_DATA_INODE; |
| @@ -3031,11 +2987,11 @@ static int selinux_inode_getattr(const struct path *path) · 88 unmodified lines | ||
| 3031 | 2987 | static bool has_cap_mac_admin(bool audit) |
| 3032 | 2988 | { |
| 3033 | 2989 | const struct cred *cred = current_cred(); |
| 3034 | − int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; | |
| 2990 | + unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT; | |
| 3035 | 2991 | |
| 3036 | − if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) | |
| 2992 | + if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts)) | |
| 3037 | 2993 | return false; |
| 3038 | − if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) | |
| 2994 | + if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true)) | |
| 3039 | 2995 | return false; |
| 3040 | 2996 | return true; |
| 3041 | 2997 | } |
| @@ -3289,7 +3245,7 @@ static int selinux_inode_copy_up(struct dentry *src, struct cred **new) · 247 unmodified lines | ||
| 3289 | 3245 | return -ENOMEM; |
| 3290 | 3246 | } |
| 3291 | 3247 | |
| 3292 | − tsec = new_creds->security; | |
| 3248 | + tsec = selinux_cred(new_creds); | |
| 3293 | 3249 | /* Get label from overlay inode and set it in create_sid */ |
| 3294 | 3250 | selinux_inode_getsecid(d_inode(src), &sid); |
| 3295 | 3251 | tsec->create_sid = sid; |
| @@ -3330,7 +3286,7 @@ static int selinux_revalidate_file_permission(struct file *file, int mask) · 34 unmodified lines | ||
| 3330 | 3286 | static int selinux_file_permission(struct file *file, int mask) |
| 3331 | 3287 | { |
| 3332 | 3288 | struct inode *inode = file_inode(file); |
| 3333 | − struct file_security_struct *fsec = file->f_security; | |
| 3289 | + struct file_security_struct *fsec = selinux_file(file); | |
| 3334 | 3290 | struct inode_security_struct *isec; |
| 3335 | 3291 | u32 sid = current_sid(); |
| 3336 | 3292 | |
| @@ -3352,11 +3308,6 @@ static int selinux_file_alloc_security(struct file *file) · 15 unmodified lines | ||
| 3352 | 3308 | return file_alloc_security(file); |
| 3353 | 3309 | } |
| 3354 | 3310 | |
| 3355 | −static void selinux_file_free_security(struct file *file) | |
| 3356 | −{ | |
| 3357 | − file_free_security(file); | |
| 3358 | −} | |
| 3359 | − | |
| 3360 | 3311 | /* |
| 3361 | 3312 | * Check whether a task has the ioctl permission and cmd |
| 3362 | 3313 | * operation to an inode. |
| @@ -3365,7 +3316,7 @@ static int ioctl_has_perm(const struct cred *cred, struct file *file, · 2 unmodified lines | ||
| 3365 | 3316 | u32 requested, u16 cmd) |
| 3366 | 3317 | { |
| 3367 | 3318 | struct common_audit_data ad; |
| 3368 | − struct file_security_struct *fsec = file->f_security; | |
| 3319 | + struct file_security_struct *fsec = selinux_file(file); | |
| 3369 | 3320 | struct inode *inode = file_inode(file); |
| 3370 | 3321 | struct inode_security_struct *isec; |
| 3371 | 3322 | struct lsm_ioctlop_audit ioctl; |
| @@ -3435,7 +3386,7 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd, · 63 unmodified lines | ||
| 3435 | 3386 | case KDSKBENT: |
| 3436 | 3387 | case KDSKBSENT: |
| 3437 | 3388 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
| 3438 | − SECURITY_CAP_AUDIT, true); | |
| 3389 | + CAP_OPT_NONE, true); | |
| 3439 | 3390 | break; |
| 3440 | 3391 | |
| 3441 | 3392 | /* default case assumes that the command will go |
| @@ -3617,7 +3568,7 @@ static void selinux_file_set_fowner(struct file *file) · 175 unmodified lines | ||
| 3617 | 3568 | { |
| 3618 | 3569 | struct file_security_struct *fsec; |
| 3619 | 3570 | |
| 3620 | − fsec = file->f_security; | |
| 3571 | + fsec = selinux_file(file); | |
| 3621 | 3572 | fsec->fown_sid = current_sid(); |
| 3622 | 3573 | } |
| 3623 | 3574 | |
| @@ -3632,7 +3583,7 @@ static int selinux_file_send_sigiotask(struct task_struct *tsk, · 8 unmodified lines | ||
| 3632 | 3583 | /* struct fown_struct is never outside the context of a struct file */ |
| 3633 | 3584 | file = container_of(fown, struct file, f_owner); |
| 3634 | 3585 | |
| 3635 | − fsec = file->f_security; | |
| 3586 | + fsec = selinux_file(file); | |
| 3636 | 3587 | |
| 3637 | 3588 | if (!signum) |
| 3638 | 3589 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| @@ -3656,7 +3607,7 @@ static int selinux_file_open(struct file *file) · 17 unmodified lines | ||
| 3656 | 3607 | struct file_security_struct *fsec; |
| 3657 | 3608 | struct inode_security_struct *isec; |
| 3658 | 3609 | |
| 3659 | − fsec = file->f_security; | |
| 3610 | + fsec = selinux_file(file); | |
| 3660 | 3611 | isec = inode_security(file_inode(file)); |
| 3661 | 3612 | /* |
| 3662 | 3613 | * Save inode label and policy sequence number |
| @@ -3689,53 +3640,16 @@ static int selinux_task_alloc(struct task_struct *task, · 26 unmodified lines | ||
| 3689 | 3640 | sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); |
| 3690 | 3641 | } |
| 3691 | 3642 | |
| 3692 | −/* | |
| 3693 | − * allocate the SELinux part of blank credentials | |
| 3694 | − */ | |
| 3695 | −static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) | |
| 3696 | −{ | |
| 3697 | − struct task_security_struct *tsec; | |
| 3698 | − | |
| 3699 | − tsec = kzalloc(sizeof(struct task_security_struct), gfp); | |
| 3700 | − if (!tsec) | |
| 3701 | − return -ENOMEM; | |
| 3702 | − | |
| 3703 | − cred->security = tsec; | |
| 3704 | − return 0; | |
| 3705 | −} | |
| 3706 | − | |
| 3707 | −/* | |
| 3708 | − * detach and free the LSM part of a set of credentials | |
| 3709 | − */ | |
| 3710 | −static void selinux_cred_free(struct cred *cred) | |
| 3711 | −{ | |
| 3712 | − struct task_security_struct *tsec = cred->security; | |
| 3713 | − | |
| 3714 | − /* | |
| 3715 | − * cred->security == NULL if security_cred_alloc_blank() or | |
| 3716 | − * security_prepare_creds() returned an error. | |
| 3717 | − */ | |
| 3718 | − BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); | |
| 3719 | − cred->security = (void *) 0x7UL; | |
| 3720 | − kfree(tsec); | |
| 3721 | −} | |
| 3722 | − | |
| 3723 | 3643 | /* |
| 3724 | 3644 | * prepare a new set of credentials for modification |
| 3725 | 3645 | */ |
| 3726 | 3646 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3727 | 3647 | gfp_t gfp) |
| 3728 | 3648 | { |
| 3729 | − const struct task_security_struct *old_tsec; | |
| 3730 | − struct task_security_struct *tsec; | |
| 3731 | − | |
| 3732 | − old_tsec = old->security; | |
| 3649 | + const struct task_security_struct *old_tsec = selinux_cred(old); | |
| 3650 | + struct task_security_struct *tsec = selinux_cred(new); | |
| 3733 | 3651 | |
| 3734 | − tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); | |
| 3735 | − if (!tsec) | |
| 3736 | − return -ENOMEM; | |
| 3737 | − | |
| 3738 | − new->security = tsec; | |
| 3652 | + *tsec = *old_tsec; | |
| 3739 | 3653 | return 0; |
| 3740 | 3654 | } |
| 3741 | 3655 | |
| @@ -3744,8 +3658,8 @@ static int selinux_cred_prepare(struct cred *new, const struct cred *old, · 2 unmodified lines | ||
| 3744 | 3658 | */ |
| 3745 | 3659 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3746 | 3660 | { |
| 3747 | − const struct task_security_struct *old_tsec = old->security; | |
| 3748 | − struct task_security_struct *tsec = new->security; | |
| 3661 | + const struct task_security_struct *old_tsec = selinux_cred(old); | |
| 3662 | + struct task_security_struct *tsec = selinux_cred(new); | |
| 3749 | 3663 | |
| 3750 | 3664 | *tsec = *old_tsec; |
| 3751 | 3665 | } |
| @@ -3761,7 +3675,7 @@ static void selinux_cred_getsecid(const struct cred *c, u32 *secid) · 9 unmodified lines | ||
| 3761 | 3675 | */ |
| 3762 | 3676 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3763 | 3677 | { |
| 3764 | − struct task_security_struct *tsec = new->security; | |
| 3678 | + struct task_security_struct *tsec = selinux_cred(new); | |
| 3765 | 3679 | u32 sid = current_sid(); |
| 3766 | 3680 | int ret; |
| 3767 | 3681 | |
| @@ -3786,7 +3700,7 @@ static int selinux_kernel_act_as(struct cred *new, u32 secid) · 18 unmodified lines | ||
| 3786 | 3700 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3787 | 3701 | { |
| 3788 | 3702 | struct inode_security_struct *isec = inode_security(inode); |
| 3789 | − struct task_security_struct *tsec = new->security; | |
| 3703 | + struct task_security_struct *tsec = selinux_cred(new); | |
| 3790 | 3704 | u32 sid = current_sid(); |
| 3791 | 3705 | int ret; |
| 3792 | 3706 | |
| @@ -3832,7 +3746,7 @@ static int selinux_kernel_module_from_file(struct file *file) · 39 unmodified lines | ||
| 3832 | 3746 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3833 | 3747 | ad.u.file = file; |
| 3834 | 3748 | |
| 3835 | − fsec = file->f_security; | |
| 3749 | + fsec = selinux_file(file); | |
| 3836 | 3750 | if (sid != fsec->sid) { |
| 3837 | 3751 | rc = avc_has_perm(&selinux_state, |
| 3838 | 3752 | sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); |
| @@ -3998,7 +3912,7 @@ static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, · 159 unmodified lines | ||
| 3998 | 3912 | static void selinux_task_to_inode(struct task_struct *p, |
| 3999 | 3913 | struct inode *inode) |
| 4000 | 3914 | { |
| 4001 | − struct inode_security_struct *isec = inode->i_security; | |
| 3915 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 4002 | 3916 | u32 sid = task_sid(p); |
| 4003 | 3917 | |
| 4004 | 3918 | spin_lock(&isec->lock); |
| @@ -4335,7 +4249,7 @@ static int sock_has_perm(struct sock *sk, u32 perms) · 330 unmodified lines | ||
| 4335 | 4249 | static int selinux_socket_create(int family, int type, |
| 4336 | 4250 | int protocol, int kern) |
| 4337 | 4251 | { |
| 4338 | − const struct task_security_struct *tsec = current_security(); | |
| 4252 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 4339 | 4253 | u32 newsid; |
| 4340 | 4254 | u16 secclass; |
| 4341 | 4255 | int rc; |
| @@ -4355,7 +4269,7 @@ static int selinux_socket_create(int family, int type, · 13 unmodified lines | ||
| 4355 | 4269 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 4356 | 4270 | int type, int protocol, int kern) |
| 4357 | 4271 | { |
| 4358 | − const struct task_security_struct *tsec = current_security(); | |
| 4272 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 4359 | 4273 | struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); |
| 4360 | 4274 | struct sk_security_struct *sksec; |
| 4361 | 4275 | u16 sclass = socket_type_to_security_class(family, type, protocol); |
| @@ -5236,7 +5150,7 @@ static int selinux_secmark_relabel_packet(u32 sid) · 874 unmodified lines | ||
| 5236 | 5150 | const struct task_security_struct *__tsec; |
| 5237 | 5151 | u32 tsid; |
| 5238 | 5152 | |
| 5239 | − __tsec = current_security(); | |
| 5153 | + __tsec = selinux_cred(current_cred()); | |
| 5240 | 5154 | tsid = __tsec->sid; |
| 5241 | 5155 | |
| 5242 | 5156 | return avc_has_perm(&selinux_state, |
| @@ -5711,51 +5625,22 @@ static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) · 468 unmodified lines | ||
| 5711 | 5625 | return selinux_nlmsg_perm(sk, skb); |
| 5712 | 5626 | } |
| 5713 | 5627 | |
| 5714 | −static int ipc_alloc_security(struct kern_ipc_perm *perm, | |
| 5715 | − u16 sclass) | |
| 5628 | +static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass) | |
| 5716 | 5629 | { |
| 5717 | − struct ipc_security_struct *isec; | |
| 5718 | − | |
| 5719 | − isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); | |
| 5720 | − if (!isec) | |
| 5721 | − return -ENOMEM; | |
| 5722 | − | |
| 5723 | 5630 | isec->sclass = sclass; |
| 5724 | 5631 | isec->sid = current_sid(); |
| 5725 | − perm->security = isec; | |
| 5726 | − | |
| 5727 | − return 0; | |
| 5728 | −} | |
| 5729 | − | |
| 5730 | −static void ipc_free_security(struct kern_ipc_perm *perm) | |
| 5731 | −{ | |
| 5732 | − struct ipc_security_struct *isec = perm->security; | |
| 5733 | − perm->security = NULL; | |
| 5734 | − kfree(isec); | |
| 5735 | 5632 | } |
| 5736 | 5633 | |
| 5737 | 5634 | static int msg_msg_alloc_security(struct msg_msg *msg) |
| 5738 | 5635 | { |
| 5739 | 5636 | struct msg_security_struct *msec; |
| 5740 | 5637 | |
| 5741 | − msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); | |
| 5742 | − if (!msec) | |
| 5743 | − return -ENOMEM; | |
| 5744 | − | |
| 5638 | + msec = selinux_msg_msg(msg); | |
| 5745 | 5639 | msec->sid = SECINITSID_UNLABELED; |
| 5746 | − msg->security = msec; | |
| 5747 | 5640 | |
| 5748 | 5641 | return 0; |
| 5749 | 5642 | } |
| 5750 | 5643 | |
| 5751 | −static void msg_msg_free_security(struct msg_msg *msg) | |
| 5752 | −{ | |
| 5753 | − struct msg_security_struct *msec = msg->security; | |
| 5754 | − | |
| 5755 | − msg->security = NULL; | |
| 5756 | − kfree(msec); | |
| 5757 | −} | |
| 5758 | − | |
| 5759 | 5644 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
| 5760 | 5645 | u32 perms) |
| 5761 | 5646 | { |
| @@ -5763,7 +5648,7 @@ static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, · 1 unmodified line | ||
| 5763 | 5648 | struct common_audit_data ad; |
| 5764 | 5649 | u32 sid = current_sid(); |
| 5765 | 5650 | |
| 5766 | − isec = ipc_perms->security; | |
| 5651 | + isec = selinux_ipc(ipc_perms); | |
| 5767 | 5652 | |
| 5768 | 5653 | ad.type = LSM_AUDIT_DATA_IPC; |
| 5769 | 5654 | ad.u.ipc_id = ipc_perms->key; |
| @@ -5777,11 +5662,6 @@ static int selinux_msg_msg_alloc_security(struct msg_msg *msg) · 7 unmodified lines | ||
| 5777 | 5662 | return msg_msg_alloc_security(msg); |
| 5778 | 5663 | } |
| 5779 | 5664 | |
| 5780 | −static void selinux_msg_msg_free_security(struct msg_msg *msg) | |
| 5781 | −{ | |
| 5782 | − msg_msg_free_security(msg); | |
| 5783 | −} | |
| 5784 | − | |
| 5785 | 5665 | /* message queue security operations */ |
| 5786 | 5666 | static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) |
| 5787 | 5667 | { |
| @@ -5790,11 +5670,8 @@ static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) · 2 unmodified lines | ||
| 5790 | 5670 | u32 sid = current_sid(); |
| 5791 | 5671 | int rc; |
| 5792 | 5672 | |
| 5793 | − rc = ipc_alloc_security(msq, SECCLASS_MSGQ); | |
| 5794 | − if (rc) | |
| 5795 | − return rc; | |
| 5796 | − | |
| 5797 | − isec = msq->security; | |
| 5673 | + isec = selinux_ipc(msq); | |
| 5674 | + ipc_init_security(isec, SECCLASS_MSGQ); | |
| 5798 | 5675 | |
| 5799 | 5676 | ad.type = LSM_AUDIT_DATA_IPC; |
| 5800 | 5677 | ad.u.ipc_id = msq->key; |
| @@ -5802,16 +5679,7 @@ static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) · 1 unmodified line | ||
| 5802 | 5679 | rc = avc_has_perm(&selinux_state, |
| 5803 | 5680 | sid, isec->sid, SECCLASS_MSGQ, |
| 5804 | 5681 | MSGQ__CREATE, &ad); |
| 5805 | − if (rc) { | |
| 5806 | − ipc_free_security(msq); | |
| 5807 | − return rc; | |
| 5808 | − } | |
| 5809 | − return 0; | |
| 5810 | −} | |
| 5811 | − | |
| 5812 | −static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) | |
| 5813 | −{ | |
| 5814 | − ipc_free_security(msq); | |
| 5682 | + return rc; | |
| 5815 | 5683 | } |
| 5816 | 5684 | |
| 5817 | 5685 | static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) |
| @@ -5820,7 +5688,7 @@ static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) · 2 unmodified lines | ||
| 5820 | 5688 | struct common_audit_data ad; |
| 5821 | 5689 | u32 sid = current_sid(); |
| 5822 | 5690 | |
| 5823 | − isec = msq->security; | |
| 5691 | + isec = selinux_ipc(msq); | |
| 5824 | 5692 | |
| 5825 | 5693 | ad.type = LSM_AUDIT_DATA_IPC; |
| 5826 | 5694 | ad.u.ipc_id = msq->key; |
| @@ -5869,8 +5737,8 @@ static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *m · 42 unmodified lines | ||
| 5869 | 5737 | u32 sid = current_sid(); |
| 5870 | 5738 | int rc; |
| 5871 | 5739 | |
| 5872 | − isec = msq->security; | |
| 5873 | − msec = msg->security; | |
| 5740 | + isec = selinux_ipc(msq); | |
| 5741 | + msec = selinux_msg_msg(msg); | |
| 5874 | 5742 | |
| 5875 | 5743 | /* |
| 5876 | 5744 | * First time through, need to assign label to the message |
| @@ -5917,8 +5785,8 @@ static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *m · 40 unmodified lines | ||
| 5917 | 5785 | u32 sid = task_sid(target); |
| 5918 | 5786 | int rc; |
| 5919 | 5787 | |
| 5920 | − isec = msq->security; | |
| 5921 | − msec = msg->security; | |
| 5788 | + isec = selinux_ipc(msq); | |
| 5789 | + msec = selinux_msg_msg(msg); | |
| 5922 | 5790 | |
| 5923 | 5791 | ad.type = LSM_AUDIT_DATA_IPC; |
| 5924 | 5792 | ad.u.ipc_id = msq->key; |
| @@ -5941,11 +5809,8 @@ static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) · 16 unmodified lines | ||
| 5941 | 5809 | u32 sid = current_sid(); |
| 5942 | 5810 | int rc; |
| 5943 | 5811 | |
| 5944 | − rc = ipc_alloc_security(shp, SECCLASS_SHM); | |
| 5945 | − if (rc) | |
| 5946 | − return rc; | |
| 5947 | − | |
| 5948 | − isec = shp->security; | |
| 5812 | + isec = selinux_ipc(shp); | |
| 5813 | + ipc_init_security(isec, SECCLASS_SHM); | |
| 5949 | 5814 | |
| 5950 | 5815 | ad.type = LSM_AUDIT_DATA_IPC; |
| 5951 | 5816 | ad.u.ipc_id = shp->key; |
| @@ -5953,16 +5818,7 @@ static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) · 1 unmodified line | ||
| 5953 | 5818 | rc = avc_has_perm(&selinux_state, |
| 5954 | 5819 | sid, isec->sid, SECCLASS_SHM, |
| 5955 | 5820 | SHM__CREATE, &ad); |
| 5956 | − if (rc) { | |
| 5957 | − ipc_free_security(shp); | |
| 5958 | − return rc; | |
| 5959 | − } | |
| 5960 | − return 0; | |
| 5961 | −} | |
| 5962 | − | |
| 5963 | −static void selinux_shm_free_security(struct kern_ipc_perm *shp) | |
| 5964 | −{ | |
| 5965 | − ipc_free_security(shp); | |
| 5821 | + return rc; | |
| 5966 | 5822 | } |
| 5967 | 5823 | |
| 5968 | 5824 | static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) |
| @@ -5971,7 +5827,7 @@ static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) · 2 unmodified lines | ||
| 5971 | 5827 | struct common_audit_data ad; |
| 5972 | 5828 | u32 sid = current_sid(); |
| 5973 | 5829 | |
| 5974 | − isec = shp->security; | |
| 5830 | + isec = selinux_ipc(shp); | |
| 5975 | 5831 | |
| 5976 | 5832 | ad.type = LSM_AUDIT_DATA_IPC; |
| 5977 | 5833 | ad.u.ipc_id = shp->key; |
| @@ -6038,11 +5894,8 @@ static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) · 60 unmodified lines | ||
| 6038 | 5894 | u32 sid = current_sid(); |
| 6039 | 5895 | int rc; |
| 6040 | 5896 | |
| 6041 | − rc = ipc_alloc_security(sma, SECCLASS_SEM); | |
| 6042 | − if (rc) | |
| 6043 | − return rc; | |
| 6044 | − | |
| 6045 | − isec = sma->security; | |
| 5897 | + isec = selinux_ipc(sma); | |
| 5898 | + ipc_init_security(isec, SECCLASS_SEM); | |
| 6046 | 5899 | |
| 6047 | 5900 | ad.type = LSM_AUDIT_DATA_IPC; |
| 6048 | 5901 | ad.u.ipc_id = sma->key; |
| @@ -6050,16 +5903,7 @@ static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) · 1 unmodified line | ||
| 6050 | 5903 | rc = avc_has_perm(&selinux_state, |
| 6051 | 5904 | sid, isec->sid, SECCLASS_SEM, |
| 6052 | 5905 | SEM__CREATE, &ad); |
| 6053 | − if (rc) { | |
| 6054 | − ipc_free_security(sma); | |
| 6055 | − return rc; | |
| 6056 | − } | |
| 6057 | − return 0; | |
| 6058 | −} | |
| 6059 | − | |
| 6060 | −static void selinux_sem_free_security(struct kern_ipc_perm *sma) | |
| 6061 | −{ | |
| 6062 | − ipc_free_security(sma); | |
| 5906 | + return rc; | |
| 6063 | 5907 | } |
| 6064 | 5908 | |
| 6065 | 5909 | static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) |
| @@ -6068,7 +5912,7 @@ static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) · 2 unmodified lines | ||
| 6068 | 5912 | struct common_audit_data ad; |
| 6069 | 5913 | u32 sid = current_sid(); |
| 6070 | 5914 | |
| 6071 | − isec = sma->security; | |
| 5915 | + isec = selinux_ipc(sma); | |
| 6072 | 5916 | |
| 6073 | 5917 | ad.type = LSM_AUDIT_DATA_IPC; |
| 6074 | 5918 | ad.u.ipc_id = sma->key; |
| @@ -6154,7 +5998,7 @@ static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) · 79 unmodified lines | ||
| 6154 | 5998 | |
| 6155 | 5999 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 6156 | 6000 | { |
| 6157 | − struct ipc_security_struct *isec = ipcp->security; | |
| 6001 | + struct ipc_security_struct *isec = selinux_ipc(ipcp); | |
| 6158 | 6002 | *secid = isec->sid; |
| 6159 | 6003 | } |
| 6160 | 6004 | |
| @@ -6173,7 +6017,7 @@ static int selinux_getprocattr(struct task_struct *p, · 12 unmodified lines | ||
| 6173 | 6017 | unsigned len; |
| 6174 | 6018 | |
| 6175 | 6019 | rcu_read_lock(); |
| 6176 | − __tsec = __task_cred(p)->security; | |
| 6020 | + __tsec = selinux_cred(__task_cred(p)); | |
| 6177 | 6021 | |
| 6178 | 6022 | if (current != p) { |
| 6179 | 6023 | error = avc_has_perm(&selinux_state, |
| @@ -6296,7 +6140,7 @@ static int selinux_setprocattr(const char *name, void *value, size_t size) · 116 unmodified lines | ||
| 6296 | 6140 | operation. See selinux_bprm_set_creds for the execve |
| 6297 | 6141 | checks and may_create for the file creation checks. The |
| 6298 | 6142 | operation will then fail if the context is not permitted. */ |
| 6299 | − tsec = new->security; | |
| 6143 | + tsec = selinux_cred(new); | |
| 6300 | 6144 | if (!strcmp(name, "exec")) { |
| 6301 | 6145 | tsec->exec_sid = sid; |
| 6302 | 6146 | } else if (!strcmp(name, "fscreate")) { |
| @@ -6380,7 +6224,7 @@ static void selinux_release_secctx(char *secdata, u32 seclen) · 77 unmodified lines | ||
| 6380 | 6224 | |
| 6381 | 6225 | static void selinux_inode_invalidate_secctx(struct inode *inode) |
| 6382 | 6226 | { |
| 6383 | − struct inode_security_struct *isec = inode->i_security; | |
| 6227 | + struct inode_security_struct *isec = selinux_inode(inode); | |
| 6384 | 6228 | |
| 6385 | 6229 | spin_lock(&isec->lock); |
| 6386 | 6230 | isec->initialized = LABEL_INVALID; |
| @@ -6425,7 +6269,7 @@ static int selinux_key_alloc(struct key *k, const struct cred *cred, · 38 unmodified lines | ||
| 6425 | 6269 | if (!ksec) |
| 6426 | 6270 | return -ENOMEM; |
| 6427 | 6271 | |
| 6428 | − tsec = cred->security; | |
| 6272 | + tsec = selinux_cred(cred); | |
| 6429 | 6273 | if (tsec->keycreate_sid) |
| 6430 | 6274 | ksec->sid = tsec->keycreate_sid; |
| 6431 | 6275 | else |
| @@ -6688,6 +6532,14 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · 256 unmodified lines | ||
| 6688 | 6532 | } |
| 6689 | 6533 | #endif |
| 6690 | 6534 | |
| 6535 | +struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { | |
| 6536 | + .lbs_cred = sizeof(struct task_security_struct), | |
| 6537 | + .lbs_file = sizeof(struct file_security_struct), | |
| 6538 | + .lbs_inode = sizeof(struct inode_security_struct), | |
| 6539 | + .lbs_ipc = sizeof(struct ipc_security_struct), | |
| 6540 | + .lbs_msg_msg = sizeof(struct msg_security_struct), | |
| 6541 | +}; | |
| 6542 | + | |
| 6691 | 6543 | static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { |
| 6692 | 6544 | LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), |
| 6693 | 6545 | LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), |
| @@ -6757,7 +6609,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 63 unmodified lines | ||
| 6757 | 6609 | |
| 6758 | 6610 | LSM_HOOK_INIT(file_permission, selinux_file_permission), |
| 6759 | 6611 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), |
| 6760 | − LSM_HOOK_INIT(file_free_security, selinux_file_free_security), | |
| 6761 | 6612 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), |
| 6762 | 6613 | LSM_HOOK_INIT(mmap_file, selinux_mmap_file), |
| 6763 | 6614 | LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), |
| @@ -6771,8 +6622,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 7 unmodified lines | ||
| 6771 | 6622 | LSM_HOOK_INIT(file_open, selinux_file_open), |
| 6772 | 6623 | |
| 6773 | 6624 | LSM_HOOK_INIT(task_alloc, selinux_task_alloc), |
| 6774 | − LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), | |
| 6775 | − LSM_HOOK_INIT(cred_free, selinux_cred_free), | |
| 6776 | 6625 | LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), |
| 6777 | 6626 | LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), |
| 6778 | 6627 | LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), |
| @@ -6800,24 +6649,20 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 21 unmodified lines | ||
| 6800 | 6649 | LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), |
| 6801 | 6650 | |
| 6802 | 6651 | LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), |
| 6803 | − LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), | |
| 6804 | 6652 | |
| 6805 | 6653 | LSM_HOOK_INIT(msg_queue_alloc_security, |
| 6806 | 6654 | selinux_msg_queue_alloc_security), |
| 6807 | − LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), | |
| 6808 | 6655 | LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), |
| 6809 | 6656 | LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), |
| 6810 | 6657 | LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), |
| 6811 | 6658 | LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), |
| 6812 | 6659 | |
| 6813 | 6660 | LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), |
| 6814 | − LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), | |
| 6815 | 6661 | LSM_HOOK_INIT(shm_associate, selinux_shm_associate), |
| 6816 | 6662 | LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), |
| 6817 | 6663 | LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), |
| 6818 | 6664 | |
| 6819 | 6665 | LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), |
| 6820 | − LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), | |
| 6821 | 6666 | LSM_HOOK_INIT(sem_associate, selinux_sem_associate), |
| 6822 | 6667 | LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), |
| 6823 | 6668 | LSM_HOOK_INIT(sem_semop, selinux_sem_semop), |
| @@ -6928,16 +6773,6 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 104 unmodified lines | ||
| 6928 | 6773 | |
| 6929 | 6774 | static __init int selinux_init(void) |
| 6930 | 6775 | { |
| 6931 | − if (!security_module_enable("selinux")) { | |
| 6932 | − selinux_enabled = 0; | |
| 6933 | − return 0; | |
| 6934 | − } | |
| 6935 | − | |
| 6936 | − if (!selinux_enabled) { | |
| 6937 | − pr_info("SELinux: Disabled at boot.\n"); | |
| 6938 | − return 0; | |
| 6939 | − } | |
| 6940 | − | |
| 6941 | 6776 | pr_info("SELinux: Initializing.\n"); |
| 6942 | 6777 | |
| 6943 | 6778 | memset(&selinux_state, 0, sizeof(selinux_state)); |
| @@ -6951,12 +6786,6 @@ static __init int selinux_init(void) · 7 unmodified lines | ||
| 6951 | 6786 | |
| 6952 | 6787 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 6953 | 6788 | |
| 6954 | − sel_inode_cache = kmem_cache_create("selinux_inode_security", | |
| 6955 | − sizeof(struct inode_security_struct), | |
| 6956 | − 0, SLAB_PANIC, NULL); | |
| 6957 | − file_security_cache = kmem_cache_create("selinux_file_security", | |
| 6958 | − sizeof(struct file_security_struct), | |
| 6959 | − 0, SLAB_PANIC, NULL); | |
| 6960 | 6789 | avc_init(); |
| 6961 | 6790 | |
| 6962 | 6791 | avtab_cache_init(); |
| @@ -6999,6 +6828,9 @@ void selinux_complete_init(void) · 36 unmodified lines | ||
| 6999 | 6828 | all processes and objects when they are created. */ |
| 7000 | 6829 | DEFINE_LSM(selinux) = { |
| 7001 | 6830 | .name = "selinux", |
| 6831 | + .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, | |
| 6832 | + .enabled = &selinux_enabled, | |
| 6833 | + .blobs = &selinux_blob_sizes, | |
| 7002 | 6834 | .init = selinux_init, |
| 7003 | 6835 | }; |
| 7004 | 6836 | |
| @@ -1,9 +1,6 @@ | ||
| 1 | 1 | /* |
| 2 | 2 | * SELinux support for the Audit LSM hooks |
| 3 | 3 | * |
| 4 | − * Most of below header was moved from include/linux/selinux.h which | |
| 5 | − * is released under below copyrights: | |
| 6 | − * | |
| 7 | 4 | * Author: James Morris <jmorris@redhat.com> |
| 8 | 5 | * |
| 9 | 6 | * Copyright (C) 2005 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| @@ -25,6 +25,8 @@ · 24 unmodified lines | ||
| 25 | 25 | #include <linux/binfmts.h> |
| 26 | 26 | #include <linux/in.h> |
| 27 | 27 | #include <linux/spinlock.h> |
| 28 | +#include <linux/lsm_hooks.h> | |
| 29 | +#include <linux/msg.h> | |
| 28 | 30 | #include <net/net_namespace.h> |
| 29 | 31 | #include "flask.h" |
| 30 | 32 | #include "avc.h" |
| @@ -56,10 +58,7 @@ enum label_initialized { · 25 unmodified lines | ||
| 56 | 58 | |
| 57 | 59 | struct inode_security_struct { |
| 58 | 60 | struct inode *inode; /* back pointer to inode object */ |
| 59 | − union { | |
| 60 | − struct list_head list; /* list of inode_security_struct */ | |
| 61 | − struct rcu_head rcu; /* for freeing the inode_security_struct */ | |
| 62 | − }; | |
| 61 | + struct list_head list; /* list of inode_security_struct */ | |
| 63 | 62 | u32 task_sid; /* SID of creating task */ |
| 64 | 63 | u32 sid; /* SID of this object */ |
| 65 | 64 | u16 sclass; /* security class of this object */ |
| @@ -158,4 +157,35 @@ struct bpf_security_struct { · 92 unmodified lines | ||
| 158 | 157 | u32 sid; /*SID of bpf obj creater*/ |
| 159 | 158 | }; |
| 160 | 159 | |
| 160 | +extern struct lsm_blob_sizes selinux_blob_sizes; | |
| 161 | +static inline struct task_security_struct *selinux_cred(const struct cred *cred) | |
| 162 | +{ | |
| 163 | + return cred->security + selinux_blob_sizes.lbs_cred; | |
| 164 | +} | |
| 165 | + | |
| 166 | +static inline struct file_security_struct *selinux_file(const struct file *file) | |
| 167 | +{ | |
| 168 | + return file->f_security + selinux_blob_sizes.lbs_file; | |
| 169 | +} | |
| 170 | + | |
| 171 | +static inline struct inode_security_struct *selinux_inode( | |
| 172 | + const struct inode *inode) | |
| 173 | +{ | |
| 174 | + if (unlikely(!inode->i_security)) | |
| 175 | + return NULL; | |
| 176 | + return inode->i_security + selinux_blob_sizes.lbs_inode; | |
| 177 | +} | |
| 178 | + | |
| 179 | +static inline struct msg_security_struct *selinux_msg_msg( | |
| 180 | + const struct msg_msg *msg_msg) | |
| 181 | +{ | |
| 182 | + return msg_msg->security + selinux_blob_sizes.lbs_msg_msg; | |
| 183 | +} | |
| 184 | + | |
| 185 | +static inline struct ipc_security_struct *selinux_ipc( | |
| 186 | + const struct kern_ipc_perm *ipc) | |
| 187 | +{ | |
| 188 | + return ipc->security + selinux_blob_sizes.lbs_ipc; | |
| 189 | +} | |
| 190 | + | |
| 161 | 191 | #endif /* _SELINUX_OBJSEC_H_ */ |
| @@ -1378,7 +1378,7 @@ static int sel_make_bools(struct selinux_fs_info *fsi) · 1377 unmodified lines | ||
| 1378 | 1378 | goto out; |
| 1379 | 1379 | } |
| 1380 | 1380 | |
| 1381 | − isec = (struct inode_security_struct *)inode->i_security; | |
| 1381 | + isec = selinux_inode(inode); | |
| 1382 | 1382 | ret = security_genfs_sid(fsi->state, "selinuxfs", page, |
| 1383 | 1383 | SECCLASS_FILE, &sid); |
| 1384 | 1384 | if (ret) { |
| @@ -1953,7 +1953,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) · 568 unmodified lines | ||
| 1953 | 1953 | } |
| 1954 | 1954 | |
| 1955 | 1955 | inode->i_ino = ++fsi->last_ino; |
| 1956 | − isec = (struct inode_security_struct *)inode->i_security; | |
| 1956 | + isec = selinux_inode(inode); | |
| 1957 | 1957 | isec->sid = SECINITSID_DEVNULL; |
| 1958 | 1958 | isec->sclass = SECCLASS_CHR_FILE; |
| 1959 | 1959 | isec->initialized = LABEL_INITIALIZED; |
| @@ -49,7 +49,6 @@ · 48 unmodified lines | ||
| 49 | 49 | #include <linux/sched.h> |
| 50 | 50 | #include <linux/audit.h> |
| 51 | 51 | #include <linux/mutex.h> |
| 52 | −#include <linux/selinux.h> | |
| 53 | 52 | #include <linux/flex_array.h> |
| 54 | 53 | #include <linux/vmalloc.h> |
| 55 | 54 | #include <net/netlabel.h> |
| @@ -79,7 +79,7 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp, · 78 unmodified lines | ||
| 79 | 79 | gfp_t gfp) |
| 80 | 80 | { |
| 81 | 81 | int rc; |
| 82 | − const struct task_security_struct *tsec = current_security(); | |
| 82 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 83 | 83 | struct xfrm_sec_ctx *ctx = NULL; |
| 84 | 84 | u32 str_len; |
| 85 | 85 | |
| @@ -138,7 +138,7 @@ static void selinux_xfrm_free(struct xfrm_sec_ctx *ctx) · 52 unmodified lines | ||
| 138 | 138 | */ |
| 139 | 139 | static int selinux_xfrm_delete(struct xfrm_sec_ctx *ctx) |
| 140 | 140 | { |
| 141 | − const struct task_security_struct *tsec = current_security(); | |
| 141 | + const struct task_security_struct *tsec = selinux_cred(current_cred()); | |
| 142 | 142 | |
| 143 | 143 | if (!ctx) |
| 144 | 144 | return 0; |
| @@ -24,6 +24,7 @@ · 23 unmodified lines | ||
| 24 | 24 | #include <linux/list.h> |
| 25 | 25 | #include <linux/rculist.h> |
| 26 | 26 | #include <linux/lsm_audit.h> |
| 27 | +#include <linux/msg.h> | |
| 27 | 28 | |
| 28 | 29 | /* |
| 29 | 30 | * Use IPv6 port labeling if IPv6 is enabled and secmarks |
| @@ -336,6 +337,7 @@ extern struct smack_known *smack_syslog_label; · 306 unmodified lines | ||
| 336 | 337 | extern struct smack_known *smack_unconfined; |
| 337 | 338 | #endif |
| 338 | 339 | extern int smack_ptrace_rule; |
| 340 | +extern struct lsm_blob_sizes smack_blob_sizes; | |
| 339 | 341 | |
| 340 | 342 | extern struct smack_known smack_known_floor; |
| 341 | 343 | extern struct smack_known smack_known_hat; |
| @@ -356,12 +358,38 @@ extern struct list_head smack_onlycap_list; · 14 unmodified lines | ||
| 356 | 358 | #define SMACK_HASH_SLOTS 16 |
| 357 | 359 | extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS]; |
| 358 | 360 | |
| 361 | +static inline struct task_smack *smack_cred(const struct cred *cred) | |
| 362 | +{ | |
| 363 | + return cred->security + smack_blob_sizes.lbs_cred; | |
| 364 | +} | |
| 365 | + | |
| 366 | +static inline struct smack_known **smack_file(const struct file *file) | |
| 367 | +{ | |
| 368 | + return (struct smack_known **)(file->f_security + | |
| 369 | + smack_blob_sizes.lbs_file); | |
| 370 | +} | |
| 371 | + | |
| 372 | +static inline struct inode_smack *smack_inode(const struct inode *inode) | |
| 373 | +{ | |
| 374 | + return inode->i_security + smack_blob_sizes.lbs_inode; | |
| 375 | +} | |
| 376 | + | |
| 377 | +static inline struct smack_known **smack_msg_msg(const struct msg_msg *msg) | |
| 378 | +{ | |
| 379 | + return msg->security + smack_blob_sizes.lbs_msg_msg; | |
| 380 | +} | |
| 381 | + | |
| 382 | +static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc) | |
| 383 | +{ | |
| 384 | + return ipc->security + smack_blob_sizes.lbs_ipc; | |
| 385 | +} | |
| 386 | + | |
| 359 | 387 | /* |
| 360 | 388 | * Is the directory transmuting? |
| 361 | 389 | */ |
| 362 | 390 | static inline int smk_inode_transmutable(const struct inode *isp) |
| 363 | 391 | { |
| 364 | − struct inode_smack *sip = isp->i_security; | |
| 392 | + struct inode_smack *sip = smack_inode(isp); | |
| 365 | 393 | return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0; |
| 366 | 394 | } |
| 367 | 395 | |
| @@ -370,7 +398,7 @@ static inline int smk_inode_transmutable(const struct inode *isp) · 2 unmodified lines | ||
| 370 | 398 | */ |
| 371 | 399 | static inline struct smack_known *smk_of_inode(const struct inode *isp) |
| 372 | 400 | { |
| 373 | − struct inode_smack *sip = isp->i_security; | |
| 401 | + struct inode_smack *sip = smack_inode(isp); | |
| 374 | 402 | return sip->smk_inode; |
| 375 | 403 | } |
| 376 | 404 | |
| @@ -382,13 +410,19 @@ static inline struct smack_known *smk_of_task(const struct task_smack *tsp) · 5 unmodified lines | ||
| 382 | 410 | return tsp->smk_task; |
| 383 | 411 | } |
| 384 | 412 | |
| 385 | −static inline struct smack_known *smk_of_task_struct(const struct task_struct *t) | |
| 413 | +static inline struct smack_known *smk_of_task_struct( | |
| 414 | + const struct task_struct *t) | |
| 386 | 415 | { |
| 387 | 416 | struct smack_known *skp; |
| 417 | + const struct cred *cred; | |
| 388 | 418 | |
| 389 | 419 | rcu_read_lock(); |
| 390 | − skp = smk_of_task(__task_cred(t)->security); | |
| 420 | + | |
| 421 | + cred = __task_cred(t); | |
| 422 | + skp = smk_of_task(smack_cred(cred)); | |
| 423 | + | |
| 391 | 424 | rcu_read_unlock(); |
| 425 | + | |
| 392 | 426 | return skp; |
| 393 | 427 | } |
| 394 | 428 | |
| @@ -405,7 +439,7 @@ static inline struct smack_known *smk_of_forked(const struct task_smack *tsp) · 10 unmodified lines | ||
| 405 | 439 | */ |
| 406 | 440 | static inline struct smack_known *smk_of_current(void) |
| 407 | 441 | { |
| 408 | − return smk_of_task(current_security()); | |
| 442 | + return smk_of_task(smack_cred(current_cred())); | |
| 409 | 443 | } |
| 410 | 444 | |
| 411 | 445 | /* |
| @@ -275,7 +275,7 @@ out_audit: · 274 unmodified lines | ||
| 275 | 275 | int smk_curacc(struct smack_known *obj_known, |
| 276 | 276 | u32 mode, struct smk_audit_info *a) |
| 277 | 277 | { |
| 278 | − struct task_smack *tsp = current_security(); | |
| 278 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 279 | 279 | |
| 280 | 280 | return smk_tskacc(tsp, obj_known, mode, a); |
| 281 | 281 | } |
| @@ -635,12 +635,12 @@ DEFINE_MUTEX(smack_onlycap_lock); · 353 unmodified lines | ||
| 635 | 635 | */ |
| 636 | 636 | bool smack_privileged_cred(int cap, const struct cred *cred) |
| 637 | 637 | { |
| 638 | − struct task_smack *tsp = cred->security; | |
| 638 | + struct task_smack *tsp = smack_cred(cred); | |
| 639 | 639 | struct smack_known *skp = tsp->smk_task; |
| 640 | 640 | struct smack_known_list_elem *sklep; |
| 641 | 641 | int rc; |
| 642 | 642 | |
| 643 | − rc = cap_capable(cred, &init_user_ns, cap, SECURITY_CAP_AUDIT); | |
| 643 | + rc = cap_capable(cred, &init_user_ns, cap, CAP_OPT_NONE); | |
| 644 | 644 | if (rc) |
| 645 | 645 | return false; |
| 646 | 646 | |
| @@ -139,7 +139,7 @@ static int smk_bu_note(char *note, struct smack_known *sskp, · 138 unmodified lines | ||
| 139 | 139 | static int smk_bu_current(char *note, struct smack_known *oskp, |
| 140 | 140 | int mode, int rc) |
| 141 | 141 | { |
| 142 | − struct task_smack *tsp = current_security(); | |
| 142 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 143 | 143 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 144 | 144 | |
| 145 | 145 | if (rc <= 0) |
| @@ -160,7 +160,7 @@ static int smk_bu_current(char *note, struct smack_known *oskp, · 14 unmodified lines | ||
| 160 | 160 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 161 | 161 | static int smk_bu_task(struct task_struct *otp, int mode, int rc) |
| 162 | 162 | { |
| 163 | − struct task_smack *tsp = current_security(); | |
| 163 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 164 | 164 | struct smack_known *smk_task = smk_of_task_struct(otp); |
| 165 | 165 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 166 | 166 | |
| @@ -182,8 +182,8 @@ static int smk_bu_task(struct task_struct *otp, int mode, int rc) · 15 unmodified lines | ||
| 182 | 182 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 183 | 183 | static int smk_bu_inode(struct inode *inode, int mode, int rc) |
| 184 | 184 | { |
| 185 | − struct task_smack *tsp = current_security(); | |
| 186 | − struct inode_smack *isp = inode->i_security; | |
| 185 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 186 | + struct inode_smack *isp = smack_inode(inode); | |
| 187 | 187 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 188 | 188 | |
| 189 | 189 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| @@ -212,10 +212,10 @@ static int smk_bu_inode(struct inode *inode, int mode, int rc) · 22 unmodified lines | ||
| 212 | 212 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
| 213 | 213 | static int smk_bu_file(struct file *file, int mode, int rc) |
| 214 | 214 | { |
| 215 | − struct task_smack *tsp = current_security(); | |
| 215 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 216 | 216 | struct smack_known *sskp = tsp->smk_task; |
| 217 | 217 | struct inode *inode = file_inode(file); |
| 218 | − struct inode_smack *isp = inode->i_security; | |
| 218 | + struct inode_smack *isp = smack_inode(inode); | |
| 219 | 219 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 220 | 220 | |
| 221 | 221 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| @@ -242,10 +242,10 @@ static int smk_bu_file(struct file *file, int mode, int rc) · 20 unmodified lines | ||
| 242 | 242 | static int smk_bu_credfile(const struct cred *cred, struct file *file, |
| 243 | 243 | int mode, int rc) |
| 244 | 244 | { |
| 245 | − struct task_smack *tsp = cred->security; | |
| 245 | + struct task_smack *tsp = smack_cred(cred); | |
| 246 | 246 | struct smack_known *sskp = tsp->smk_task; |
| 247 | 247 | struct inode *inode = file_inode(file); |
| 248 | − struct inode_smack *isp = inode->i_security; | |
| 248 | + struct inode_smack *isp = smack_inode(inode); | |
| 249 | 249 | char acc[SMK_NUM_ACCESS_TYPE + 1]; |
| 250 | 250 | |
| 251 | 251 | if (isp->smk_flags & SMK_INODE_IMPURE) |
| @@ -305,50 +305,35 @@ static struct smack_known *smk_fetch(const char *name, struct inode *ip, · 53 unmodified lines | ||
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | /** |
| 308 | − * new_inode_smack - allocate an inode security blob | |
| 308 | + * init_inode_smack - initialize an inode security blob | |
| 309 | + * @isp: the blob to initialize | |
| 309 | 310 | * @skp: a pointer to the Smack label entry to use in the blob |
| 310 | 311 | * |
| 311 | − * Returns the new blob or NULL if there's no memory available | |
| 312 | 312 | */ |
| 313 | −static struct inode_smack *new_inode_smack(struct smack_known *skp) | |
| 313 | +static void init_inode_smack(struct inode *inode, struct smack_known *skp) | |
| 314 | 314 | { |
| 315 | − struct inode_smack *isp; | |
| 316 | − | |
| 317 | − isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS); | |
| 318 | − if (isp == NULL) | |
| 319 | − return NULL; | |
| 315 | + struct inode_smack *isp = smack_inode(inode); | |
| 320 | 316 | |
| 321 | 317 | isp->smk_inode = skp; |
| 322 | 318 | isp->smk_flags = 0; |
| 323 | 319 | mutex_init(&isp->smk_lock); |
| 324 | − | |
| 325 | − return isp; | |
| 326 | 320 | } |
| 327 | 321 | |
| 328 | 322 | /** |
| 329 | − * new_task_smack - allocate a task security blob | |
| 323 | + * init_task_smack - initialize a task security blob | |
| 324 | + * @tsp: blob to initialize | |
| 330 | 325 | * @task: a pointer to the Smack label for the running task |
| 331 | 326 | * @forked: a pointer to the Smack label for the forked task |
| 332 | − * @gfp: type of the memory for the allocation | |
| 333 | 327 | * |
| 334 | − * Returns the new blob or NULL if there's no memory available | |
| 335 | 328 | */ |
| 336 | −static struct task_smack *new_task_smack(struct smack_known *task, | |
| 337 | − struct smack_known *forked, gfp_t gfp) | |
| 329 | +static void init_task_smack(struct task_smack *tsp, struct smack_known *task, | |
| 330 | + struct smack_known *forked) | |
| 338 | 331 | { |
| 339 | − struct task_smack *tsp; | |
| 340 | − | |
| 341 | − tsp = kzalloc(sizeof(struct task_smack), gfp); | |
| 342 | − if (tsp == NULL) | |
| 343 | − return NULL; | |
| 344 | − | |
| 345 | 332 | tsp->smk_task = task; |
| 346 | 333 | tsp->smk_forked = forked; |
| 347 | 334 | INIT_LIST_HEAD(&tsp->smk_rules); |
| 348 | 335 | INIT_LIST_HEAD(&tsp->smk_relabel); |
| 349 | 336 | mutex_init(&tsp->smk_rules_lock); |
| 350 | − | |
| 351 | − return tsp; | |
| 352 | 337 | } |
| 353 | 338 | |
| 354 | 339 | /** |
| @@ -448,7 +433,7 @@ static int smk_ptrace_rule_check(struct task_struct *tracer, · 93 unmodified lines | ||
| 448 | 433 | |
| 449 | 434 | rcu_read_lock(); |
| 450 | 435 | tracercred = __task_cred(tracer); |
| 451 | − tsp = tracercred->security; | |
| 436 | + tsp = smack_cred(tracercred); | |
| 452 | 437 | tracer_known = smk_of_task(tsp); |
| 453 | 438 | |
| 454 | 439 | if ((mode & PTRACE_MODE_ATTACH) && |
| @@ -515,7 +500,7 @@ static int smack_ptrace_traceme(struct task_struct *ptp) · 60 unmodified lines | ||
| 515 | 500 | int rc; |
| 516 | 501 | struct smack_known *skp; |
| 517 | 502 | |
| 518 | − skp = smk_of_task(current_security()); | |
| 503 | + skp = smk_of_task(smack_cred(current_cred())); | |
| 519 | 504 | |
| 520 | 505 | rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__); |
| 521 | 506 | return rc; |
| @@ -718,6 +703,13 @@ static int smack_set_mnt_opts(struct super_block *sb, · 196 unmodified lines | ||
| 718 | 703 | if (sp->smk_flags & SMK_SB_INITIALIZED) |
| 719 | 704 | return 0; |
| 720 | 705 | |
| 706 | + if (inode->i_security == NULL) { | |
| 707 | + int rc = lsm_inode_alloc(inode); | |
| 708 | + | |
| 709 | + if (rc) | |
| 710 | + return rc; | |
| 711 | + } | |
| 712 | + | |
| 721 | 713 | if (!smack_privileged(CAP_MAC_ADMIN)) { |
| 722 | 714 | /* |
| 723 | 715 | * Unprivileged mounts don't get to specify Smack values. |
| @@ -782,17 +774,12 @@ static int smack_set_mnt_opts(struct super_block *sb, · 58 unmodified lines | ||
| 782 | 774 | /* |
| 783 | 775 | * Initialize the root inode. |
| 784 | 776 | */ |
| 785 | − isp = inode->i_security; | |
| 786 | − if (isp == NULL) { | |
| 787 | − isp = new_inode_smack(sp->smk_root); | |
| 788 | − if (isp == NULL) | |
| 789 | − return -ENOMEM; | |
| 790 | − inode->i_security = isp; | |
| 791 | − } else | |
| 792 | − isp->smk_inode = sp->smk_root; | |
| 777 | + init_inode_smack(inode, sp->smk_root); | |
| 793 | 778 | |
| 794 | − if (transmute) | |
| 779 | + if (transmute) { | |
| 780 | + isp = smack_inode(inode); | |
| 795 | 781 | isp->smk_flags |= SMK_INODE_TRANSMUTE; |
| 782 | + } | |
| 796 | 783 | |
| 797 | 784 | return 0; |
| 798 | 785 | } |
| @@ -831,7 +818,7 @@ static int smack_sb_statfs(struct dentry *dentry) · 32 unmodified lines | ||
| 831 | 818 | static int smack_bprm_set_creds(struct linux_binprm *bprm) |
| 832 | 819 | { |
| 833 | 820 | struct inode *inode = file_inode(bprm->file); |
| 834 | − struct task_smack *bsp = bprm->cred->security; | |
| 821 | + struct task_smack *bsp = smack_cred(bprm->cred); | |
| 835 | 822 | struct inode_smack *isp; |
| 836 | 823 | struct superblock_smack *sbsp; |
| 837 | 824 | int rc; |
| @@ -839,7 +826,7 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm) · 1 unmodified line | ||
| 839 | 826 | if (bprm->called_set_creds) |
| 840 | 827 | return 0; |
| 841 | 828 | |
| 842 | − isp = inode->i_security; | |
| 829 | + isp = smack_inode(inode); | |
| 843 | 830 | if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) |
| 844 | 831 | return 0; |
| 845 | 832 | |
| @@ -890,48 +877,10 @@ static int smack_inode_alloc_security(struct inode *inode) · 44 unmodified lines | ||
| 890 | 877 | { |
| 891 | 878 | struct smack_known *skp = smk_of_current(); |
| 892 | 879 | |
| 893 | − inode->i_security = new_inode_smack(skp); | |
| 894 | − if (inode->i_security == NULL) | |
| 895 | − return -ENOMEM; | |
| 880 | + init_inode_smack(inode, skp); | |
| 896 | 881 | return 0; |
| 897 | 882 | } |
| 898 | 883 | |
| 899 | −/** | |
| 900 | − * smack_inode_free_rcu - Free inode_smack blob from cache | |
| 901 | − * @head: the rcu_head for getting inode_smack pointer | |
| 902 | − * | |
| 903 | − * Call back function called from call_rcu() to free | |
| 904 | − * the i_security blob pointer in inode | |
| 905 | − */ | |
| 906 | −static void smack_inode_free_rcu(struct rcu_head *head) | |
| 907 | −{ | |
| 908 | − struct inode_smack *issp; | |
| 909 | − | |
| 910 | − issp = container_of(head, struct inode_smack, smk_rcu); | |
| 911 | − kmem_cache_free(smack_inode_cache, issp); | |
| 912 | −} | |
| 913 | − | |
| 914 | −/** | |
| 915 | − * smack_inode_free_security - free an inode blob using call_rcu() | |
| 916 | − * @inode: the inode with a blob | |
| 917 | − * | |
| 918 | − * Clears the blob pointer in inode using RCU | |
| 919 | − */ | |
| 920 | −static void smack_inode_free_security(struct inode *inode) | |
| 921 | −{ | |
| 922 | − struct inode_smack *issp = inode->i_security; | |
| 923 | − | |
| 924 | − /* | |
| 925 | − * The inode may still be referenced in a path walk and | |
| 926 | − * a call to smack_inode_permission() can be made | |
| 927 | − * after smack_inode_free_security() is called. | |
| 928 | − * To avoid race condition free the i_security via RCU | |
| 929 | − * and leave the current inode->i_security pointer intact. | |
| 930 | − * The inode will be freed after the RCU grace period too. | |
| 931 | − */ | |
| 932 | − call_rcu(&issp->smk_rcu, smack_inode_free_rcu); | |
| 933 | −} | |
| 934 | − | |
| 935 | 884 | /** |
| 936 | 885 | * smack_inode_init_security - copy out the smack from an inode |
| 937 | 886 | * @inode: the newly created inode |
| @@ -947,7 +896,7 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir, · 9 unmodified lines | ||
| 947 | 896 | const struct qstr *qstr, const char **name, |
| 948 | 897 | void **value, size_t *len) |
| 949 | 898 | { |
| 950 | − struct inode_smack *issp = inode->i_security; | |
| 899 | + struct inode_smack *issp = smack_inode(inode); | |
| 951 | 900 | struct smack_known *skp = smk_of_current(); |
| 952 | 901 | struct smack_known *isp = smk_of_inode(inode); |
| 953 | 902 | struct smack_known *dsp = smk_of_inode(dir); |
| @@ -1285,7 +1234,7 @@ static void smack_inode_post_setxattr(struct dentry *dentry, const char *name, · 331 unmodified lines | ||
| 1285 | 1234 | const void *value, size_t size, int flags) |
| 1286 | 1235 | { |
| 1287 | 1236 | struct smack_known *skp; |
| 1288 | − struct inode_smack *isp = d_backing_inode(dentry)->i_security; | |
| 1237 | + struct inode_smack *isp = smack_inode(d_backing_inode(dentry)); | |
| 1289 | 1238 | |
| 1290 | 1239 | if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { |
| 1291 | 1240 | isp->smk_flags |= SMK_INODE_TRANSMUTE; |
| @@ -1366,7 +1315,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name) · 74 unmodified lines | ||
| 1366 | 1315 | if (rc != 0) |
| 1367 | 1316 | return rc; |
| 1368 | 1317 | |
| 1369 | − isp = d_backing_inode(dentry)->i_security; | |
| 1318 | + isp = smack_inode(d_backing_inode(dentry)); | |
| 1370 | 1319 | /* |
| 1371 | 1320 | * Don't do anything special for these. |
| 1372 | 1321 | * XATTR_NAME_SMACKIPIN |
| @@ -1498,24 +1447,12 @@ static void smack_inode_getsecid(struct inode *inode, u32 *secid) · 125 unmodified lines | ||
| 1498 | 1447 | */ |
| 1499 | 1448 | static int smack_file_alloc_security(struct file *file) |
| 1500 | 1449 | { |
| 1501 | − struct smack_known *skp = smk_of_current(); | |
| 1450 | + struct smack_known **blob = smack_file(file); | |
| 1502 | 1451 | |
| 1503 | − file->f_security = skp; | |
| 1452 | + *blob = smk_of_current(); | |
| 1504 | 1453 | return 0; |
| 1505 | 1454 | } |
| 1506 | 1455 | |
| 1507 | −/** | |
| 1508 | − * smack_file_free_security - clear a file security blob | |
| 1509 | − * @file: the object | |
| 1510 | − * | |
| 1511 | − * The security blob for a file is a pointer to the master | |
| 1512 | − * label list, so no memory is freed. | |
| 1513 | − */ | |
| 1514 | −static void smack_file_free_security(struct file *file) | |
| 1515 | −{ | |
| 1516 | − file->f_security = NULL; | |
| 1517 | −} | |
| 1518 | − | |
| 1519 | 1456 | /** |
| 1520 | 1457 | * smack_file_ioctl - Smack check on ioctls |
| 1521 | 1458 | * @file: the object |
| @@ -1653,7 +1590,7 @@ static int smack_mmap_file(struct file *file, · 131 unmodified lines | ||
| 1653 | 1590 | if (unlikely(IS_PRIVATE(file_inode(file)))) |
| 1654 | 1591 | return 0; |
| 1655 | 1592 | |
| 1656 | − isp = file_inode(file)->i_security; | |
| 1593 | + isp = smack_inode(file_inode(file)); | |
| 1657 | 1594 | if (isp->smk_mmap == NULL) |
| 1658 | 1595 | return 0; |
| 1659 | 1596 | sbsp = file_inode(file)->i_sb->s_security; |
| @@ -1662,7 +1599,7 @@ static int smack_mmap_file(struct file *file, · 2 unmodified lines | ||
| 1662 | 1599 | return -EACCES; |
| 1663 | 1600 | mkp = isp->smk_mmap; |
| 1664 | 1601 | |
| 1665 | − tsp = current_security(); | |
| 1602 | + tsp = smack_cred(current_cred()); | |
| 1666 | 1603 | skp = smk_of_current(); |
| 1667 | 1604 | rc = 0; |
| 1668 | 1605 | |
| @@ -1740,7 +1677,9 @@ static int smack_mmap_file(struct file *file, · 71 unmodified lines | ||
| 1740 | 1677 | */ |
| 1741 | 1678 | static void smack_file_set_fowner(struct file *file) |
| 1742 | 1679 | { |
| 1743 | − file->f_security = smk_of_current(); | |
| 1680 | + struct smack_known **blob = smack_file(file); | |
| 1681 | + | |
| 1682 | + *blob = smk_of_current(); | |
| 1744 | 1683 | } |
| 1745 | 1684 | |
| 1746 | 1685 | /** |
| @@ -1757,8 +1696,9 @@ static void smack_file_set_fowner(struct file *file) · 10 unmodified lines | ||
| 1757 | 1696 | static int smack_file_send_sigiotask(struct task_struct *tsk, |
| 1758 | 1697 | struct fown_struct *fown, int signum) |
| 1759 | 1698 | { |
| 1699 | + struct smack_known **blob; | |
| 1760 | 1700 | struct smack_known *skp; |
| 1761 | − struct smack_known *tkp = smk_of_task(tsk->cred->security); | |
| 1701 | + struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred)); | |
| 1762 | 1702 | const struct cred *tcred; |
| 1763 | 1703 | struct file *file; |
| 1764 | 1704 | int rc; |
| @@ -1770,7 +1710,8 @@ static int smack_file_send_sigiotask(struct task_struct *tsk, · 5 unmodified lines | ||
| 1770 | 1710 | file = container_of(fown, struct file, f_owner); |
| 1771 | 1711 | |
| 1772 | 1712 | /* we don't log here as rc can be overriden */ |
| 1773 | − skp = file->f_security; | |
| 1713 | + blob = smack_file(file); | |
| 1714 | + skp = *blob; | |
| 1774 | 1715 | rc = smk_access(skp, tkp, MAY_DELIVER, NULL); |
| 1775 | 1716 | rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc); |
| 1776 | 1717 | |
| @@ -1811,7 +1752,7 @@ static int smack_file_receive(struct file *file) · 34 unmodified lines | ||
| 1811 | 1752 | if (inode->i_sb->s_magic == SOCKFS_MAGIC) { |
| 1812 | 1753 | sock = SOCKET_I(inode); |
| 1813 | 1754 | ssp = sock->sk->sk_security; |
| 1814 | − tsp = current_security(); | |
| 1755 | + tsp = smack_cred(current_cred()); | |
| 1815 | 1756 | /* |
| 1816 | 1757 | * If the receiving process can't write to the |
| 1817 | 1758 | * passed socket or if the passed socket can't |
| @@ -1853,7 +1794,7 @@ static int smack_file_receive(struct file *file) · 35 unmodified lines | ||
| 1853 | 1794 | */ |
| 1854 | 1795 | static int smack_file_open(struct file *file) |
| 1855 | 1796 | { |
| 1856 | − struct task_smack *tsp = file->f_cred->security; | |
| 1797 | + struct task_smack *tsp = smack_cred(file->f_cred); | |
| 1857 | 1798 | struct inode *inode = file_inode(file); |
| 1858 | 1799 | struct smk_audit_info ad; |
| 1859 | 1800 | int rc; |
| @@ -1881,14 +1822,7 @@ static int smack_file_open(struct file *file) · 21 unmodified lines | ||
| 1881 | 1822 | */ |
| 1882 | 1823 | static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 1883 | 1824 | { |
| 1884 | − struct task_smack *tsp; | |
| 1885 | − | |
| 1886 | − tsp = new_task_smack(NULL, NULL, gfp); | |
| 1887 | − if (tsp == NULL) | |
| 1888 | − return -ENOMEM; | |
| 1889 | − | |
| 1890 | − cred->security = tsp; | |
| 1891 | − | |
| 1825 | + init_task_smack(smack_cred(cred), NULL, NULL); | |
| 1892 | 1826 | return 0; |
| 1893 | 1827 | } |
| 1894 | 1828 | |
| @@ -1900,15 +1834,11 @@ static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) · 5 unmodified lines | ||
| 1900 | 1834 | */ |
| 1901 | 1835 | static void smack_cred_free(struct cred *cred) |
| 1902 | 1836 | { |
| 1903 | − struct task_smack *tsp = cred->security; | |
| 1837 | + struct task_smack *tsp = smack_cred(cred); | |
| 1904 | 1838 | struct smack_rule *rp; |
| 1905 | 1839 | struct list_head *l; |
| 1906 | 1840 | struct list_head *n; |
| 1907 | 1841 | |
| 1908 | − if (tsp == NULL) | |
| 1909 | − return; | |
| 1910 | − cred->security = NULL; | |
| 1911 | − | |
| 1912 | 1842 | smk_destroy_label_list(&tsp->smk_relabel); |
| 1913 | 1843 | |
| 1914 | 1844 | list_for_each_safe(l, n, &tsp->smk_rules) { |
| @@ -1916,7 +1846,6 @@ static void smack_cred_free(struct cred *cred) · 1 unmodified line | ||
| 1916 | 1846 | list_del(&rp->list); |
| 1917 | 1847 | kfree(rp); |
| 1918 | 1848 | } |
| 1919 | − kfree(tsp); | |
| 1920 | 1849 | } |
| 1921 | 1850 | |
| 1922 | 1851 | /** |
| @@ -1930,15 +1859,11 @@ static void smack_cred_free(struct cred *cred) · 7 unmodified lines | ||
| 1930 | 1859 | static int smack_cred_prepare(struct cred *new, const struct cred *old, |
| 1931 | 1860 | gfp_t gfp) |
| 1932 | 1861 | { |
| 1933 | − struct task_smack *old_tsp = old->security; | |
| 1934 | − struct task_smack *new_tsp; | |
| 1862 | + struct task_smack *old_tsp = smack_cred(old); | |
| 1863 | + struct task_smack *new_tsp = smack_cred(new); | |
| 1935 | 1864 | int rc; |
| 1936 | 1865 | |
| 1937 | − new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp); | |
| 1938 | − if (new_tsp == NULL) | |
| 1939 | − return -ENOMEM; | |
| 1940 | − | |
| 1941 | − new->security = new_tsp; | |
| 1866 | + init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task); | |
| 1942 | 1867 | |
| 1943 | 1868 | rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp); |
| 1944 | 1869 | if (rc != 0) |
| @@ -1946,10 +1871,7 @@ static int smack_cred_prepare(struct cred *new, const struct cred *old, · 1 unmodified line | ||
| 1946 | 1871 | |
| 1947 | 1872 | rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel, |
| 1948 | 1873 | gfp); |
| 1949 | − if (rc != 0) | |
| 1950 | − return rc; | |
| 1951 | − | |
| 1952 | − return 0; | |
| 1874 | + return rc; | |
| 1953 | 1875 | } |
| 1954 | 1876 | |
| 1955 | 1877 | /** |
| @@ -1961,15 +1883,14 @@ static int smack_cred_prepare(struct cred *new, const struct cred *old, · 5 unmodified lines | ||
| 1961 | 1883 | */ |
| 1962 | 1884 | static void smack_cred_transfer(struct cred *new, const struct cred *old) |
| 1963 | 1885 | { |
| 1964 | − struct task_smack *old_tsp = old->security; | |
| 1965 | − struct task_smack *new_tsp = new->security; | |
| 1886 | + struct task_smack *old_tsp = smack_cred(old); | |
| 1887 | + struct task_smack *new_tsp = smack_cred(new); | |
| 1966 | 1888 | |
| 1967 | 1889 | new_tsp->smk_task = old_tsp->smk_task; |
| 1968 | 1890 | new_tsp->smk_forked = old_tsp->smk_task; |
| 1969 | 1891 | mutex_init(&new_tsp->smk_rules_lock); |
| 1970 | 1892 | INIT_LIST_HEAD(&new_tsp->smk_rules); |
| 1971 | 1893 | |
| 1972 | − | |
| 1973 | 1894 | /* cbs copy rule list */ |
| 1974 | 1895 | } |
| 1975 | 1896 | |
| @@ -1980,12 +1901,12 @@ static void smack_cred_transfer(struct cred *new, const struct cred *old) · 4 unmodified lines | ||
| 1980 | 1901 | * |
| 1981 | 1902 | * Sets the secid to contain a u32 version of the smack label. |
| 1982 | 1903 | */ |
| 1983 | −static void smack_cred_getsecid(const struct cred *c, u32 *secid) | |
| 1904 | +static void smack_cred_getsecid(const struct cred *cred, u32 *secid) | |
| 1984 | 1905 | { |
| 1985 | 1906 | struct smack_known *skp; |
| 1986 | 1907 | |
| 1987 | 1908 | rcu_read_lock(); |
| 1988 | − skp = smk_of_task(c->security); | |
| 1909 | + skp = smk_of_task(smack_cred(cred)); | |
| 1989 | 1910 | *secid = skp->smk_secid; |
| 1990 | 1911 | rcu_read_unlock(); |
| 1991 | 1912 | } |
| @@ -1999,7 +1920,7 @@ static void smack_cred_getsecid(const struct cred *c, u32 *secid) · 7 unmodified lines | ||
| 1999 | 1920 | */ |
| 2000 | 1921 | static int smack_kernel_act_as(struct cred *new, u32 secid) |
| 2001 | 1922 | { |
| 2002 | − struct task_smack *new_tsp = new->security; | |
| 1923 | + struct task_smack *new_tsp = smack_cred(new); | |
| 2003 | 1924 | |
| 2004 | 1925 | new_tsp->smk_task = smack_from_secid(secid); |
| 2005 | 1926 | return 0; |
| @@ -2016,8 +1937,8 @@ static int smack_kernel_act_as(struct cred *new, u32 secid) · 10 unmodified lines | ||
| 2016 | 1937 | static int smack_kernel_create_files_as(struct cred *new, |
| 2017 | 1938 | struct inode *inode) |
| 2018 | 1939 | { |
| 2019 | − struct inode_smack *isp = inode->i_security; | |
| 2020 | − struct task_smack *tsp = new->security; | |
| 1940 | + struct inode_smack *isp = smack_inode(inode); | |
| 1941 | + struct task_smack *tsp = smack_cred(new); | |
| 2021 | 1942 | |
| 2022 | 1943 | tsp->smk_forked = isp->smk_inode; |
| 2023 | 1944 | tsp->smk_task = tsp->smk_forked; |
| @@ -2201,7 +2122,7 @@ static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info, · 177 unmodified lines | ||
| 2201 | 2122 | * specific behavior. This is not clean. For one thing |
| 2202 | 2123 | * we can't take privilege into account. |
| 2203 | 2124 | */ |
| 2204 | − skp = smk_of_task(cred->security); | |
| 2125 | + skp = smk_of_task(smack_cred(cred)); | |
| 2205 | 2126 | rc = smk_access(skp, tkp, MAY_DELIVER, &ad); |
| 2206 | 2127 | rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc); |
| 2207 | 2128 | return rc; |
| @@ -2216,7 +2137,7 @@ static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info, · 8 unmodified lines | ||
| 2216 | 2137 | */ |
| 2217 | 2138 | static void smack_task_to_inode(struct task_struct *p, struct inode *inode) |
| 2218 | 2139 | { |
| 2219 | − struct inode_smack *isp = inode->i_security; | |
| 2140 | + struct inode_smack *isp = smack_inode(inode); | |
| 2220 | 2141 | struct smack_known *skp = smk_of_task_struct(p); |
| 2221 | 2142 | |
| 2222 | 2143 | isp->smk_inode = skp; |
| @@ -2679,7 +2600,7 @@ static int smack_inode_setsecurity(struct inode *inode, const char *name, · 456 unmodified lines | ||
| 2679 | 2600 | const void *value, size_t size, int flags) |
| 2680 | 2601 | { |
| 2681 | 2602 | struct smack_known *skp; |
| 2682 | − struct inode_smack *nsp = inode->i_security; | |
| 2603 | + struct inode_smack *nsp = smack_inode(inode); | |
| 2683 | 2604 | struct socket_smack *ssp; |
| 2684 | 2605 | struct socket *sock; |
| 2685 | 2606 | int rc = 0; |
| @@ -2888,23 +2809,12 @@ static int smack_flags_to_may(int flags) · 202 unmodified lines | ||
| 2888 | 2809 | */ |
| 2889 | 2810 | static int smack_msg_msg_alloc_security(struct msg_msg *msg) |
| 2890 | 2811 | { |
| 2891 | − struct smack_known *skp = smk_of_current(); | |
| 2812 | + struct smack_known **blob = smack_msg_msg(msg); | |
| 2892 | 2813 | |
| 2893 | − msg->security = skp; | |
| 2814 | + *blob = smk_of_current(); | |
| 2894 | 2815 | return 0; |
| 2895 | 2816 | } |
| 2896 | 2817 | |
| 2897 | −/** | |
| 2898 | − * smack_msg_msg_free_security - Clear the security blob for msg_msg | |
| 2899 | − * @msg: the object | |
| 2900 | − * | |
| 2901 | − * Clears the blob pointer | |
| 2902 | − */ | |
| 2903 | −static void smack_msg_msg_free_security(struct msg_msg *msg) | |
| 2904 | −{ | |
| 2905 | − msg->security = NULL; | |
| 2906 | −} | |
| 2907 | − | |
| 2908 | 2818 | /** |
| 2909 | 2819 | * smack_of_ipc - the smack pointer for the ipc |
| 2910 | 2820 | * @isp: the object |
| @@ -2913,7 +2823,9 @@ static void smack_msg_msg_free_security(struct msg_msg *msg) · 2 unmodified lines | ||
| 2913 | 2823 | */ |
| 2914 | 2824 | static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp) |
| 2915 | 2825 | { |
| 2916 | − return (struct smack_known *)isp->security; | |
| 2826 | + struct smack_known **blob = smack_ipc(isp); | |
| 2827 | + | |
| 2828 | + return *blob; | |
| 2917 | 2829 | } |
| 2918 | 2830 | |
| 2919 | 2831 | /** |
| @@ -2924,23 +2836,12 @@ static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp) · 4 unmodified lines | ||
| 2924 | 2836 | */ |
| 2925 | 2837 | static int smack_ipc_alloc_security(struct kern_ipc_perm *isp) |
| 2926 | 2838 | { |
| 2927 | − struct smack_known *skp = smk_of_current(); | |
| 2839 | + struct smack_known **blob = smack_ipc(isp); | |
| 2928 | 2840 | |
| 2929 | − isp->security = skp; | |
| 2841 | + *blob = smk_of_current(); | |
| 2930 | 2842 | return 0; |
| 2931 | 2843 | } |
| 2932 | 2844 | |
| 2933 | −/** | |
| 2934 | − * smack_ipc_free_security - Clear the security blob for ipc | |
| 2935 | − * @isp: the object | |
| 2936 | − * | |
| 2937 | − * Clears the blob pointer | |
| 2938 | − */ | |
| 2939 | −static void smack_ipc_free_security(struct kern_ipc_perm *isp) | |
| 2940 | −{ | |
| 2941 | − isp->security = NULL; | |
| 2942 | −} | |
| 2943 | − | |
| 2944 | 2845 | /** |
| 2945 | 2846 | * smk_curacc_shm : check if current has access on shm |
| 2946 | 2847 | * @isp : the object |
| @@ -3238,7 +3139,8 @@ static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg · 291 unmodified lines | ||
| 3238 | 3139 | */ |
| 3239 | 3140 | static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) |
| 3240 | 3141 | { |
| 3241 | − struct smack_known *iskp = ipp->security; | |
| 3142 | + struct smack_known **blob = smack_ipc(ipp); | |
| 3143 | + struct smack_known *iskp = *blob; | |
| 3242 | 3144 | int may = smack_flags_to_may(flag); |
| 3243 | 3145 | struct smk_audit_info ad; |
| 3244 | 3146 | int rc; |
| @@ -3259,7 +3161,8 @@ static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) · 14 unmodified lines | ||
| 3259 | 3161 | */ |
| 3260 | 3162 | static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid) |
| 3261 | 3163 | { |
| 3262 | − struct smack_known *iskp = ipp->security; | |
| 3164 | + struct smack_known **blob = smack_ipc(ipp); | |
| 3165 | + struct smack_known *iskp = *blob; | |
| 3263 | 3166 | |
| 3264 | 3167 | *secid = iskp->smk_secid; |
| 3265 | 3168 | } |
| @@ -3287,7 +3190,7 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) · 21 unmodified lines | ||
| 3287 | 3190 | if (inode == NULL) |
| 3288 | 3191 | return; |
| 3289 | 3192 | |
| 3290 | − isp = inode->i_security; | |
| 3193 | + isp = smack_inode(inode); | |
| 3291 | 3194 | |
| 3292 | 3195 | mutex_lock(&isp->smk_lock); |
| 3293 | 3196 | /* |
| @@ -3390,13 +3293,12 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) · 96 unmodified lines | ||
| 3390 | 3293 | */ |
| 3391 | 3294 | final = &smack_known_star; |
| 3392 | 3295 | /* |
| 3393 | − * Fall through. | |
| 3394 | − * | |
| 3395 | 3296 | * If a smack value has been set we want to use it, |
| 3396 | 3297 | * but since tmpfs isn't giving us the opportunity |
| 3397 | 3298 | * to set mount options simulate setting the |
| 3398 | 3299 | * superblock default. |
| 3399 | 3300 | */ |
| 3301 | + /* Fall through */ | |
| 3400 | 3302 | default: |
| 3401 | 3303 | /* |
| 3402 | 3304 | * This isn't an understood special case. |
| @@ -3528,7 +3430,7 @@ static int smack_getprocattr(struct task_struct *p, char *name, char **value) · 125 unmodified lines | ||
| 3528 | 3430 | */ |
| 3529 | 3431 | static int smack_setprocattr(const char *name, void *value, size_t size) |
| 3530 | 3432 | { |
| 3531 | − struct task_smack *tsp = current_security(); | |
| 3433 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 3532 | 3434 | struct cred *new; |
| 3533 | 3435 | struct smack_known *skp; |
| 3534 | 3436 | struct smack_known_list_elem *sklep; |
| @@ -3569,7 +3471,7 @@ static int smack_setprocattr(const char *name, void *value, size_t size) · 34 unmodified lines | ||
| 3569 | 3471 | if (new == NULL) |
| 3570 | 3472 | return -ENOMEM; |
| 3571 | 3473 | |
| 3572 | − tsp = new->security; | |
| 3474 | + tsp = smack_cred(new); | |
| 3573 | 3475 | tsp->smk_task = skp; |
| 3574 | 3476 | /* |
| 3575 | 3477 | * process can change its label only once |
| @@ -4214,7 +4116,7 @@ static void smack_inet_csk_clone(struct sock *sk, · 638 unmodified lines | ||
| 4214 | 4116 | static int smack_key_alloc(struct key *key, const struct cred *cred, |
| 4215 | 4117 | unsigned long flags) |
| 4216 | 4118 | { |
| 4217 | − struct smack_known *skp = smk_of_task(cred->security); | |
| 4119 | + struct smack_known *skp = smk_of_task(smack_cred(cred)); | |
| 4218 | 4120 | |
| 4219 | 4121 | key->security = skp; |
| 4220 | 4122 | return 0; |
| @@ -4245,7 +4147,7 @@ static int smack_key_permission(key_ref_t key_ref, · 24 unmodified lines | ||
| 4245 | 4147 | { |
| 4246 | 4148 | struct key *keyp; |
| 4247 | 4149 | struct smk_audit_info ad; |
| 4248 | − struct smack_known *tkp = smk_of_task(cred->security); | |
| 4150 | + struct smack_known *tkp = smk_of_task(smack_cred(cred)); | |
| 4249 | 4151 | int request = 0; |
| 4250 | 4152 | int rc; |
| 4251 | 4153 | |
| @@ -4520,12 +4422,12 @@ static int smack_inode_copy_up(struct dentry *dentry, struct cred **new) · 268 unmodified lines | ||
| 4520 | 4422 | return -ENOMEM; |
| 4521 | 4423 | } |
| 4522 | 4424 | |
| 4523 | − tsp = new_creds->security; | |
| 4425 | + tsp = smack_cred(new_creds); | |
| 4524 | 4426 | |
| 4525 | 4427 | /* |
| 4526 | 4428 | * Get label from overlay inode and set it in create_sid |
| 4527 | 4429 | */ |
| 4528 | − isp = d_inode(dentry->d_parent)->i_security; | |
| 4430 | + isp = smack_inode(d_inode(dentry->d_parent)); | |
| 4529 | 4431 | skp = isp->smk_inode; |
| 4530 | 4432 | tsp->smk_task = skp; |
| 4531 | 4433 | *new = new_creds; |
| @@ -4548,8 +4450,8 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode, · 16 unmodified lines | ||
| 4548 | 4450 | const struct cred *old, |
| 4549 | 4451 | struct cred *new) |
| 4550 | 4452 | { |
| 4551 | − struct task_smack *otsp = old->security; | |
| 4552 | − struct task_smack *ntsp = new->security; | |
| 4453 | + struct task_smack *otsp = smack_cred(old); | |
| 4454 | + struct task_smack *ntsp = smack_cred(new); | |
| 4553 | 4455 | struct inode_smack *isp; |
| 4554 | 4456 | int may; |
| 4555 | 4457 | |
| @@ -4562,7 +4464,7 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode, · 6 unmodified lines | ||
| 4562 | 4464 | /* |
| 4563 | 4465 | * the attribute of the containing directory |
| 4564 | 4466 | */ |
| 4565 | − isp = d_inode(dentry->d_parent)->i_security; | |
| 4467 | + isp = smack_inode(d_inode(dentry->d_parent)); | |
| 4566 | 4468 | |
| 4567 | 4469 | if (isp->smk_flags & SMK_INODE_TRANSMUTE) { |
| 4568 | 4470 | rcu_read_lock(); |
| @@ -4582,6 +4484,14 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode, · 13 unmodified lines | ||
| 4582 | 4484 | return 0; |
| 4583 | 4485 | } |
| 4584 | 4486 | |
| 4487 | +struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = { | |
| 4488 | + .lbs_cred = sizeof(struct task_smack), | |
| 4489 | + .lbs_file = sizeof(struct smack_known *), | |
| 4490 | + .lbs_inode = sizeof(struct inode_smack), | |
| 4491 | + .lbs_ipc = sizeof(struct smack_known *), | |
| 4492 | + .lbs_msg_msg = sizeof(struct smack_known *), | |
| 4493 | +}; | |
| 4494 | + | |
| 4585 | 4495 | static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { |
| 4586 | 4496 | LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check), |
| 4587 | 4497 | LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme), |
| @@ -4597,7 +4507,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { · 9 unmodified lines | ||
| 4597 | 4507 | LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds), |
| 4598 | 4508 | |
| 4599 | 4509 | LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security), |
| 4600 | − LSM_HOOK_INIT(inode_free_security, smack_inode_free_security), | |
| 4601 | 4510 | LSM_HOOK_INIT(inode_init_security, smack_inode_init_security), |
| 4602 | 4511 | LSM_HOOK_INIT(inode_link, smack_inode_link), |
| 4603 | 4512 | LSM_HOOK_INIT(inode_unlink, smack_inode_unlink), |
| @@ -4616,7 +4525,6 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { · 12 unmodified lines | ||
| 4616 | 4525 | LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid), |
| 4617 | 4526 | |
| 4618 | 4527 | LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security), |
| 4619 | − LSM_HOOK_INIT(file_free_security, smack_file_free_security), | |
| 4620 | 4528 | LSM_HOOK_INIT(file_ioctl, smack_file_ioctl), |
| 4621 | 4529 | LSM_HOOK_INIT(file_lock, smack_file_lock), |
| 4622 | 4530 | LSM_HOOK_INIT(file_fcntl, smack_file_fcntl), |
| @@ -4652,23 +4560,19 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { · 29 unmodified lines | ||
| 4652 | 4560 | LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid), |
| 4653 | 4561 | |
| 4654 | 4562 | LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security), |
| 4655 | − LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security), | |
| 4656 | 4563 | |
| 4657 | 4564 | LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security), |
| 4658 | − LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security), | |
| 4659 | 4565 | LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate), |
| 4660 | 4566 | LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl), |
| 4661 | 4567 | LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd), |
| 4662 | 4568 | LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv), |
| 4663 | 4569 | |
| 4664 | 4570 | LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security), |
| 4665 | − LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security), | |
| 4666 | 4571 | LSM_HOOK_INIT(shm_associate, smack_shm_associate), |
| 4667 | 4572 | LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl), |
| 4668 | 4573 | LSM_HOOK_INIT(shm_shmat, smack_shm_shmat), |
| 4669 | 4574 | |
| 4670 | 4575 | LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security), |
| 4671 | − LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security), | |
| 4672 | 4576 | LSM_HOOK_INIT(sem_associate, smack_sem_associate), |
| 4673 | 4577 | LSM_HOOK_INIT(sem_semctl, smack_sem_semctl), |
| 4674 | 4578 | LSM_HOOK_INIT(sem_semop, smack_sem_semop), |
| @@ -4759,23 +4663,23 @@ static __init void init_smack_known_list(void) · 84 unmodified lines | ||
| 4759 | 4663 | */ |
| 4760 | 4664 | static __init int smack_init(void) |
| 4761 | 4665 | { |
| 4762 | − struct cred *cred; | |
| 4666 | + struct cred *cred = (struct cred *) current->cred; | |
| 4763 | 4667 | struct task_smack *tsp; |
| 4764 | 4668 | |
| 4765 | − if (!security_module_enable("smack")) | |
| 4766 | − return 0; | |
| 4767 | − | |
| 4768 | 4669 | smack_inode_cache = KMEM_CACHE(inode_smack, 0); |
| 4769 | 4670 | if (!smack_inode_cache) |
| 4770 | 4671 | return -ENOMEM; |
| 4771 | 4672 | |
| 4772 | − tsp = new_task_smack(&smack_known_floor, &smack_known_floor, | |
| 4773 | − GFP_KERNEL); | |
| 4774 | − if (tsp == NULL) { | |
| 4775 | − kmem_cache_destroy(smack_inode_cache); | |
| 4776 | − return -ENOMEM; | |
| 4777 | − } | |
| 4673 | + /* | |
| 4674 | + * Set the security state for the initial task. | |
| 4675 | + */ | |
| 4676 | + tsp = smack_cred(cred); | |
| 4677 | + init_task_smack(tsp, &smack_known_floor, &smack_known_floor); | |
| 4778 | 4678 | |
| 4679 | + /* | |
| 4680 | + * Register with LSM | |
| 4681 | + */ | |
| 4682 | + security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack"); | |
| 4779 | 4683 | smack_enabled = 1; |
| 4780 | 4684 | |
| 4781 | 4685 | pr_info("Smack: Initializing.\n"); |
| @@ -4789,20 +4693,9 @@ static __init int smack_init(void) · 7 unmodified lines | ||
| 4789 | 4693 | pr_info("Smack: IPv6 Netfilter enabled.\n"); |
| 4790 | 4694 | #endif |
| 4791 | 4695 | |
| 4792 | − /* | |
| 4793 | − * Set the security state for the initial task. | |
| 4794 | − */ | |
| 4795 | − cred = (struct cred *) current->cred; | |
| 4796 | − cred->security = tsp; | |
| 4797 | − | |
| 4798 | 4696 | /* initialize the smack_known_list */ |
| 4799 | 4697 | init_smack_known_list(); |
| 4800 | 4698 | |
| 4801 | − /* | |
| 4802 | − * Register with LSM | |
| 4803 | − */ | |
| 4804 | − security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack"); | |
| 4805 | − | |
| 4806 | 4699 | return 0; |
| 4807 | 4700 | } |
| 4808 | 4701 | |
| @@ -4812,5 +4705,7 @@ static __init int smack_init(void) · 3 unmodified lines | ||
| 4812 | 4705 | */ |
| 4813 | 4706 | DEFINE_LSM(smack) = { |
| 4814 | 4707 | .name = "smack", |
| 4708 | + .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, | |
| 4709 | + .blobs = &smack_blob_sizes, | |
| 4815 | 4710 | .init = smack_init, |
| 4816 | 4711 | }; |
| @@ -2208,14 +2208,14 @@ static const struct file_operations smk_logging_ops = { · 2207 unmodified lines | ||
| 2208 | 2208 | |
| 2209 | 2209 | static void *load_self_seq_start(struct seq_file *s, loff_t *pos) |
| 2210 | 2210 | { |
| 2211 | − struct task_smack *tsp = current_security(); | |
| 2211 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2212 | 2212 | |
| 2213 | 2213 | return smk_seq_start(s, pos, &tsp->smk_rules); |
| 2214 | 2214 | } |
| 2215 | 2215 | |
| 2216 | 2216 | static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2217 | 2217 | { |
| 2218 | − struct task_smack *tsp = current_security(); | |
| 2218 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2219 | 2219 | |
| 2220 | 2220 | return smk_seq_next(s, v, pos, &tsp->smk_rules); |
| 2221 | 2221 | } |
| @@ -2262,7 +2262,7 @@ static int smk_open_load_self(struct inode *inode, struct file *file) · 40 unmodified lines | ||
| 2262 | 2262 | static ssize_t smk_write_load_self(struct file *file, const char __user *buf, |
| 2263 | 2263 | size_t count, loff_t *ppos) |
| 2264 | 2264 | { |
| 2265 | − struct task_smack *tsp = current_security(); | |
| 2265 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2266 | 2266 | |
| 2267 | 2267 | return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, |
| 2268 | 2268 | &tsp->smk_rules_lock, SMK_FIXED24_FMT); |
| @@ -2414,14 +2414,14 @@ static const struct file_operations smk_load2_ops = { · 145 unmodified lines | ||
| 2414 | 2414 | |
| 2415 | 2415 | static void *load_self2_seq_start(struct seq_file *s, loff_t *pos) |
| 2416 | 2416 | { |
| 2417 | − struct task_smack *tsp = current_security(); | |
| 2417 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2418 | 2418 | |
| 2419 | 2419 | return smk_seq_start(s, pos, &tsp->smk_rules); |
| 2420 | 2420 | } |
| 2421 | 2421 | |
| 2422 | 2422 | static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2423 | 2423 | { |
| 2424 | − struct task_smack *tsp = current_security(); | |
| 2424 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2425 | 2425 | |
| 2426 | 2426 | return smk_seq_next(s, v, pos, &tsp->smk_rules); |
| 2427 | 2427 | } |
| @@ -2467,7 +2467,7 @@ static int smk_open_load_self2(struct inode *inode, struct file *file) · 39 unmodified lines | ||
| 2467 | 2467 | static ssize_t smk_write_load_self2(struct file *file, const char __user *buf, |
| 2468 | 2468 | size_t count, loff_t *ppos) |
| 2469 | 2469 | { |
| 2470 | − struct task_smack *tsp = current_security(); | |
| 2470 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2471 | 2471 | |
| 2472 | 2472 | return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, |
| 2473 | 2473 | &tsp->smk_rules_lock, SMK_LONG_FMT); |
| @@ -2681,14 +2681,14 @@ static const struct file_operations smk_syslog_ops = { · 207 unmodified lines | ||
| 2681 | 2681 | |
| 2682 | 2682 | static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos) |
| 2683 | 2683 | { |
| 2684 | − struct task_smack *tsp = current_security(); | |
| 2684 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2685 | 2685 | |
| 2686 | 2686 | return smk_seq_start(s, pos, &tsp->smk_relabel); |
| 2687 | 2687 | } |
| 2688 | 2688 | |
| 2689 | 2689 | static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2690 | 2690 | { |
| 2691 | − struct task_smack *tsp = current_security(); | |
| 2691 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2692 | 2692 | |
| 2693 | 2693 | return smk_seq_next(s, v, pos, &tsp->smk_relabel); |
| 2694 | 2694 | } |
| @@ -2736,7 +2736,7 @@ static int smk_open_relabel_self(struct inode *inode, struct file *file) · 41 unmodified lines | ||
| 2736 | 2736 | static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf, |
| 2737 | 2737 | size_t count, loff_t *ppos) |
| 2738 | 2738 | { |
| 2739 | − struct task_smack *tsp = current_security(); | |
| 2739 | + struct task_smack *tsp = smack_cred(current_cred()); | |
| 2740 | 2740 | char *data; |
| 2741 | 2741 | int rc; |
| 2742 | 2742 | LIST_HEAD(list_tmp); |
| @@ -32,6 +32,7 @@ static char *tomoyo_print_bprm(struct linux_binprm *bprm, · 31 unmodified lines | ||
| 32 | 32 | int argv_count = bprm->argc; |
| 33 | 33 | int envp_count = bprm->envc; |
| 34 | 34 | bool truncated = false; |
| 35 | + | |
| 35 | 36 | if (!buffer) |
| 36 | 37 | return NULL; |
| 37 | 38 | len = snprintf(buffer, tomoyo_buffer_len - 1, "argv[]={ "); |
| @@ -49,6 +50,7 @@ static char *tomoyo_print_bprm(struct linux_binprm *bprm, · 11 unmodified lines | ||
| 49 | 50 | while (offset < PAGE_SIZE) { |
| 50 | 51 | const char *kaddr = dump->data; |
| 51 | 52 | const unsigned char c = kaddr[offset++]; |
| 53 | + | |
| 52 | 54 | if (cp == last_start) |
| 53 | 55 | *cp++ = '"'; |
| 54 | 56 | if (cp >= buffer + tomoyo_buffer_len - 32) { |
| @@ -154,19 +156,18 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 99 unmodified lines | ||
| 154 | 156 | char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS); |
| 155 | 157 | int pos; |
| 156 | 158 | u8 i; |
| 159 | + | |
| 157 | 160 | if (!buffer) |
| 158 | 161 | return NULL; |
| 159 | 162 | |
| 160 | 163 | tomoyo_convert_time(ktime_get_real_seconds(), &stamp); |
| 161 | 164 | |
| 162 | 165 | pos = snprintf(buffer, tomoyo_buffer_len - 1, |
| 163 | − "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s " | |
| 164 | − "granted=%s (global-pid=%u) task={ pid=%u ppid=%u " | |
| 165 | − "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u " | |
| 166 | − "fsuid=%u fsgid=%u }", stamp.year, stamp.month, | |
| 167 | − stamp.day, stamp.hour, stamp.min, stamp.sec, r->profile, | |
| 168 | − tomoyo_mode[r->mode], tomoyo_yesno(r->granted), gpid, | |
| 169 | − tomoyo_sys_getpid(), tomoyo_sys_getppid(), | |
| 166 | + "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s granted=%s (global-pid=%u) task={ pid=%u ppid=%u uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }", | |
| 167 | + stamp.year, stamp.month, stamp.day, stamp.hour, | |
| 168 | + stamp.min, stamp.sec, r->profile, tomoyo_mode[r->mode], | |
| 169 | + tomoyo_yesno(r->granted), gpid, tomoyo_sys_getpid(), | |
| 170 | + tomoyo_sys_getppid(), | |
| 170 | 171 | from_kuid(&init_user_ns, current_uid()), |
| 171 | 172 | from_kgid(&init_user_ns, current_gid()), |
| 172 | 173 | from_kuid(&init_user_ns, current_euid()), |
| @@ -185,6 +186,7 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 12 unmodified lines | ||
| 185 | 186 | struct tomoyo_mini_stat *stat; |
| 186 | 187 | unsigned int dev; |
| 187 | 188 | umode_t mode; |
| 189 | + | |
| 188 | 190 | if (!obj->stat_valid[i]) |
| 189 | 191 | continue; |
| 190 | 192 | stat = &obj->stat[i]; |
| @@ -193,8 +195,8 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 2 unmodified lines | ||
| 193 | 195 | if (i & 1) { |
| 194 | 196 | pos += snprintf(buffer + pos, |
| 195 | 197 | tomoyo_buffer_len - 1 - pos, |
| 196 | − " path%u.parent={ uid=%u gid=%u " | |
| 197 | − "ino=%lu perm=0%o }", (i >> 1) + 1, | |
| 198 | + " path%u.parent={ uid=%u gid=%u ino=%lu perm=0%o }", | |
| 199 | + (i >> 1) + 1, | |
| 198 | 200 | from_kuid(&init_user_ns, stat->uid), |
| 199 | 201 | from_kgid(&init_user_ns, stat->gid), |
| 200 | 202 | (unsigned long)stat->ino, |
| @@ -202,8 +204,8 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 1 unmodified line | ||
| 202 | 204 | continue; |
| 203 | 205 | } |
| 204 | 206 | pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos, |
| 205 | − " path%u={ uid=%u gid=%u ino=%lu major=%u" | |
| 206 | − " minor=%u perm=0%o type=%s", (i >> 1) + 1, | |
| 207 | + " path%u={ uid=%u gid=%u ino=%lu major=%u minor=%u perm=0%o type=%s", | |
| 208 | + (i >> 1) + 1, | |
| 207 | 209 | from_kuid(&init_user_ns, stat->uid), |
| 208 | 210 | from_kgid(&init_user_ns, stat->gid), |
| 209 | 211 | (unsigned long)stat->ino, |
| @@ -249,6 +251,7 @@ char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt, · 39 unmodified lines | ||
| 249 | 251 | const char *symlink = NULL; |
| 250 | 252 | int pos; |
| 251 | 253 | const char *domainname = r->domain->domainname->name; |
| 254 | + | |
| 252 | 255 | header = tomoyo_print_header(r); |
| 253 | 256 | if (!header) |
| 254 | 257 | return NULL; |
| @@ -256,6 +259,7 @@ char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt, · 1 unmodified line | ||
| 256 | 259 | len += strlen(domainname) + strlen(header) + 10; |
| 257 | 260 | if (r->ee) { |
| 258 | 261 | struct file *file = r->ee->bprm->file; |
| 262 | + | |
| 259 | 263 | realpath = tomoyo_realpath_from_path(&file->f_path); |
| 260 | 264 | bprm_info = tomoyo_print_bprm(r->ee->bprm, &r->ee->dump); |
| 261 | 265 | if (!realpath || !bprm_info) |
| @@ -275,6 +279,7 @@ char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt, · 13 unmodified lines | ||
| 275 | 279 | pos = snprintf(buf, len, "%s", header); |
| 276 | 280 | if (realpath) { |
| 277 | 281 | struct linux_binprm *bprm = r->ee->bprm; |
| 282 | + | |
| 278 | 283 | pos += snprintf(buf + pos, len - pos, |
| 279 | 284 | " exec={ realpath=\"%s\" argc=%d envc=%d %s }", |
| 280 | 285 | realpath, bprm->argc, bprm->envc, bprm_info); |
| @@ -328,6 +333,7 @@ static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns, · 47 unmodified lines | ||
| 328 | 333 | const u8 category = tomoyo_index2category[index] + |
| 329 | 334 | TOMOYO_MAX_MAC_INDEX; |
| 330 | 335 | struct tomoyo_profile *p; |
| 336 | + | |
| 331 | 337 | if (!tomoyo_policy_loaded) |
| 332 | 338 | return false; |
| 333 | 339 | p = tomoyo_profile(ns, profile); |
| @@ -362,6 +368,7 @@ void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt, · 28 unmodified lines | ||
| 362 | 368 | char *buf; |
| 363 | 369 | struct tomoyo_log *entry; |
| 364 | 370 | bool quota_exceeded = false; |
| 371 | + | |
| 365 | 372 | if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type, |
| 366 | 373 | r->matched_acl, r->granted)) |
| 367 | 374 | goto out; |
| @@ -413,6 +420,7 @@ void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...) · 45 unmodified lines | ||
| 413 | 420 | { |
| 414 | 421 | va_list args; |
| 415 | 422 | int len; |
| 423 | + | |
| 416 | 424 | va_start(args, fmt); |
| 417 | 425 | len = vsnprintf((char *) &len, 1, fmt, args) + 1; |
| 418 | 426 | va_end(args); |
| @@ -431,6 +439,7 @@ void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...) · 12 unmodified lines | ||
| 431 | 439 | void tomoyo_read_log(struct tomoyo_io_buffer *head) |
| 432 | 440 | { |
| 433 | 441 | struct tomoyo_log *ptr = NULL; |
| 442 | + | |
| 434 | 443 | if (head->r.w_pos) |
| 435 | 444 | return; |
| 436 | 445 | kfree(head->read_buf); |
| @@ -197,6 +197,7 @@ static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...) · 196 unmodified lines | ||
| 197 | 197 | { |
| 198 | 198 | va_list args; |
| 199 | 199 | const int pos = strlen(buffer); |
| 200 | + | |
| 200 | 201 | va_start(args, fmt); |
| 201 | 202 | vsnprintf(buffer + pos, len - pos - 1, fmt, args); |
| 202 | 203 | va_end(args); |
| @@ -214,6 +215,7 @@ static bool tomoyo_flush(struct tomoyo_io_buffer *head) · 11 unmodified lines | ||
| 214 | 215 | while (head->r.w_pos) { |
| 215 | 216 | const char *w = head->r.w[0]; |
| 216 | 217 | size_t len = strlen(w); |
| 218 | + | |
| 217 | 219 | if (len) { |
| 218 | 220 | if (len > head->read_user_buf_avail) |
| 219 | 221 | len = head->read_user_buf_avail; |
| @@ -279,6 +281,7 @@ static void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, · 59 unmodified lines | ||
| 279 | 281 | size_t len; |
| 280 | 282 | size_t pos = head->r.avail; |
| 281 | 283 | int size = head->readbuf_size - pos; |
| 284 | + | |
| 282 | 285 | if (size <= 0) |
| 283 | 286 | return; |
| 284 | 287 | va_start(args, fmt); |
| @@ -344,13 +347,14 @@ static bool tomoyo_namespace_enabled; · 59 unmodified lines | ||
| 344 | 347 | void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns) |
| 345 | 348 | { |
| 346 | 349 | unsigned int idx; |
| 350 | + | |
| 347 | 351 | for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++) |
| 348 | 352 | INIT_LIST_HEAD(&ns->acl_group[idx]); |
| 349 | 353 | for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++) |
| 350 | 354 | INIT_LIST_HEAD(&ns->group_list[idx]); |
| 351 | 355 | for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++) |
| 352 | 356 | INIT_LIST_HEAD(&ns->policy_list[idx]); |
| 353 | − ns->profile_version = 20110903; | |
| 357 | + ns->profile_version = 20150505; | |
| 354 | 358 | tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list); |
| 355 | 359 | list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list); |
| 356 | 360 | } |
| @@ -433,6 +437,7 @@ static void tomoyo_print_number_union_nospace · 76 unmodified lines | ||
| 433 | 437 | u8 min_type = ptr->value_type[0]; |
| 434 | 438 | const u8 max_type = ptr->value_type[1]; |
| 435 | 439 | char buffer[128]; |
| 440 | + | |
| 436 | 441 | buffer[0] = '\0'; |
| 437 | 442 | for (i = 0; i < 2; i++) { |
| 438 | 443 | switch (min_type) { |
| @@ -487,6 +492,7 @@ static struct tomoyo_profile *tomoyo_assign_profile · 48 unmodified lines | ||
| 487 | 492 | { |
| 488 | 493 | struct tomoyo_profile *ptr; |
| 489 | 494 | struct tomoyo_profile *entry; |
| 495 | + | |
| 490 | 496 | if (profile >= TOMOYO_MAX_PROFILES) |
| 491 | 497 | return NULL; |
| 492 | 498 | ptr = ns->profile_ptr[profile]; |
| @@ -530,6 +536,7 @@ struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns, · 37 unmodified lines | ||
| 530 | 536 | { |
| 531 | 537 | static struct tomoyo_profile tomoyo_null_profile; |
| 532 | 538 | struct tomoyo_profile *ptr = ns->profile_ptr[profile]; |
| 539 | + | |
| 533 | 540 | if (!ptr) |
| 534 | 541 | ptr = &tomoyo_null_profile; |
| 535 | 542 | return ptr; |
| @@ -546,6 +553,7 @@ struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns, · 10 unmodified lines | ||
| 546 | 553 | static s8 tomoyo_find_yesno(const char *string, const char *find) |
| 547 | 554 | { |
| 548 | 555 | const char *cp = strstr(string, find); |
| 556 | + | |
| 549 | 557 | if (cp) { |
| 550 | 558 | cp += strlen(find); |
| 551 | 559 | if (!strncmp(cp, "=yes", 4)) |
| @@ -569,6 +577,7 @@ static void tomoyo_set_uint(unsigned int *i, const char *string, · 17 unmodified lines | ||
| 569 | 577 | const char *find) |
| 570 | 578 | { |
| 571 | 579 | const char *cp = strstr(string, find); |
| 580 | + | |
| 572 | 581 | if (cp) |
| 573 | 582 | sscanf(cp + strlen(find), "=%u", i); |
| 574 | 583 | } |
| @@ -587,6 +596,7 @@ static int tomoyo_set_mode(char *name, const char *value, · 12 unmodified lines | ||
| 587 | 596 | { |
| 588 | 597 | u8 i; |
| 589 | 598 | u8 config; |
| 599 | + | |
| 590 | 600 | if (!strcmp(name, "CONFIG")) { |
| 591 | 601 | i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX; |
| 592 | 602 | config = profile->default_config; |
| @@ -595,10 +605,12 @@ static int tomoyo_set_mode(char *name, const char *value, · 2 unmodified lines | ||
| 595 | 605 | for (i = 0; i < TOMOYO_MAX_MAC_INDEX |
| 596 | 606 | + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) { |
| 597 | 607 | int len = 0; |
| 608 | + | |
| 598 | 609 | if (i < TOMOYO_MAX_MAC_INDEX) { |
| 599 | 610 | const u8 c = tomoyo_index2category[i]; |
| 600 | 611 | const char *category = |
| 601 | 612 | tomoyo_category_keywords[c]; |
| 613 | + | |
| 602 | 614 | len = strlen(category); |
| 603 | 615 | if (strncmp(name, category, len) || |
| 604 | 616 | name[len++] != ':' || name[len++] != ':') |
| @@ -618,6 +630,7 @@ static int tomoyo_set_mode(char *name, const char *value, · 13 unmodified lines | ||
| 618 | 630 | config = TOMOYO_CONFIG_USE_DEFAULT; |
| 619 | 631 | } else { |
| 620 | 632 | u8 mode; |
| 633 | + | |
| 621 | 634 | for (mode = 0; mode < 4; mode++) |
| 622 | 635 | if (strstr(value, tomoyo_mode[mode])) |
| 623 | 636 | /* |
| @@ -664,6 +677,7 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head) · 40 unmodified lines | ||
| 664 | 677 | unsigned int i; |
| 665 | 678 | char *cp; |
| 666 | 679 | struct tomoyo_profile *profile; |
| 680 | + | |
| 667 | 681 | if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version) |
| 668 | 682 | == 1) |
| 669 | 683 | return 0; |
| @@ -683,6 +697,7 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head) · 13 unmodified lines | ||
| 683 | 697 | const struct tomoyo_path_info *new_comment |
| 684 | 698 | = tomoyo_get_name(cp); |
| 685 | 699 | const struct tomoyo_path_info *old_comment; |
| 700 | + | |
| 686 | 701 | if (!new_comment) |
| 687 | 702 | return -ENOMEM; |
| 688 | 703 | spin_lock(&lock); |
| @@ -732,6 +747,7 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head) · 43 unmodified lines | ||
| 732 | 747 | struct tomoyo_policy_namespace *ns = |
| 733 | 748 | container_of(head->r.ns, typeof(*ns), namespace_list); |
| 734 | 749 | const struct tomoyo_profile *profile; |
| 750 | + | |
| 735 | 751 | if (head->r.eof) |
| 736 | 752 | return; |
| 737 | 753 | next: |
| @@ -760,6 +776,7 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head) · 22 unmodified lines | ||
| 760 | 776 | u8 i; |
| 761 | 777 | const struct tomoyo_path_info *comment = |
| 762 | 778 | profile->comment; |
| 779 | + | |
| 763 | 780 | tomoyo_print_namespace(head); |
| 764 | 781 | tomoyo_io_printf(head, "%u-COMMENT=", index); |
| 765 | 782 | tomoyo_set_string(head, comment ? comment->name : ""); |
| @@ -788,6 +805,7 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head) · 22 unmodified lines | ||
| 788 | 805 | + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) { |
| 789 | 806 | const u8 i = head->r.bit; |
| 790 | 807 | const u8 config = profile->config[i]; |
| 808 | + | |
| 791 | 809 | if (config == TOMOYO_CONFIG_USE_DEFAULT) |
| 792 | 810 | continue; |
| 793 | 811 | tomoyo_print_namespace(head); |
| @@ -847,10 +865,10 @@ static int tomoyo_update_manager_entry(const char *manager, · 53 unmodified lines | ||
| 847 | 865 | struct tomoyo_acl_param param = { |
| 848 | 866 | /* .ns = &tomoyo_kernel_namespace, */ |
| 849 | 867 | .is_delete = is_delete, |
| 850 | − .list = &tomoyo_kernel_namespace. | |
| 851 | − policy_list[TOMOYO_ID_MANAGER], | |
| 868 | + .list = &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER], | |
| 852 | 869 | }; |
| 853 | 870 | int error = is_delete ? -ENOENT : -ENOMEM; |
| 871 | + | |
| 854 | 872 | if (!tomoyo_correct_domain(manager) && |
| 855 | 873 | !tomoyo_correct_word(manager)) |
| 856 | 874 | return -EINVAL; |
| @@ -894,10 +912,10 @@ static void tomoyo_read_manager(struct tomoyo_io_buffer *head) · 37 unmodified lines | ||
| 894 | 912 | { |
| 895 | 913 | if (head->r.eof) |
| 896 | 914 | return; |
| 897 | − list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace. | |
| 898 | − policy_list[TOMOYO_ID_MANAGER]) { | |
| 915 | + list_for_each_cookie(head->r.acl, &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER]) { | |
| 899 | 916 | struct tomoyo_manager *ptr = |
| 900 | 917 | list_entry(head->r.acl, typeof(*ptr), head.list); |
| 918 | + | |
| 901 | 919 | if (ptr->head.is_deleted) |
| 902 | 920 | continue; |
| 903 | 921 | if (!tomoyo_flush(head)) |
| @@ -933,8 +951,7 @@ static bool tomoyo_manager(void) · 29 unmodified lines | ||
| 933 | 951 | exe = tomoyo_get_exe(); |
| 934 | 952 | if (!exe) |
| 935 | 953 | return false; |
| 936 | − list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace. | |
| 937 | − policy_list[TOMOYO_ID_MANAGER], head.list) { | |
| 954 | + list_for_each_entry_rcu(ptr, &tomoyo_kernel_namespace.policy_list[TOMOYO_ID_MANAGER], head.list) { | |
| 938 | 955 | if (!ptr->head.is_deleted && |
| 939 | 956 | (!tomoyo_pathcmp(domainname, ptr->manager) || |
| 940 | 957 | !strcmp(exe, ptr->manager->name))) { |
| @@ -945,9 +962,10 @@ static bool tomoyo_manager(void) · 4 unmodified lines | ||
| 945 | 962 | if (!found) { /* Reduce error messages. */ |
| 946 | 963 | static pid_t last_pid; |
| 947 | 964 | const pid_t pid = current->pid; |
| 965 | + | |
| 948 | 966 | if (last_pid != pid) { |
| 949 | − printk(KERN_WARNING "%s ( %s ) is not permitted to " | |
| 950 | − "update policies.\n", domainname->name, exe); | |
| 967 | + pr_warn("%s ( %s ) is not permitted to update policies.\n", | |
| 968 | + domainname->name, exe); | |
| 951 | 969 | last_pid = pid; |
| 952 | 970 | } |
| 953 | 971 | } |
| @@ -974,19 +992,21 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head, · 20 unmodified lines | ||
| 974 | 992 | unsigned int pid; |
| 975 | 993 | struct tomoyo_domain_info *domain = NULL; |
| 976 | 994 | bool global_pid = false; |
| 995 | + | |
| 977 | 996 | if (strncmp(data, "select ", 7)) |
| 978 | 997 | return false; |
| 979 | 998 | data += 7; |
| 980 | 999 | if (sscanf(data, "pid=%u", &pid) == 1 || |
| 981 | 1000 | (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { |
| 982 | 1001 | struct task_struct *p; |
| 1002 | + | |
| 983 | 1003 | rcu_read_lock(); |
| 984 | 1004 | if (global_pid) |
| 985 | 1005 | p = find_task_by_pid_ns(pid, &init_pid_ns); |
| 986 | 1006 | else |
| 987 | 1007 | p = find_task_by_vpid(pid); |
| 988 | 1008 | if (p) |
| 989 | − domain = tomoyo_real_domain(p); | |
| 1009 | + domain = tomoyo_task(p)->domain_info; | |
| 990 | 1010 | rcu_read_unlock(); |
| 991 | 1011 | } else if (!strncmp(data, "domain=", 7)) { |
| 992 | 1012 | if (tomoyo_domain_def(data + 7)) |
| @@ -1020,10 +1040,11 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head, · 27 unmodified lines | ||
| 1020 | 1040 | * Returns true if @a == @b, false otherwise. |
| 1021 | 1041 | */ |
| 1022 | 1042 | static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a, |
| 1023 | − const struct tomoyo_acl_info *b) | |
| 1043 | + const struct tomoyo_acl_info *b) | |
| 1024 | 1044 | { |
| 1025 | 1045 | const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head); |
| 1026 | 1046 | const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head); |
| 1047 | + | |
| 1027 | 1048 | return p1->domainname == p2->domainname; |
| 1028 | 1049 | } |
| 1029 | 1050 | |
| @@ -1039,11 +1060,13 @@ static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a, · 9 unmodified lines | ||
| 1039 | 1060 | static int tomoyo_write_task(struct tomoyo_acl_param *param) |
| 1040 | 1061 | { |
| 1041 | 1062 | int error = -EINVAL; |
| 1063 | + | |
| 1042 | 1064 | if (tomoyo_str_starts(¶m->data, "manual_domain_transition ")) { |
| 1043 | 1065 | struct tomoyo_task_acl e = { |
| 1044 | 1066 | .head.type = TOMOYO_TYPE_MANUAL_TASK_ACL, |
| 1045 | 1067 | .domainname = tomoyo_get_domainname(param), |
| 1046 | 1068 | }; |
| 1069 | + | |
| 1047 | 1070 | if (e.domainname) |
| 1048 | 1071 | error = tomoyo_update_domain(&e.head, sizeof(e), param, |
| 1049 | 1072 | tomoyo_same_task_acl, |
| @@ -1110,7 +1133,7 @@ static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns, · 60 unmodified lines | ||
| 1110 | 1133 | }; |
| 1111 | 1134 | static const struct { |
| 1112 | 1135 | const char *keyword; |
| 1113 | − int (*write) (struct tomoyo_acl_param *); | |
| 1136 | + int (*write)(struct tomoyo_acl_param *param); | |
| 1114 | 1137 | } tomoyo_callback[5] = { |
| 1115 | 1138 | { "file ", tomoyo_write_file }, |
| 1116 | 1139 | { "network inet ", tomoyo_write_inet_network }, |
| @@ -1151,9 +1174,11 @@ static int tomoyo_write_domain(struct tomoyo_io_buffer *head) · 34 unmodified lines | ||
| 1151 | 1174 | struct tomoyo_domain_info *domain = head->w.domain; |
| 1152 | 1175 | const bool is_delete = head->w.is_delete; |
| 1153 | 1176 | bool is_select = !is_delete && tomoyo_str_starts(&data, "select "); |
| 1154 | − unsigned int profile; | |
| 1177 | + unsigned int idx; | |
| 1178 | + | |
| 1155 | 1179 | if (*data == '<') { |
| 1156 | 1180 | int ret = 0; |
| 1181 | + | |
| 1157 | 1182 | domain = NULL; |
| 1158 | 1183 | if (is_delete) |
| 1159 | 1184 | ret = tomoyo_delete_domain(data); |
| @@ -1167,23 +1192,27 @@ static int tomoyo_write_domain(struct tomoyo_io_buffer *head) · 7 unmodified lines | ||
| 1167 | 1192 | if (!domain) |
| 1168 | 1193 | return -EINVAL; |
| 1169 | 1194 | ns = domain->ns; |
| 1170 | − if (sscanf(data, "use_profile %u", &profile) == 1 | |
| 1171 | − && profile < TOMOYO_MAX_PROFILES) { | |
| 1172 | − if (!tomoyo_policy_loaded || ns->profile_ptr[profile]) | |
| 1173 | − domain->profile = (u8) profile; | |
| 1195 | + if (sscanf(data, "use_profile %u", &idx) == 1 | |
| 1196 | + && idx < TOMOYO_MAX_PROFILES) { | |
| 1197 | + if (!tomoyo_policy_loaded || ns->profile_ptr[idx]) | |
| 1198 | + if (!is_delete) | |
| 1199 | + domain->profile = (u8) idx; | |
| 1174 | 1200 | return 0; |
| 1175 | 1201 | } |
| 1176 | − if (sscanf(data, "use_group %u\n", &profile) == 1 | |
| 1177 | − && profile < TOMOYO_MAX_ACL_GROUPS) { | |
| 1202 | + if (sscanf(data, "use_group %u\n", &idx) == 1 | |
| 1203 | + && idx < TOMOYO_MAX_ACL_GROUPS) { | |
| 1178 | 1204 | if (!is_delete) |
| 1179 | − domain->group = (u8) profile; | |
| 1205 | + set_bit(idx, domain->group); | |
| 1206 | + else | |
| 1207 | + clear_bit(idx, domain->group); | |
| 1180 | 1208 | return 0; |
| 1181 | 1209 | } |
| 1182 | − for (profile = 0; profile < TOMOYO_MAX_DOMAIN_INFO_FLAGS; profile++) { | |
| 1183 | − const char *cp = tomoyo_dif[profile]; | |
| 1210 | + for (idx = 0; idx < TOMOYO_MAX_DOMAIN_INFO_FLAGS; idx++) { | |
| 1211 | + const char *cp = tomoyo_dif[idx]; | |
| 1212 | + | |
| 1184 | 1213 | if (strncmp(data, cp, strlen(cp) - 1)) |
| 1185 | 1214 | continue; |
| 1186 | − domain->flags[profile] = !is_delete; | |
| 1215 | + domain->flags[idx] = !is_delete; | |
| 1187 | 1216 | return 0; |
| 1188 | 1217 | } |
| 1189 | 1218 | return tomoyo_write_domain2(ns, &domain->acl_info_list, data, |
| @@ -1225,9 +1254,11 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 35 unmodified lines | ||
| 1225 | 1254 | const struct tomoyo_envp *envp = |
| 1226 | 1255 | (typeof(envp)) (argv + cond->argc); |
| 1227 | 1256 | u16 skip; |
| 1257 | + | |
| 1228 | 1258 | for (skip = 0; skip < head->r.cond_index; skip++) { |
| 1229 | 1259 | const u8 left = condp->left; |
| 1230 | 1260 | const u8 right = condp->right; |
| 1261 | + | |
| 1231 | 1262 | condp++; |
| 1232 | 1263 | switch (left) { |
| 1233 | 1264 | case TOMOYO_ARGV_ENTRY: |
| @@ -1253,6 +1284,7 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 19 unmodified lines | ||
| 1253 | 1284 | const u8 match = condp->equals; |
| 1254 | 1285 | const u8 left = condp->left; |
| 1255 | 1286 | const u8 right = condp->right; |
| 1287 | + | |
| 1256 | 1288 | if (!tomoyo_flush(head)) |
| 1257 | 1289 | return false; |
| 1258 | 1290 | condp++; |
| @@ -1262,8 +1294,7 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 3 unmodified lines | ||
| 1262 | 1294 | case TOMOYO_ARGV_ENTRY: |
| 1263 | 1295 | tomoyo_io_printf(head, |
| 1264 | 1296 | "exec.argv[%lu]%s=\"", |
| 1265 | − argv->index, argv-> | |
| 1266 | − is_not ? "!" : ""); | |
| 1297 | + argv->index, argv->is_not ? "!" : ""); | |
| 1267 | 1298 | tomoyo_set_string(head, |
| 1268 | 1299 | argv->value->name); |
| 1269 | 1300 | tomoyo_set_string(head, "\""); |
| @@ -1274,12 +1305,10 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 4 unmodified lines | ||
| 1274 | 1305 | "exec.envp[\""); |
| 1275 | 1306 | tomoyo_set_string(head, |
| 1276 | 1307 | envp->name->name); |
| 1277 | − tomoyo_io_printf(head, "\"]%s=", envp-> | |
| 1278 | − is_not ? "!" : ""); | |
| 1308 | + tomoyo_io_printf(head, "\"]%s=", envp->is_not ? "!" : ""); | |
| 1279 | 1309 | if (envp->value) { |
| 1280 | 1310 | tomoyo_set_string(head, "\""); |
| 1281 | − tomoyo_set_string(head, envp-> | |
| 1282 | − value->name); | |
| 1311 | + tomoyo_set_string(head, envp->value->name); | |
| 1283 | 1312 | tomoyo_set_string(head, "\""); |
| 1284 | 1313 | } else { |
| 1285 | 1314 | tomoyo_set_string(head, |
| @@ -1375,6 +1404,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 89 unmodified lines | ||
| 1375 | 1404 | struct tomoyo_path_acl *ptr = |
| 1376 | 1405 | container_of(acl, typeof(*ptr), head); |
| 1377 | 1406 | const u16 perm = ptr->perm; |
| 1407 | + | |
| 1378 | 1408 | for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) { |
| 1379 | 1409 | if (!(perm & (1 << bit))) |
| 1380 | 1410 | continue; |
| @@ -1395,6 +1425,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 14 unmodified lines | ||
| 1395 | 1425 | } else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) { |
| 1396 | 1426 | struct tomoyo_task_acl *ptr = |
| 1397 | 1427 | container_of(acl, typeof(*ptr), head); |
| 1428 | + | |
| 1398 | 1429 | tomoyo_set_group(head, "task "); |
| 1399 | 1430 | tomoyo_set_string(head, "manual_domain_transition "); |
| 1400 | 1431 | tomoyo_set_string(head, ptr->domainname->name); |
| @@ -1404,6 +1435,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 3 unmodified lines | ||
| 1404 | 1435 | struct tomoyo_path2_acl *ptr = |
| 1405 | 1436 | container_of(acl, typeof(*ptr), head); |
| 1406 | 1437 | const u8 perm = ptr->perm; |
| 1438 | + | |
| 1407 | 1439 | for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) { |
| 1408 | 1440 | if (!(perm & (1 << bit))) |
| 1409 | 1441 | continue; |
| @@ -1424,6 +1456,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 14 unmodified lines | ||
| 1424 | 1456 | struct tomoyo_path_number_acl *ptr = |
| 1425 | 1457 | container_of(acl, typeof(*ptr), head); |
| 1426 | 1458 | const u8 perm = ptr->perm; |
| 1459 | + | |
| 1427 | 1460 | for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) { |
| 1428 | 1461 | if (!(perm & (1 << bit))) |
| 1429 | 1462 | continue; |
| @@ -1444,6 +1477,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 14 unmodified lines | ||
| 1444 | 1477 | struct tomoyo_mkdev_acl *ptr = |
| 1445 | 1478 | container_of(acl, typeof(*ptr), head); |
| 1446 | 1479 | const u8 perm = ptr->perm; |
| 1480 | + | |
| 1447 | 1481 | for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) { |
| 1448 | 1482 | if (!(perm & (1 << bit))) |
| 1449 | 1483 | continue; |
| @@ -1490,6 +1524,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 40 unmodified lines | ||
| 1490 | 1524 | ->name); |
| 1491 | 1525 | } else { |
| 1492 | 1526 | char buf[128]; |
| 1527 | + | |
| 1493 | 1528 | tomoyo_print_ip(buf, sizeof(buf), &ptr->address); |
| 1494 | 1529 | tomoyo_io_printf(head, "%s", buf); |
| 1495 | 1530 | } |
| @@ -1519,6 +1554,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 23 unmodified lines | ||
| 1519 | 1554 | } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) { |
| 1520 | 1555 | struct tomoyo_mount_acl *ptr = |
| 1521 | 1556 | container_of(acl, typeof(*ptr), head); |
| 1557 | + | |
| 1522 | 1558 | tomoyo_set_group(head, "file mount"); |
| 1523 | 1559 | tomoyo_print_name_union(head, &ptr->dev_name); |
| 1524 | 1560 | tomoyo_print_name_union(head, &ptr->dir_name); |
| @@ -1562,6 +1598,7 @@ static bool tomoyo_read_domain2(struct tomoyo_io_buffer *head, · 37 unmodified lines | ||
| 1562 | 1598 | list_for_each_cookie(head->r.acl, list) { |
| 1563 | 1599 | struct tomoyo_acl_info *ptr = |
| 1564 | 1600 | list_entry(head->r.acl, typeof(*ptr), list); |
| 1601 | + | |
| 1565 | 1602 | if (!tomoyo_print_entry(head, ptr)) |
| 1566 | 1603 | return false; |
| 1567 | 1604 | } |
| @@ -1583,8 +1620,9 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head) · 15 unmodified lines | ||
| 1583 | 1620 | list_for_each_cookie(head->r.domain, &tomoyo_domain_list) { |
| 1584 | 1621 | struct tomoyo_domain_info *domain = |
| 1585 | 1622 | list_entry(head->r.domain, typeof(*domain), list); |
| 1623 | + u8 i; | |
| 1624 | + | |
| 1586 | 1625 | switch (head->r.step) { |
| 1587 | − u8 i; | |
| 1588 | 1626 | case 0: |
| 1589 | 1627 | if (domain->is_deleted && |
| 1590 | 1628 | !head->r.print_this_domain_only) |
| @@ -1594,22 +1632,33 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head) · 3 unmodified lines | ||
| 1594 | 1632 | tomoyo_set_lf(head); |
| 1595 | 1633 | tomoyo_io_printf(head, "use_profile %u\n", |
| 1596 | 1634 | domain->profile); |
| 1597 | − tomoyo_io_printf(head, "use_group %u\n", | |
| 1598 | − domain->group); | |
| 1599 | 1635 | for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++) |
| 1600 | 1636 | if (domain->flags[i]) |
| 1601 | 1637 | tomoyo_set_string(head, tomoyo_dif[i]); |
| 1638 | + head->r.index = 0; | |
| 1602 | 1639 | head->r.step++; |
| 1603 | − tomoyo_set_lf(head); | |
| 1604 | 1640 | /* fall through */ |
| 1605 | 1641 | case 1: |
| 1642 | + while (head->r.index < TOMOYO_MAX_ACL_GROUPS) { | |
| 1643 | + i = head->r.index++; | |
| 1644 | + if (!test_bit(i, domain->group)) | |
| 1645 | + continue; | |
| 1646 | + tomoyo_io_printf(head, "use_group %u\n", i); | |
| 1647 | + if (!tomoyo_flush(head)) | |
| 1648 | + return; | |
| 1649 | + } | |
| 1650 | + head->r.index = 0; | |
| 1651 | + head->r.step++; | |
| 1652 | + tomoyo_set_lf(head); | |
| 1653 | + /* fall through */ | |
| 1654 | + case 2: | |
| 1606 | 1655 | if (!tomoyo_read_domain2(head, &domain->acl_info_list)) |
| 1607 | 1656 | return; |
| 1608 | 1657 | head->r.step++; |
| 1609 | 1658 | if (!tomoyo_set_lf(head)) |
| 1610 | 1659 | return; |
| 1611 | 1660 | /* fall through */ |
| 1612 | − case 2: | |
| 1661 | + case 3: | |
| 1613 | 1662 | head->r.step = 0; |
| 1614 | 1663 | if (head->r.print_this_domain_only) |
| 1615 | 1664 | goto done; |
| @@ -1668,7 +1717,7 @@ static void tomoyo_read_pid(struct tomoyo_io_buffer *head) · 52 unmodified lines | ||
| 1668 | 1717 | else |
| 1669 | 1718 | p = find_task_by_vpid(pid); |
| 1670 | 1719 | if (p) |
| 1671 | − domain = tomoyo_real_domain(p); | |
| 1720 | + domain = tomoyo_task(p)->domain_info; | |
| 1672 | 1721 | rcu_read_unlock(); |
| 1673 | 1722 | if (!domain) |
| 1674 | 1723 | return; |
| @@ -1711,6 +1760,7 @@ static int tomoyo_write_exception(struct tomoyo_io_buffer *head) · 36 unmodified lines | ||
| 1711 | 1760 | .data = head->write_buf, |
| 1712 | 1761 | }; |
| 1713 | 1762 | u8 i; |
| 1763 | + | |
| 1714 | 1764 | if (tomoyo_str_starts(¶m.data, "aggregator ")) |
| 1715 | 1765 | return tomoyo_write_aggregator(¶m); |
| 1716 | 1766 | for (i = 0; i < TOMOYO_MAX_TRANSITION_TYPE; i++) |
| @@ -1722,6 +1772,7 @@ static int tomoyo_write_exception(struct tomoyo_io_buffer *head) · 5 unmodified lines | ||
| 1722 | 1772 | if (tomoyo_str_starts(¶m.data, "acl_group ")) { |
| 1723 | 1773 | unsigned int group; |
| 1724 | 1774 | char *data; |
| 1775 | + | |
| 1725 | 1776 | group = simple_strtoul(param.data, &data, 10); |
| 1726 | 1777 | if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ') |
| 1727 | 1778 | return tomoyo_write_domain2 |
| @@ -1746,12 +1797,15 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) · 18 unmodified lines | ||
| 1746 | 1797 | struct tomoyo_policy_namespace *ns = |
| 1747 | 1798 | container_of(head->r.ns, typeof(*ns), namespace_list); |
| 1748 | 1799 | struct list_head *list = &ns->group_list[idx]; |
| 1800 | + | |
| 1749 | 1801 | list_for_each_cookie(head->r.group, list) { |
| 1750 | 1802 | struct tomoyo_group *group = |
| 1751 | 1803 | list_entry(head->r.group, typeof(*group), head.list); |
| 1804 | + | |
| 1752 | 1805 | list_for_each_cookie(head->r.acl, &group->member_list) { |
| 1753 | 1806 | struct tomoyo_acl_head *ptr = |
| 1754 | 1807 | list_entry(head->r.acl, typeof(*ptr), list); |
| 1808 | + | |
| 1755 | 1809 | if (ptr->is_deleted) |
| 1756 | 1810 | continue; |
| 1757 | 1811 | if (!tomoyo_flush(head)) |
| @@ -1771,10 +1825,10 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) · 13 unmodified lines | ||
| 1771 | 1825 | head)->number); |
| 1772 | 1826 | } else if (idx == TOMOYO_ADDRESS_GROUP) { |
| 1773 | 1827 | char buffer[128]; |
| 1774 | − | |
| 1775 | 1828 | struct tomoyo_address_group *member = |
| 1776 | 1829 | container_of(ptr, typeof(*member), |
| 1777 | 1830 | head); |
| 1831 | + | |
| 1778 | 1832 | tomoyo_print_ip(buffer, sizeof(buffer), |
| 1779 | 1833 | &member->address); |
| 1780 | 1834 | tomoyo_io_printf(head, " %s", buffer); |
| @@ -1802,6 +1856,7 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) · 21 unmodified lines | ||
| 1802 | 1856 | struct tomoyo_policy_namespace *ns = |
| 1803 | 1857 | container_of(head->r.ns, typeof(*ns), namespace_list); |
| 1804 | 1858 | struct list_head *list = &ns->policy_list[idx]; |
| 1859 | + | |
| 1805 | 1860 | list_for_each_cookie(head->r.acl, list) { |
| 1806 | 1861 | struct tomoyo_acl_head *acl = |
| 1807 | 1862 | container_of(head->r.acl, typeof(*acl), list); |
| @@ -1814,6 +1869,7 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) · 6 unmodified lines | ||
| 1814 | 1869 | { |
| 1815 | 1870 | struct tomoyo_transition_control *ptr = |
| 1816 | 1871 | container_of(acl, typeof(*ptr), head); |
| 1872 | + | |
| 1817 | 1873 | tomoyo_print_namespace(head); |
| 1818 | 1874 | tomoyo_set_string(head, tomoyo_transition_type |
| 1819 | 1875 | [ptr->type]); |
| @@ -1829,6 +1885,7 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) · 9 unmodified lines | ||
| 1829 | 1885 | { |
| 1830 | 1886 | struct tomoyo_aggregator *ptr = |
| 1831 | 1887 | container_of(acl, typeof(*ptr), head); |
| 1888 | + | |
| 1832 | 1889 | tomoyo_print_namespace(head); |
| 1833 | 1890 | tomoyo_set_string(head, "aggregator "); |
| 1834 | 1891 | tomoyo_set_string(head, |
| @@ -1858,6 +1915,7 @@ static void tomoyo_read_exception(struct tomoyo_io_buffer *head) · 23 unmodified lines | ||
| 1858 | 1915 | { |
| 1859 | 1916 | struct tomoyo_policy_namespace *ns = |
| 1860 | 1917 | container_of(head->r.ns, typeof(*ns), namespace_list); |
| 1918 | + | |
| 1861 | 1919 | if (head->r.eof) |
| 1862 | 1920 | return; |
| 1863 | 1921 | while (head->r.step < TOMOYO_MAX_POLICY && |
| @@ -1921,6 +1979,7 @@ static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); · 57 unmodified lines | ||
| 1921 | 1979 | static int tomoyo_truncate(char *str) |
| 1922 | 1980 | { |
| 1923 | 1981 | char *start = str; |
| 1982 | + | |
| 1924 | 1983 | while (*(unsigned char *) str > (unsigned char) ' ') |
| 1925 | 1984 | str++; |
| 1926 | 1985 | *str = '\0'; |
| @@ -1943,6 +2002,7 @@ static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header) · 16 unmodified lines | ||
| 1943 | 2002 | char *symlink = NULL; |
| 1944 | 2003 | char *cp = strchr(header, '\n'); |
| 1945 | 2004 | int len; |
| 2005 | + | |
| 1946 | 2006 | if (!cp) |
| 1947 | 2007 | return; |
| 1948 | 2008 | cp = strchr(cp + 1, '\n'); |
| @@ -2002,6 +2062,7 @@ int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) · 53 unmodified lines | ||
| 2002 | 2062 | static unsigned int tomoyo_serial; |
| 2003 | 2063 | struct tomoyo_query entry = { }; |
| 2004 | 2064 | bool quota_exceeded = false; |
| 2065 | + | |
| 2005 | 2066 | va_start(args, fmt); |
| 2006 | 2067 | len = vsnprintf((char *) &len, 1, fmt, args) + 1; |
| 2007 | 2068 | va_end(args); |
| @@ -2063,8 +2124,7 @@ int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) · 55 unmodified lines | ||
| 2063 | 2124 | (tomoyo_answer_wait, entry.answer || |
| 2064 | 2125 | !atomic_read(&tomoyo_query_observers), HZ)) |
| 2065 | 2126 | break; |
| 2066 | − else | |
| 2067 | − entry.timer++; | |
| 2127 | + entry.timer++; | |
| 2068 | 2128 | } |
| 2069 | 2129 | spin_lock(&tomoyo_query_list_lock); |
| 2070 | 2130 | list_del(&entry.list); |
| @@ -2100,6 +2160,7 @@ static struct tomoyo_domain_info *tomoyo_find_domain_by_qid · 29 unmodified lines | ||
| 2100 | 2160 | { |
| 2101 | 2161 | struct tomoyo_query *ptr; |
| 2102 | 2162 | struct tomoyo_domain_info *domain = NULL; |
| 2163 | + | |
| 2103 | 2164 | spin_lock(&tomoyo_query_list_lock); |
| 2104 | 2165 | list_for_each_entry(ptr, &tomoyo_query_list, list) { |
| 2105 | 2166 | if (ptr->serial != serial) |
| @@ -2142,15 +2203,15 @@ static void tomoyo_read_query(struct tomoyo_io_buffer *head) · 36 unmodified lines | ||
| 2142 | 2203 | unsigned int pos = 0; |
| 2143 | 2204 | size_t len = 0; |
| 2144 | 2205 | char *buf; |
| 2206 | + | |
| 2145 | 2207 | if (head->r.w_pos) |
| 2146 | 2208 | return; |
| 2147 | − if (head->read_buf) { | |
| 2148 | − kfree(head->read_buf); | |
| 2149 | − head->read_buf = NULL; | |
| 2150 | − } | |
| 2209 | + kfree(head->read_buf); | |
| 2210 | + head->read_buf = NULL; | |
| 2151 | 2211 | spin_lock(&tomoyo_query_list_lock); |
| 2152 | 2212 | list_for_each(tmp, &tomoyo_query_list) { |
| 2153 | 2213 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
| 2214 | + | |
| 2154 | 2215 | if (pos++ != head->r.query_index) |
| 2155 | 2216 | continue; |
| 2156 | 2217 | len = ptr->query_len; |
| @@ -2168,6 +2229,7 @@ static void tomoyo_read_query(struct tomoyo_io_buffer *head) · 11 unmodified lines | ||
| 2168 | 2229 | spin_lock(&tomoyo_query_list_lock); |
| 2169 | 2230 | list_for_each(tmp, &tomoyo_query_list) { |
| 2170 | 2231 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
| 2232 | + | |
| 2171 | 2233 | if (pos++ != head->r.query_index) |
| 2172 | 2234 | continue; |
| 2173 | 2235 | /* |
| @@ -2202,9 +2264,11 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head) · 28 unmodified lines | ||
| 2202 | 2264 | struct list_head *tmp; |
| 2203 | 2265 | unsigned int serial; |
| 2204 | 2266 | unsigned int answer; |
| 2267 | + | |
| 2205 | 2268 | spin_lock(&tomoyo_query_list_lock); |
| 2206 | 2269 | list_for_each(tmp, &tomoyo_query_list) { |
| 2207 | 2270 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
| 2271 | + | |
| 2208 | 2272 | ptr->timer = 0; |
| 2209 | 2273 | } |
| 2210 | 2274 | spin_unlock(&tomoyo_query_list_lock); |
| @@ -2213,6 +2277,7 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head) · 2 unmodified lines | ||
| 2213 | 2277 | spin_lock(&tomoyo_query_list_lock); |
| 2214 | 2278 | list_for_each(tmp, &tomoyo_query_list) { |
| 2215 | 2279 | struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list); |
| 2280 | + | |
| 2216 | 2281 | if (ptr->serial != serial) |
| 2217 | 2282 | continue; |
| 2218 | 2283 | ptr->answer = answer; |
| @@ -2235,7 +2300,7 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head) · 16 unmodified lines | ||
| 2235 | 2300 | static void tomoyo_read_version(struct tomoyo_io_buffer *head) |
| 2236 | 2301 | { |
| 2237 | 2302 | if (!head->r.eof) { |
| 2238 | − tomoyo_io_printf(head, "2.5.0"); | |
| 2303 | + tomoyo_io_printf(head, "2.6.0"); | |
| 2239 | 2304 | head->r.eof = true; |
| 2240 | 2305 | } |
| 2241 | 2306 | } |
| @@ -2287,6 +2352,7 @@ static void tomoyo_read_stat(struct tomoyo_io_buffer *head) · 45 unmodified lines | ||
| 2287 | 2352 | { |
| 2288 | 2353 | u8 i; |
| 2289 | 2354 | unsigned int total = 0; |
| 2355 | + | |
| 2290 | 2356 | if (head->r.eof) |
| 2291 | 2357 | return; |
| 2292 | 2358 | for (i = 0; i < TOMOYO_MAX_POLICY_STAT; i++) { |
| @@ -2295,9 +2361,9 @@ static void tomoyo_read_stat(struct tomoyo_io_buffer *head) · 2 unmodified lines | ||
| 2295 | 2361 | tomoyo_stat_updated[i]); |
| 2296 | 2362 | if (tomoyo_stat_modified[i]) { |
| 2297 | 2363 | struct tomoyo_time stamp; |
| 2364 | + | |
| 2298 | 2365 | tomoyo_convert_time(tomoyo_stat_modified[i], &stamp); |
| 2299 | − tomoyo_io_printf(head, " (Last: %04u/%02u/%02u " | |
| 2300 | − "%02u:%02u:%02u)", | |
| 2366 | + tomoyo_io_printf(head, " (Last: %04u/%02u/%02u %02u:%02u:%02u)", | |
| 2301 | 2367 | stamp.year, stamp.month, stamp.day, |
| 2302 | 2368 | stamp.hour, stamp.min, stamp.sec); |
| 2303 | 2369 | } |
| @@ -2305,6 +2371,7 @@ static void tomoyo_read_stat(struct tomoyo_io_buffer *head) · 1 unmodified line | ||
| 2305 | 2371 | } |
| 2306 | 2372 | for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) { |
| 2307 | 2373 | unsigned int used = tomoyo_memory_used[i]; |
| 2374 | + | |
| 2308 | 2375 | total += used; |
| 2309 | 2376 | tomoyo_io_printf(head, "Memory used by %-22s %10u", |
| 2310 | 2377 | tomoyo_memory_headers[i], used); |
| @@ -2329,6 +2396,7 @@ static int tomoyo_write_stat(struct tomoyo_io_buffer *head) · 18 unmodified lines | ||
| 2329 | 2396 | { |
| 2330 | 2397 | char *data = head->write_buf; |
| 2331 | 2398 | u8 i; |
| 2399 | + | |
| 2332 | 2400 | if (tomoyo_str_starts(&data, "Memory used by ")) |
| 2333 | 2401 | for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) |
| 2334 | 2402 | if (tomoyo_str_starts(&data, tomoyo_memory_headers[i])) |
| @@ -2457,6 +2525,7 @@ int tomoyo_open_control(const u8 type, struct file *file) · 122 unmodified lines | ||
| 2457 | 2525 | __poll_t tomoyo_poll_control(struct file *file, poll_table *wait) |
| 2458 | 2526 | { |
| 2459 | 2527 | struct tomoyo_io_buffer *head = file->private_data; |
| 2528 | + | |
| 2460 | 2529 | if (head->poll) |
| 2461 | 2530 | return head->poll(file, wait) | EPOLLOUT | EPOLLWRNORM; |
| 2462 | 2531 | return EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM; |
| @@ -2472,6 +2541,7 @@ __poll_t tomoyo_poll_control(struct file *file, poll_table *wait) · 9 unmodified lines | ||
| 2472 | 2541 | static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head) |
| 2473 | 2542 | { |
| 2474 | 2543 | struct list_head *ns; |
| 2544 | + | |
| 2475 | 2545 | if (head->type != TOMOYO_EXCEPTIONPOLICY && |
| 2476 | 2546 | head->type != TOMOYO_PROFILE) |
| 2477 | 2547 | return; |
| @@ -2517,7 +2587,7 @@ ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer, · 39 unmodified lines | ||
| 2517 | 2587 | int idx; |
| 2518 | 2588 | |
| 2519 | 2589 | if (!head->read) |
| 2520 | − return -ENOSYS; | |
| 2590 | + return -EINVAL; | |
| 2521 | 2591 | if (mutex_lock_interruptible(&head->io_sem)) |
| 2522 | 2592 | return -EINTR; |
| 2523 | 2593 | head->read_user_buf = buffer; |
| @@ -2557,6 +2627,7 @@ static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line) · 33 unmodified lines | ||
| 2557 | 2627 | head->type == TOMOYO_PROFILE) { |
| 2558 | 2628 | if (*line == '<') { |
| 2559 | 2629 | char *cp = strchr(line, ' '); |
| 2630 | + | |
| 2560 | 2631 | if (cp) { |
| 2561 | 2632 | *cp++ = '\0'; |
| 2562 | 2633 | head->w.ns = tomoyo_assign_namespace(line); |
| @@ -2589,8 +2660,9 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, · 26 unmodified lines | ||
| 2589 | 2660 | size_t avail_len = buffer_len; |
| 2590 | 2661 | char *cp0 = head->write_buf; |
| 2591 | 2662 | int idx; |
| 2663 | + | |
| 2592 | 2664 | if (!head->write) |
| 2593 | − return -ENOSYS; | |
| 2665 | + return -EINVAL; | |
| 2594 | 2666 | if (!access_ok(buffer, buffer_len)) |
| 2595 | 2667 | return -EFAULT; |
| 2596 | 2668 | if (mutex_lock_interruptible(&head->io_sem)) |
| @@ -2600,9 +2672,11 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, · 3 unmodified lines | ||
| 2600 | 2672 | /* Read a line and dispatch it to the policy handler. */ |
| 2601 | 2673 | while (avail_len > 0) { |
| 2602 | 2674 | char c; |
| 2675 | + | |
| 2603 | 2676 | if (head->w.avail >= head->writebuf_size - 1) { |
| 2604 | 2677 | const int len = head->writebuf_size * 2; |
| 2605 | 2678 | char *cp = kzalloc(len, GFP_NOFS); |
| 2679 | + | |
| 2606 | 2680 | if (!cp) { |
| 2607 | 2681 | error = -ENOMEM; |
| 2608 | 2682 | break; |
| @@ -2701,30 +2775,32 @@ void tomoyo_check_profile(void) · 92 unmodified lines | ||
| 2701 | 2775 | { |
| 2702 | 2776 | struct tomoyo_domain_info *domain; |
| 2703 | 2777 | const int idx = tomoyo_read_lock(); |
| 2778 | + | |
| 2704 | 2779 | tomoyo_policy_loaded = true; |
| 2705 | − printk(KERN_INFO "TOMOYO: 2.5.0\n"); | |
| 2780 | + pr_info("TOMOYO: 2.6.0\n"); | |
| 2706 | 2781 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
| 2707 | 2782 | const u8 profile = domain->profile; |
| 2708 | − const struct tomoyo_policy_namespace *ns = domain->ns; | |
| 2709 | − if (ns->profile_version != 20110903) | |
| 2710 | − printk(KERN_ERR | |
| 2711 | − "Profile version %u is not supported.\n", | |
| 2783 | + struct tomoyo_policy_namespace *ns = domain->ns; | |
| 2784 | + | |
| 2785 | + if (ns->profile_version == 20110903) { | |
| 2786 | + pr_info_once("Converting profile version from %u to %u.\n", | |
| 2787 | + 20110903, 20150505); | |
| 2788 | + ns->profile_version = 20150505; | |
| 2789 | + } | |
| 2790 | + if (ns->profile_version != 20150505) | |
| 2791 | + pr_err("Profile version %u is not supported.\n", | |
| 2712 | 2792 | ns->profile_version); |
| 2713 | 2793 | else if (!ns->profile_ptr[profile]) |
| 2714 | − printk(KERN_ERR | |
| 2715 | − "Profile %u (used by '%s') is not defined.\n", | |
| 2794 | + pr_err("Profile %u (used by '%s') is not defined.\n", | |
| 2716 | 2795 | profile, domain->domainname->name); |
| 2717 | 2796 | else |
| 2718 | 2797 | continue; |
| 2719 | − printk(KERN_ERR | |
| 2720 | − "Userland tools for TOMOYO 2.5 must be installed and " | |
| 2721 | − "policy must be initialized.\n"); | |
| 2722 | − printk(KERN_ERR "Please see http://tomoyo.sourceforge.jp/2.5/ " | |
| 2723 | − "for more information.\n"); | |
| 2798 | + pr_err("Userland tools for TOMOYO 2.6 must be installed and policy must be initialized.\n"); | |
| 2799 | + pr_err("Please see https://tomoyo.osdn.jp/2.6/ for more information.\n"); | |
| 2724 | 2800 | panic("STOP!"); |
| 2725 | 2801 | } |
| 2726 | 2802 | tomoyo_read_unlock(idx); |
| 2727 | − printk(KERN_INFO "Mandatory Access Control activated.\n"); | |
| 2803 | + pr_info("Mandatory Access Control activated.\n"); | |
| 2728 | 2804 | } |
| 2729 | 2805 | |
| 2730 | 2806 | /** |
| @@ -2743,9 +2819,11 @@ void __init tomoyo_load_builtin_policy(void) · 12 unmodified lines | ||
| 2743 | 2819 | #include "builtin-policy.h" |
| 2744 | 2820 | u8 i; |
| 2745 | 2821 | const int idx = tomoyo_read_lock(); |
| 2822 | + | |
| 2746 | 2823 | for (i = 0; i < 5; i++) { |
| 2747 | 2824 | struct tomoyo_io_buffer head = { }; |
| 2748 | 2825 | char *start = ""; |
| 2826 | + | |
| 2749 | 2827 | switch (i) { |
| 2750 | 2828 | case 0: |
| 2751 | 2829 | start = tomoyo_builtin_profile; |
| @@ -2775,6 +2853,7 @@ void __init tomoyo_load_builtin_policy(void) · 23 unmodified lines | ||
| 2775 | 2853 | } |
| 2776 | 2854 | while (1) { |
| 2777 | 2855 | char *end = strchr(start, '\n'); |
| 2856 | + | |
| 2778 | 2857 | if (!end) |
| 2779 | 2858 | break; |
| 2780 | 2859 | *end = '\0'; |
| @@ -10,6 +10,8 @@ · 9 unmodified lines | ||
| 10 | 10 | #ifndef _SECURITY_TOMOYO_COMMON_H |
| 11 | 11 | #define _SECURITY_TOMOYO_COMMON_H |
| 12 | 12 | |
| 13 | +#define pr_fmt(fmt) fmt | |
| 14 | + | |
| 13 | 15 | #include <linux/ctype.h> |
| 14 | 16 | #include <linux/string.h> |
| 15 | 17 | #include <linux/mm.h> |
| @@ -29,6 +31,7 @@ · 13 unmodified lines | ||
| 29 | 31 | #include <linux/in.h> |
| 30 | 32 | #include <linux/in6.h> |
| 31 | 33 | #include <linux/un.h> |
| 34 | +#include <linux/lsm_hooks.h> | |
| 32 | 35 | #include <net/sock.h> |
| 33 | 36 | #include <net/af_unix.h> |
| 34 | 37 | #include <net/ip.h> |
| @@ -681,11 +684,12 @@ struct tomoyo_domain_info { · 646 unmodified lines | ||
| 681 | 684 | const struct tomoyo_path_info *domainname; |
| 682 | 685 | /* Namespace for this domain. Never NULL. */ |
| 683 | 686 | struct tomoyo_policy_namespace *ns; |
| 687 | + /* Group numbers to use. */ | |
| 688 | + unsigned long group[TOMOYO_MAX_ACL_GROUPS / BITS_PER_LONG]; | |
| 684 | 689 | u8 profile; /* Profile number to use. */ |
| 685 | − u8 group; /* Group number to use. */ | |
| 686 | 690 | bool is_deleted; /* Delete flag. */ |
| 687 | 691 | bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS]; |
| 688 | − atomic_t users; /* Number of referring credentials. */ | |
| 692 | + atomic_t users; /* Number of referring tasks. */ | |
| 689 | 693 | }; |
| 690 | 694 | |
| 691 | 695 | /* |
| @@ -787,9 +791,9 @@ struct tomoyo_acl_param { · 95 unmodified lines | ||
| 787 | 791 | * interfaces. |
| 788 | 792 | */ |
| 789 | 793 | struct tomoyo_io_buffer { |
| 790 | − void (*read) (struct tomoyo_io_buffer *); | |
| 791 | − int (*write) (struct tomoyo_io_buffer *); | |
| 792 | − __poll_t (*poll) (struct file *file, poll_table *wait); | |
| 794 | + void (*read)(struct tomoyo_io_buffer *head); | |
| 795 | + int (*write)(struct tomoyo_io_buffer *head); | |
| 796 | + __poll_t (*poll)(struct file *file, poll_table *wait); | |
| 793 | 797 | /* Exclusive lock for this structure. */ |
| 794 | 798 | struct mutex io_sem; |
| 795 | 799 | char __user *read_user_buf; |
| @@ -906,12 +910,18 @@ struct tomoyo_policy_namespace { · 110 unmodified lines | ||
| 906 | 910 | struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS]; |
| 907 | 911 | /* List for connecting to tomoyo_namespace_list list. */ |
| 908 | 912 | struct list_head namespace_list; |
| 909 | − /* Profile version. Currently only 20110903 is defined. */ | |
| 913 | + /* Profile version. Currently only 20150505 is defined. */ | |
| 910 | 914 | unsigned int profile_version; |
| 911 | 915 | /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */ |
| 912 | 916 | const char *name; |
| 913 | 917 | }; |
| 914 | 918 | |
| 919 | +/* Structure for "struct task_struct"->security. */ | |
| 920 | +struct tomoyo_task { | |
| 921 | + struct tomoyo_domain_info *domain_info; | |
| 922 | + struct tomoyo_domain_info *old_domain_info; | |
| 923 | +}; | |
| 924 | + | |
| 915 | 925 | /********** Function prototypes. **********/ |
| 916 | 926 | |
| 917 | 927 | bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address, |
| @@ -1020,6 +1030,7 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, · 102 unmodified lines | ||
| 1020 | 1030 | struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param); |
| 1021 | 1031 | struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, |
| 1022 | 1032 | const bool transit); |
| 1033 | +struct tomoyo_domain_info *tomoyo_domain(void); | |
| 1023 | 1034 | struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname); |
| 1024 | 1035 | struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, |
| 1025 | 1036 | const u8 idx); |
| @@ -1034,8 +1045,8 @@ void *tomoyo_commit_ok(void *data, const unsigned int size); · 8 unmodified lines | ||
| 1034 | 1045 | void __init tomoyo_load_builtin_policy(void); |
| 1035 | 1046 | void __init tomoyo_mm_init(void); |
| 1036 | 1047 | void tomoyo_check_acl(struct tomoyo_request_info *r, |
| 1037 | − bool (*check_entry) (struct tomoyo_request_info *, | |
| 1038 | − const struct tomoyo_acl_info *)); | |
| 1048 | + bool (*check_entry)(struct tomoyo_request_info *, | |
| 1049 | + const struct tomoyo_acl_info *)); | |
| 1039 | 1050 | void tomoyo_check_profile(void); |
| 1040 | 1051 | void tomoyo_convert_time(time64_t time, struct tomoyo_time *stamp); |
| 1041 | 1052 | void tomoyo_del_condition(struct list_head *element); |
| @@ -1062,6 +1073,7 @@ void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt, · 20 unmodified lines | ||
| 1062 | 1073 | /********** External variable definitions. **********/ |
| 1063 | 1074 | |
| 1064 | 1075 | extern bool tomoyo_policy_loaded; |
| 1076 | +extern int tomoyo_enabled; | |
| 1065 | 1077 | extern const char * const tomoyo_condition_keyword |
| 1066 | 1078 | [TOMOYO_MAX_CONDITION_KEYWORD]; |
| 1067 | 1079 | extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS]; |
| @@ -1085,6 +1097,7 @@ extern struct tomoyo_domain_info tomoyo_kernel_domain; · 17 unmodified lines | ||
| 1085 | 1097 | extern struct tomoyo_policy_namespace tomoyo_kernel_namespace; |
| 1086 | 1098 | extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT]; |
| 1087 | 1099 | extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT]; |
| 1100 | +extern struct lsm_blob_sizes tomoyo_blob_sizes; | |
| 1088 | 1101 | |
| 1089 | 1102 | /********** Inlined functions. **********/ |
| 1090 | 1103 | |
| @@ -1121,6 +1134,7 @@ static inline void tomoyo_read_unlock(int idx) · 30 unmodified lines | ||
| 1121 | 1134 | static inline pid_t tomoyo_sys_getppid(void) |
| 1122 | 1135 | { |
| 1123 | 1136 | pid_t pid; |
| 1137 | + | |
| 1124 | 1138 | rcu_read_lock(); |
| 1125 | 1139 | pid = task_tgid_vnr(rcu_dereference(current->real_parent)); |
| 1126 | 1140 | rcu_read_unlock(); |
| @@ -1197,26 +1211,15 @@ static inline void tomoyo_put_group(struct tomoyo_group *group) · 70 unmodified lines | ||
| 1197 | 1211 | } |
| 1198 | 1212 | |
| 1199 | 1213 | /** |
| 1200 | − * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread. | |
| 1201 | − * | |
| 1202 | − * Returns pointer to "struct tomoyo_domain_info" for current thread. | |
| 1203 | − */ | |
| 1204 | −static inline struct tomoyo_domain_info *tomoyo_domain(void) | |
| 1205 | −{ | |
| 1206 | − return current_cred()->security; | |
| 1207 | −} | |
| 1208 | − | |
| 1209 | −/** | |
| 1210 | − * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread. | |
| 1214 | + * tomoyo_task - Get "struct tomoyo_task" for specified thread. | |
| 1211 | 1215 | * |
| 1212 | − * @task: Pointer to "struct task_struct". | |
| 1216 | + * @task - Pointer to "struct task_struct". | |
| 1213 | 1217 | * |
| 1214 | − * Returns pointer to "struct tomoyo_security" for specified thread. | |
| 1218 | + * Returns pointer to "struct tomoyo_task" for specified thread. | |
| 1215 | 1219 | */ |
| 1216 | −static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct | |
| 1217 | − *task) | |
| 1220 | +static inline struct tomoyo_task *tomoyo_task(struct task_struct *task) | |
| 1218 | 1221 | { |
| 1219 | − return task_cred_xxx(task, security); | |
| 1222 | + return task->security + tomoyo_blob_sizes.lbs_task; | |
| 1220 | 1223 | } |
| 1221 | 1224 | |
| 1222 | 1225 | /** |
| @@ -28,9 +28,11 @@ static bool tomoyo_argv(const unsigned int index, const char *arg_ptr, · 27 unmodified lines | ||
| 28 | 28 | { |
| 29 | 29 | int i; |
| 30 | 30 | struct tomoyo_path_info arg; |
| 31 | + | |
| 31 | 32 | arg.name = arg_ptr; |
| 32 | 33 | for (i = 0; i < argc; argv++, checked++, i++) { |
| 33 | 34 | bool result; |
| 35 | + | |
| 34 | 36 | if (index != argv->index) |
| 35 | 37 | continue; |
| 36 | 38 | *checked = 1; |
| @@ -62,12 +64,14 @@ static bool tomoyo_envp(const char *env_name, const char *env_value, · 25 unmodified lines | ||
| 62 | 64 | int i; |
| 63 | 65 | struct tomoyo_path_info name; |
| 64 | 66 | struct tomoyo_path_info value; |
| 67 | + | |
| 65 | 68 | name.name = env_name; |
| 66 | 69 | tomoyo_fill_path_info(&name); |
| 67 | 70 | value.name = env_value; |
| 68 | 71 | tomoyo_fill_path_info(&value); |
| 69 | 72 | for (i = 0; i < envc; envp++, checked++, i++) { |
| 70 | 73 | bool result; |
| 74 | + | |
| 71 | 75 | if (!tomoyo_path_matches_pattern(&name, envp->name)) |
| 72 | 76 | continue; |
| 73 | 77 | *checked = 1; |
| @@ -113,6 +117,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 39 unmodified lines | ||
| 113 | 117 | bool result = true; |
| 114 | 118 | u8 local_checked[32]; |
| 115 | 119 | u8 *checked; |
| 120 | + | |
| 116 | 121 | if (argc + envc <= sizeof(local_checked)) { |
| 117 | 122 | checked = local_checked; |
| 118 | 123 | memset(local_checked, 0, sizeof(local_checked)); |
| @@ -131,6 +136,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 12 unmodified lines | ||
| 131 | 136 | /* Read. */ |
| 132 | 137 | const char *kaddr = dump->data; |
| 133 | 138 | const unsigned char c = kaddr[offset++]; |
| 139 | + | |
| 134 | 140 | if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) { |
| 135 | 141 | if (c == '\\') { |
| 136 | 142 | arg_ptr[arg_len++] = '\\'; |
| @@ -160,6 +166,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 23 unmodified lines | ||
| 160 | 166 | argv_count--; |
| 161 | 167 | } else if (envp_count) { |
| 162 | 168 | char *cp = strchr(arg_ptr, '='); |
| 169 | + | |
| 163 | 170 | if (cp) { |
| 164 | 171 | *cp = '\0'; |
| 165 | 172 | if (!tomoyo_envp(arg_ptr, cp + 1, |
| @@ -182,6 +189,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 16 unmodified lines | ||
| 182 | 189 | out: |
| 183 | 190 | if (result) { |
| 184 | 191 | int i; |
| 192 | + | |
| 185 | 193 | /* Check not-yet-checked entries. */ |
| 186 | 194 | for (i = 0; i < argc; i++) { |
| 187 | 195 | if (checked[i]) |
| @@ -229,6 +237,7 @@ static bool tomoyo_scan_exec_realpath(struct file *file, · 41 unmodified lines | ||
| 229 | 237 | { |
| 230 | 238 | bool result; |
| 231 | 239 | struct tomoyo_path_info exe; |
| 240 | + | |
| 232 | 241 | if (!file) |
| 233 | 242 | return false; |
| 234 | 243 | exe.name = tomoyo_realpath_from_path(&file->f_path); |
| @@ -250,6 +259,7 @@ static bool tomoyo_scan_exec_realpath(struct file *file, · 15 unmodified lines | ||
| 250 | 259 | static const struct tomoyo_path_info *tomoyo_get_dqword(char *start) |
| 251 | 260 | { |
| 252 | 261 | char *cp = start + strlen(start) - 1; |
| 262 | + | |
| 253 | 263 | if (cp == start || *start++ != '"' || *cp != '"') |
| 254 | 264 | return NULL; |
| 255 | 265 | *cp = '\0'; |
| @@ -270,6 +280,7 @@ static bool tomoyo_parse_name_union_quoted(struct tomoyo_acl_param *param, · 14 unmodified lines | ||
| 270 | 280 | struct tomoyo_name_union *ptr) |
| 271 | 281 | { |
| 272 | 282 | char *filename = param->data; |
| 283 | + | |
| 273 | 284 | if (*filename == '@') |
| 274 | 285 | return tomoyo_parse_name_union(param, ptr); |
| 275 | 286 | ptr->filename = tomoyo_get_dqword(filename); |
| @@ -310,6 +321,7 @@ static bool tomoyo_parse_envp(char *left, char *right, · 34 unmodified lines | ||
| 310 | 321 | const struct tomoyo_path_info *name; |
| 311 | 322 | const struct tomoyo_path_info *value; |
| 312 | 323 | char *cp = left + strlen(left) - 1; |
| 324 | + | |
| 313 | 325 | if (*cp-- != ']' || *cp != '"') |
| 314 | 326 | goto out; |
| 315 | 327 | *cp = '\0'; |
| @@ -364,6 +376,7 @@ static inline bool tomoyo_same_condition(const struct tomoyo_condition *a, · 48 unmodified lines | ||
| 364 | 376 | static u8 tomoyo_condition_type(const char *word) |
| 365 | 377 | { |
| 366 | 378 | u8 i; |
| 379 | + | |
| 367 | 380 | for (i = 0; i < TOMOYO_MAX_CONDITION_KEYWORD; i++) { |
| 368 | 381 | if (!strcmp(word, tomoyo_condition_keyword[i])) |
| 369 | 382 | break; |
| @@ -395,6 +408,7 @@ static struct tomoyo_condition *tomoyo_commit_condition · 25 unmodified lines | ||
| 395 | 408 | { |
| 396 | 409 | struct tomoyo_condition *ptr; |
| 397 | 410 | bool found = false; |
| 411 | + | |
| 398 | 412 | if (mutex_lock_interruptible(&tomoyo_policy_lock)) { |
| 399 | 413 | dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__); |
| 400 | 414 | ptr = NULL; |
| @@ -442,12 +456,14 @@ static char *tomoyo_get_transit_preference(struct tomoyo_acl_param *param, · 41 unmodified lines | ||
| 442 | 456 | { |
| 443 | 457 | char * const pos = param->data; |
| 444 | 458 | bool flag; |
| 459 | + | |
| 445 | 460 | if (*pos == '<') { |
| 446 | 461 | e->transit = tomoyo_get_domainname(param); |
| 447 | 462 | goto done; |
| 448 | 463 | } |
| 449 | 464 | { |
| 450 | 465 | char *cp = strchr(pos, ' '); |
| 466 | + | |
| 451 | 467 | if (cp) |
| 452 | 468 | *cp = '\0'; |
| 453 | 469 | flag = tomoyo_correct_path(pos) || !strcmp(pos, "keep") || |
| @@ -489,6 +505,7 @@ struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param) · 35 unmodified lines | ||
| 489 | 505 | tomoyo_get_transit_preference(param, &e); |
| 490 | 506 | char * const end_of_string = start_of_string + strlen(start_of_string); |
| 491 | 507 | char *pos; |
| 508 | + | |
| 492 | 509 | rerun: |
| 493 | 510 | pos = start_of_string; |
| 494 | 511 | while (1) { |
| @@ -498,6 +515,7 @@ rerun: · 3 unmodified lines | ||
| 498 | 515 | char *cp; |
| 499 | 516 | char *right_word; |
| 500 | 517 | bool is_not; |
| 518 | + | |
| 501 | 519 | if (!*left_word) |
| 502 | 520 | break; |
| 503 | 521 | /* |
| @@ -622,8 +640,8 @@ rerun: · 118 unmodified lines | ||
| 622 | 640 | } |
| 623 | 641 | store_value: |
| 624 | 642 | if (!condp) { |
| 625 | − dprintk(KERN_WARNING "%u: dry_run left=%u right=%u " | |
| 626 | − "match=%u\n", __LINE__, left, right, !is_not); | |
| 643 | + dprintk(KERN_WARNING "%u: dry_run left=%u right=%u match=%u\n", | |
| 644 | + __LINE__, left, right, !is_not); | |
| 627 | 645 | continue; |
| 628 | 646 | } |
| 629 | 647 | condp->left = left; |
| @@ -660,6 +678,7 @@ store_value: · 30 unmodified lines | ||
| 660 | 678 | envp = (struct tomoyo_envp *) (argv + e.argc); |
| 661 | 679 | { |
| 662 | 680 | bool flag = false; |
| 681 | + | |
| 663 | 682 | for (pos = start_of_string; pos < end_of_string; pos++) { |
| 664 | 683 | if (*pos) |
| 665 | 684 | continue; |
| @@ -698,6 +717,7 @@ void tomoyo_get_attributes(struct tomoyo_obj_info *obj) · 32 unmodified lines | ||
| 698 | 717 | |
| 699 | 718 | for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) { |
| 700 | 719 | struct inode *inode; |
| 720 | + | |
| 701 | 721 | switch (i) { |
| 702 | 722 | case TOMOYO_PATH1: |
| 703 | 723 | dentry = obj->path1.dentry; |
| @@ -718,6 +738,7 @@ void tomoyo_get_attributes(struct tomoyo_obj_info *obj) · 14 unmodified lines | ||
| 718 | 738 | inode = d_backing_inode(dentry); |
| 719 | 739 | if (inode) { |
| 720 | 740 | struct tomoyo_mini_stat *stat = &obj->stat[i]; |
| 741 | + | |
| 721 | 742 | stat->uid = inode->i_uid; |
| 722 | 743 | stat->gid = inode->i_gid; |
| 723 | 744 | stat->ino = inode->i_ino; |
| @@ -726,8 +747,7 @@ void tomoyo_get_attributes(struct tomoyo_obj_info *obj) · 2 unmodified lines | ||
| 726 | 747 | stat->rdev = inode->i_rdev; |
| 727 | 748 | obj->stat_valid[i] = true; |
| 728 | 749 | } |
| 729 | − if (i & 1) /* i == TOMOYO_PATH1_PARENT || | |
| 730 | − i == TOMOYO_PATH2_PARENT */ | |
| 750 | + if (i & 1) /* TOMOYO_PATH1_PARENT or TOMOYO_PATH2_PARENT */ | |
| 731 | 751 | dput(dentry); |
| 732 | 752 | } |
| 733 | 753 | } |
| @@ -758,6 +778,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 24 unmodified lines | ||
| 758 | 778 | u16 argc; |
| 759 | 779 | u16 envc; |
| 760 | 780 | struct linux_binprm *bprm = NULL; |
| 781 | + | |
| 761 | 782 | if (!cond) |
| 762 | 783 | return true; |
| 763 | 784 | condc = cond->condc; |
| @@ -780,6 +801,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 16 unmodified lines | ||
| 780 | 801 | const u8 right = condp->right; |
| 781 | 802 | bool is_bitop[2] = { false, false }; |
| 782 | 803 | u8 j; |
| 804 | + | |
| 783 | 805 | condp++; |
| 784 | 806 | /* Check argv[] and envp[] later. */ |
| 785 | 807 | if (left == TOMOYO_ARGV_ENTRY || left == TOMOYO_ENVP_ENTRY) |
| @@ -787,10 +809,11 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 1 unmodified line | ||
| 787 | 809 | /* Check string expressions. */ |
| 788 | 810 | if (right == TOMOYO_NAME_UNION) { |
| 789 | 811 | const struct tomoyo_name_union *ptr = names_p++; |
| 812 | + struct tomoyo_path_info *symlink; | |
| 813 | + struct tomoyo_execve *ee; | |
| 814 | + struct file *file; | |
| 815 | + | |
| 790 | 816 | switch (left) { |
| 791 | − struct tomoyo_path_info *symlink; | |
| 792 | − struct tomoyo_execve *ee; | |
| 793 | − struct file *file; | |
| 794 | 817 | case TOMOYO_SYMLINK_TARGET: |
| 795 | 818 | symlink = obj ? obj->symlink_target : NULL; |
| 796 | 819 | if (!symlink || |
| @@ -812,6 +835,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 15 unmodified lines | ||
| 812 | 835 | for (j = 0; j < 2; j++) { |
| 813 | 836 | const u8 index = j ? right : left; |
| 814 | 837 | unsigned long value = 0; |
| 838 | + | |
| 815 | 839 | switch (index) { |
| 816 | 840 | case TOMOYO_TASK_UID: |
| 817 | 841 | value = from_kuid(&init_user_ns, current_uid()); |
| @@ -874,31 +898,31 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 56 unmodified lines | ||
| 874 | 898 | value = S_ISVTX; |
| 875 | 899 | break; |
| 876 | 900 | case TOMOYO_MODE_OWNER_READ: |
| 877 | − value = S_IRUSR; | |
| 901 | + value = 0400; | |
| 878 | 902 | break; |
| 879 | 903 | case TOMOYO_MODE_OWNER_WRITE: |
| 880 | − value = S_IWUSR; | |
| 904 | + value = 0200; | |
| 881 | 905 | break; |
| 882 | 906 | case TOMOYO_MODE_OWNER_EXECUTE: |
| 883 | − value = S_IXUSR; | |
| 907 | + value = 0100; | |
| 884 | 908 | break; |
| 885 | 909 | case TOMOYO_MODE_GROUP_READ: |
| 886 | − value = S_IRGRP; | |
| 910 | + value = 0040; | |
| 887 | 911 | break; |
| 888 | 912 | case TOMOYO_MODE_GROUP_WRITE: |
| 889 | − value = S_IWGRP; | |
| 913 | + value = 0020; | |
| 890 | 914 | break; |
| 891 | 915 | case TOMOYO_MODE_GROUP_EXECUTE: |
| 892 | − value = S_IXGRP; | |
| 916 | + value = 0010; | |
| 893 | 917 | break; |
| 894 | 918 | case TOMOYO_MODE_OTHERS_READ: |
| 895 | − value = S_IROTH; | |
| 919 | + value = 0004; | |
| 896 | 920 | break; |
| 897 | 921 | case TOMOYO_MODE_OTHERS_WRITE: |
| 898 | − value = S_IWOTH; | |
| 922 | + value = 0002; | |
| 899 | 923 | break; |
| 900 | 924 | case TOMOYO_MODE_OTHERS_EXECUTE: |
| 901 | − value = S_IXOTH; | |
| 925 | + value = 0001; | |
| 902 | 926 | break; |
| 903 | 927 | case TOMOYO_EXEC_ARGC: |
| 904 | 928 | if (!bprm) |
| @@ -923,6 +947,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 18 unmodified lines | ||
| 923 | 947 | { |
| 924 | 948 | u8 stat_index; |
| 925 | 949 | struct tomoyo_mini_stat *stat; |
| 950 | + | |
| 926 | 951 | switch (index) { |
| 927 | 952 | case TOMOYO_PATH1_UID: |
| 928 | 953 | case TOMOYO_PATH1_GID: |
| @@ -1036,12 +1061,14 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 107 unmodified lines | ||
| 1036 | 1061 | if (left == TOMOYO_NUMBER_UNION) { |
| 1037 | 1062 | /* Fetch values now. */ |
| 1038 | 1063 | const struct tomoyo_number_union *ptr = numbers_p++; |
| 1064 | + | |
| 1039 | 1065 | min_v[0] = ptr->values[0]; |
| 1040 | 1066 | max_v[0] = ptr->values[1]; |
| 1041 | 1067 | } |
| 1042 | 1068 | if (right == TOMOYO_NUMBER_UNION) { |
| 1043 | 1069 | /* Fetch values now. */ |
| 1044 | 1070 | const struct tomoyo_number_union *ptr = numbers_p++; |
| 1071 | + | |
| 1045 | 1072 | if (ptr->group) { |
| 1046 | 1073 | if (tomoyo_number_matches_group(min_v[0], |
| 1047 | 1074 | max_v[0], |
| @@ -30,10 +30,10 @@ struct tomoyo_domain_info tomoyo_kernel_domain; · 29 unmodified lines | ||
| 30 | 30 | */ |
| 31 | 31 | int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size, |
| 32 | 32 | struct tomoyo_acl_param *param, |
| 33 | − bool (*check_duplicate) (const struct tomoyo_acl_head | |
| 34 | − *, | |
| 35 | − const struct tomoyo_acl_head | |
| 36 | − *)) | |
| 33 | + bool (*check_duplicate)(const struct tomoyo_acl_head | |
| 34 | + *, | |
| 35 | + const struct tomoyo_acl_head | |
| 36 | + *)) | |
| 37 | 37 | { |
| 38 | 38 | int error = param->is_delete ? -ENOENT : -ENOMEM; |
| 39 | 39 | struct tomoyo_acl_head *entry; |
| @@ -90,13 +90,13 @@ static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *a, · 50 unmodified lines | ||
| 90 | 90 | */ |
| 91 | 91 | int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size, |
| 92 | 92 | struct tomoyo_acl_param *param, |
| 93 | − bool (*check_duplicate) (const struct tomoyo_acl_info | |
| 94 | − *, | |
| 95 | − const struct tomoyo_acl_info | |
| 96 | − *), | |
| 97 | − bool (*merge_duplicate) (struct tomoyo_acl_info *, | |
| 98 | − struct tomoyo_acl_info *, | |
| 99 | − const bool)) | |
| 93 | + bool (*check_duplicate)(const struct tomoyo_acl_info | |
| 94 | + *, | |
| 95 | + const struct tomoyo_acl_info | |
| 96 | + *), | |
| 97 | + bool (*merge_duplicate)(struct tomoyo_acl_info *, | |
| 98 | + struct tomoyo_acl_info *, | |
| 99 | + const bool)) | |
| 100 | 100 | { |
| 101 | 101 | const bool is_delete = param->is_delete; |
| 102 | 102 | int error = is_delete ? -ENOENT : -ENOMEM; |
| @@ -157,13 +157,13 @@ out: · 54 unmodified lines | ||
| 157 | 157 | * Caller holds tomoyo_read_lock(). |
| 158 | 158 | */ |
| 159 | 159 | void tomoyo_check_acl(struct tomoyo_request_info *r, |
| 160 | − bool (*check_entry) (struct tomoyo_request_info *, | |
| 161 | − const struct tomoyo_acl_info *)) | |
| 160 | + bool (*check_entry)(struct tomoyo_request_info *, | |
| 161 | + const struct tomoyo_acl_info *)) | |
| 162 | 162 | { |
| 163 | 163 | const struct tomoyo_domain_info *domain = r->domain; |
| 164 | 164 | struct tomoyo_acl_info *ptr; |
| 165 | − bool retried = false; | |
| 166 | 165 | const struct list_head *list = &domain->acl_info_list; |
| 166 | + u16 i = 0; | |
| 167 | 167 | |
| 168 | 168 | retry: |
| 169 | 169 | list_for_each_entry_rcu(ptr, list, list) { |
| @@ -177,9 +177,10 @@ retry: · 7 unmodified lines | ||
| 177 | 177 | r->granted = true; |
| 178 | 178 | return; |
| 179 | 179 | } |
| 180 | − if (!retried) { | |
| 181 | − retried = true; | |
| 182 | − list = &domain->ns->acl_group[domain->group]; | |
| 180 | + for (; i < TOMOYO_MAX_ACL_GROUPS; i++) { | |
| 181 | + if (!test_bit(i, domain->group)) | |
| 182 | + continue; | |
| 183 | + list = &domain->ns->acl_group[i++]; | |
| 183 | 184 | goto retry; |
| 184 | 185 | } |
| 185 | 186 | r->granted = false; |
| @@ -198,6 +199,7 @@ LIST_HEAD(tomoyo_domain_list); · 12 unmodified lines | ||
| 198 | 199 | static const char *tomoyo_last_word(const char *name) |
| 199 | 200 | { |
| 200 | 201 | const char *cp = strrchr(name, ' '); |
| 202 | + | |
| 201 | 203 | if (cp) |
| 202 | 204 | return cp + 1; |
| 203 | 205 | return name; |
| @@ -220,6 +222,7 @@ static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a, · 16 unmodified lines | ||
| 220 | 222 | const struct tomoyo_transition_control *p2 = container_of(b, |
| 221 | 223 | typeof(*p2), |
| 222 | 224 | head); |
| 225 | + | |
| 223 | 226 | return p1->type == p2->type && p1->is_last_name == p2->is_last_name |
| 224 | 227 | && p1->domainname == p2->domainname |
| 225 | 228 | && p1->program == p2->program; |
| @@ -240,6 +243,7 @@ int tomoyo_write_transition_control(struct tomoyo_acl_param *param, · 14 unmodified lines | ||
| 240 | 243 | int error = param->is_delete ? -ENOENT : -ENOMEM; |
| 241 | 244 | char *program = param->data; |
| 242 | 245 | char *domainname = strstr(program, " from "); |
| 246 | + | |
| 243 | 247 | if (domainname) { |
| 244 | 248 | *domainname = '\0'; |
| 245 | 249 | domainname += 6; |
| @@ -293,6 +297,7 @@ static inline bool tomoyo_scan_transition · 47 unmodified lines | ||
| 293 | 297 | const enum tomoyo_transition_type type) |
| 294 | 298 | { |
| 295 | 299 | const struct tomoyo_transition_control *ptr; |
| 300 | + | |
| 296 | 301 | list_for_each_entry_rcu(ptr, list, head.list) { |
| 297 | 302 | if (ptr->head.is_deleted || ptr->type != type) |
| 298 | 303 | continue; |
| @@ -338,9 +343,11 @@ static enum tomoyo_transition_type tomoyo_transition_type · 39 unmodified lines | ||
| 338 | 343 | { |
| 339 | 344 | const char *last_name = tomoyo_last_word(domainname->name); |
| 340 | 345 | enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET; |
| 346 | + | |
| 341 | 347 | while (type < TOMOYO_MAX_TRANSITION_TYPE) { |
| 342 | 348 | const struct list_head * const list = |
| 343 | 349 | &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL]; |
| 350 | + | |
| 344 | 351 | if (!tomoyo_scan_transition(list, domainname, program, |
| 345 | 352 | last_name, type)) { |
| 346 | 353 | type++; |
| @@ -375,6 +382,7 @@ static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a, · 28 unmodified lines | ||
| 375 | 382 | head); |
| 376 | 383 | const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2), |
| 377 | 384 | head); |
| 385 | + | |
| 378 | 386 | return p1->original_name == p2->original_name && |
| 379 | 387 | p1->aggregated_name == p2->aggregated_name; |
| 380 | 388 | } |
| @@ -394,6 +402,7 @@ int tomoyo_write_aggregator(struct tomoyo_acl_param *param) · 13 unmodified lines | ||
| 394 | 402 | int error = param->is_delete ? -ENOENT : -ENOMEM; |
| 395 | 403 | const char *original_name = tomoyo_read_token(param); |
| 396 | 404 | const char *aggregated_name = tomoyo_read_token(param); |
| 405 | + | |
| 397 | 406 | if (!tomoyo_correct_word(original_name) || |
| 398 | 407 | !tomoyo_correct_path(aggregated_name)) |
| 399 | 408 | return -EINVAL; |
| @@ -426,6 +435,7 @@ static struct tomoyo_policy_namespace *tomoyo_find_namespace · 26 unmodified lines | ||
| 426 | 435 | (const char *name, const unsigned int len) |
| 427 | 436 | { |
| 428 | 437 | struct tomoyo_policy_namespace *ns; |
| 438 | + | |
| 429 | 439 | list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) { |
| 430 | 440 | if (strncmp(name, ns->name, len) || |
| 431 | 441 | (name[len] && name[len] != ' ')) |
| @@ -451,6 +461,7 @@ struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname) · 19 unmodified lines | ||
| 451 | 461 | struct tomoyo_policy_namespace *entry; |
| 452 | 462 | const char *cp = domainname; |
| 453 | 463 | unsigned int len = 0; |
| 464 | + | |
| 454 | 465 | while (*cp && *cp++ != ' ') |
| 455 | 466 | len++; |
| 456 | 467 | ptr = tomoyo_find_namespace(domainname, len); |
| @@ -466,6 +477,7 @@ struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname) · 9 unmodified lines | ||
| 466 | 477 | ptr = tomoyo_find_namespace(domainname, len); |
| 467 | 478 | if (!ptr && tomoyo_memory_ok(entry)) { |
| 468 | 479 | char *name = (char *) (entry + 1); |
| 480 | + | |
| 469 | 481 | ptr = entry; |
| 470 | 482 | memmove(name, domainname, len); |
| 471 | 483 | name[len] = '\0'; |
| @@ -490,6 +502,7 @@ static bool tomoyo_namespace_jump(const char *domainname) · 18 unmodified lines | ||
| 490 | 502 | { |
| 491 | 503 | const char *namespace = tomoyo_current_namespace()->name; |
| 492 | 504 | const int len = strlen(namespace); |
| 505 | + | |
| 493 | 506 | return strncmp(domainname, namespace, len) || |
| 494 | 507 | (domainname[len] && domainname[len] != ' '); |
| 495 | 508 | } |
| @@ -510,6 +523,7 @@ struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, · 14 unmodified lines | ||
| 510 | 523 | struct tomoyo_domain_info e = { }; |
| 511 | 524 | struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname); |
| 512 | 525 | bool created = false; |
| 526 | + | |
| 513 | 527 | if (entry) { |
| 514 | 528 | if (transit) { |
| 515 | 529 | /* |
| @@ -546,8 +560,9 @@ struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, · 30 unmodified lines | ||
| 546 | 560 | */ |
| 547 | 561 | if (transit) { |
| 548 | 562 | const struct tomoyo_domain_info *domain = tomoyo_domain(); |
| 563 | + | |
| 549 | 564 | e.profile = domain->profile; |
| 550 | − e.group = domain->group; | |
| 565 | + memcpy(e.group, domain->group, sizeof(e.group)); | |
| 551 | 566 | } |
| 552 | 567 | e.domainname = tomoyo_get_name(domainname); |
| 553 | 568 | if (!e.domainname) |
| @@ -569,12 +584,17 @@ out: · 15 unmodified lines | ||
| 569 | 584 | if (entry && transit) { |
| 570 | 585 | if (created) { |
| 571 | 586 | struct tomoyo_request_info r; |
| 587 | + int i; | |
| 588 | + | |
| 572 | 589 | tomoyo_init_request_info(&r, entry, |
| 573 | 590 | TOMOYO_MAC_FILE_EXECUTE); |
| 574 | 591 | r.granted = false; |
| 575 | 592 | tomoyo_write_log(&r, "use_profile %u\n", |
| 576 | 593 | entry->profile); |
| 577 | − tomoyo_write_log(&r, "use_group %u\n", entry->group); | |
| 594 | + for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++) | |
| 595 | + if (test_bit(i, entry->group)) | |
| 596 | + tomoyo_write_log(&r, "use_group %u\n", | |
| 597 | + i); | |
| 578 | 598 | tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES); |
| 579 | 599 | } |
| 580 | 600 | } |
| @@ -712,6 +732,7 @@ retry: · 131 unmodified lines | ||
| 712 | 732 | struct tomoyo_aggregator *ptr; |
| 713 | 733 | struct list_head *list = |
| 714 | 734 | &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR]; |
| 735 | + | |
| 715 | 736 | /* Check 'aggregator' directive. */ |
| 716 | 737 | candidate = &exename; |
| 717 | 738 | list_for_each_entry_rcu(ptr, list, head.list) { |
| @@ -747,6 +768,7 @@ retry: · 29 unmodified lines | ||
| 747 | 768 | */ |
| 748 | 769 | if (ee->transition) { |
| 749 | 770 | const char *domainname = ee->transition->name; |
| 771 | + | |
| 750 | 772 | reject_on_transition_failure = true; |
| 751 | 773 | if (!strcmp(domainname, "keep")) |
| 752 | 774 | goto force_keep_domain; |
| @@ -758,6 +780,7 @@ retry: · 5 unmodified lines | ||
| 758 | 780 | goto force_initialize_domain; |
| 759 | 781 | if (!strcmp(domainname, "parent")) { |
| 760 | 782 | char *cp; |
| 783 | + | |
| 761 | 784 | strncpy(ee->tmp, old_domain->domainname->name, |
| 762 | 785 | TOMOYO_EXEC_TMPSIZE - 1); |
| 763 | 786 | cp = strrchr(ee->tmp, ' '); |
| @@ -822,8 +845,7 @@ force_jump_domain: · 58 unmodified lines | ||
| 822 | 845 | if (domain) |
| 823 | 846 | retval = 0; |
| 824 | 847 | else if (reject_on_transition_failure) { |
| 825 | − printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n", | |
| 826 | − ee->tmp); | |
| 848 | + pr_warn("ERROR: Domain '%s' not ready.\n", ee->tmp); | |
| 827 | 849 | retval = -ENOMEM; |
| 828 | 850 | } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING) |
| 829 | 851 | retval = -ENOMEM; |
| @@ -834,16 +856,20 @@ force_jump_domain: · 4 unmodified lines | ||
| 834 | 856 | ee->r.granted = false; |
| 835 | 857 | tomoyo_write_log(&ee->r, "%s", tomoyo_dif |
| 836 | 858 | [TOMOYO_DIF_TRANSITION_FAILED]); |
| 837 | − printk(KERN_WARNING | |
| 838 | − "ERROR: Domain '%s' not defined.\n", ee->tmp); | |
| 859 | + pr_warn("ERROR: Domain '%s' not defined.\n", ee->tmp); | |
| 839 | 860 | } |
| 840 | 861 | } |
| 841 | 862 | out: |
| 842 | 863 | if (!domain) |
| 843 | 864 | domain = old_domain; |
| 844 | 865 | /* Update reference count on "struct tomoyo_domain_info". */ |
| 845 | − atomic_inc(&domain->users); | |
| 846 | − bprm->cred->security = domain; | |
| 866 | + { | |
| 867 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 868 | + | |
| 869 | + s->old_domain_info = s->domain_info; | |
| 870 | + s->domain_info = domain; | |
| 871 | + atomic_inc(&domain->users); | |
| 872 | + } | |
| 847 | 873 | kfree(exename.name); |
| 848 | 874 | if (!retval) { |
| 849 | 875 | ee->r.domain = domain; |
| @@ -214,6 +214,7 @@ static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r) · 213 unmodified lines | ||
| 214 | 214 | const u8 type = r->param.path_number.operation; |
| 215 | 215 | u8 radix; |
| 216 | 216 | char buffer[64]; |
| 217 | + | |
| 217 | 218 | switch (type) { |
| 218 | 219 | case TOMOYO_TYPE_CREATE: |
| 219 | 220 | case TOMOYO_TYPE_MKDIR: |
| @@ -253,6 +254,7 @@ static bool tomoyo_check_path_acl(struct tomoyo_request_info *r, · 33 unmodified lines | ||
| 253 | 254 | { |
| 254 | 255 | const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl), |
| 255 | 256 | head); |
| 257 | + | |
| 256 | 258 | if (acl->perm & (1 << r->param.path.operation)) { |
| 257 | 259 | r->param.path.matched_path = |
| 258 | 260 | tomoyo_compare_name_union(r->param.path.filename, |
| @@ -275,6 +277,7 @@ static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r, · 16 unmodified lines | ||
| 275 | 277 | { |
| 276 | 278 | const struct tomoyo_path_number_acl *acl = |
| 277 | 279 | container_of(ptr, typeof(*acl), head); |
| 280 | + | |
| 278 | 281 | return (acl->perm & (1 << r->param.path_number.operation)) && |
| 279 | 282 | tomoyo_compare_number_union(r->param.path_number.number, |
| 280 | 283 | &acl->number) && |
| @@ -295,6 +298,7 @@ static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r, · 14 unmodified lines | ||
| 295 | 298 | { |
| 296 | 299 | const struct tomoyo_path2_acl *acl = |
| 297 | 300 | container_of(ptr, typeof(*acl), head); |
| 301 | + | |
| 298 | 302 | return (acl->perm & (1 << r->param.path2.operation)) && |
| 299 | 303 | tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1) |
| 300 | 304 | && tomoyo_compare_name_union(r->param.path2.filename2, |
| @@ -314,6 +318,7 @@ static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r, · 13 unmodified lines | ||
| 314 | 318 | { |
| 315 | 319 | const struct tomoyo_mkdev_acl *acl = |
| 316 | 320 | container_of(ptr, typeof(*acl), head); |
| 321 | + | |
| 317 | 322 | return (acl->perm & (1 << r->param.mkdev.operation)) && |
| 318 | 323 | tomoyo_compare_number_union(r->param.mkdev.mode, |
| 319 | 324 | &acl->mode) && |
| @@ -338,6 +343,7 @@ static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a, · 18 unmodified lines | ||
| 338 | 343 | { |
| 339 | 344 | const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head); |
| 340 | 345 | const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head); |
| 346 | + | |
| 341 | 347 | return tomoyo_same_name_union(&p1->name, &p2->name); |
| 342 | 348 | } |
| 343 | 349 | |
| @@ -358,6 +364,7 @@ static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a, · 14 unmodified lines | ||
| 358 | 364 | ->perm; |
| 359 | 365 | u16 perm = *a_perm; |
| 360 | 366 | const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm; |
| 367 | + | |
| 361 | 368 | if (is_delete) |
| 362 | 369 | perm &= ~b_perm; |
| 363 | 370 | else |
| @@ -384,6 +391,7 @@ static int tomoyo_update_path_acl(const u16 perm, · 20 unmodified lines | ||
| 384 | 391 | .perm = perm |
| 385 | 392 | }; |
| 386 | 393 | int error; |
| 394 | + | |
| 387 | 395 | if (!tomoyo_parse_name_union(param, &e.name)) |
| 388 | 396 | error = -EINVAL; |
| 389 | 397 | else |
| @@ -407,6 +415,7 @@ static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a, · 17 unmodified lines | ||
| 407 | 415 | { |
| 408 | 416 | const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head); |
| 409 | 417 | const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head); |
| 418 | + | |
| 410 | 419 | return tomoyo_same_name_union(&p1->name, &p2->name) && |
| 411 | 420 | tomoyo_same_number_union(&p1->mode, &p2->mode) && |
| 412 | 421 | tomoyo_same_number_union(&p1->major, &p2->major) && |
| @@ -431,6 +440,7 @@ static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a, · 18 unmodified lines | ||
| 431 | 440 | u8 perm = *a_perm; |
| 432 | 441 | const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head) |
| 433 | 442 | ->perm; |
| 443 | + | |
| 434 | 444 | if (is_delete) |
| 435 | 445 | perm &= ~b_perm; |
| 436 | 446 | else |
| @@ -457,6 +467,7 @@ static int tomoyo_update_mkdev_acl(const u8 perm, · 20 unmodified lines | ||
| 457 | 467 | .perm = perm |
| 458 | 468 | }; |
| 459 | 469 | int error; |
| 470 | + | |
| 460 | 471 | if (!tomoyo_parse_name_union(param, &e.name) || |
| 461 | 472 | !tomoyo_parse_number_union(param, &e.mode) || |
| 462 | 473 | !tomoyo_parse_number_union(param, &e.major) || |
| @@ -486,6 +497,7 @@ static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a, · 23 unmodified lines | ||
| 486 | 497 | { |
| 487 | 498 | const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head); |
| 488 | 499 | const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head); |
| 500 | + | |
| 489 | 501 | return tomoyo_same_name_union(&p1->name1, &p2->name1) && |
| 490 | 502 | tomoyo_same_name_union(&p1->name2, &p2->name2); |
| 491 | 503 | } |
| @@ -507,6 +519,7 @@ static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a, · 15 unmodified lines | ||
| 507 | 519 | ->perm; |
| 508 | 520 | u8 perm = *a_perm; |
| 509 | 521 | const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm; |
| 522 | + | |
| 510 | 523 | if (is_delete) |
| 511 | 524 | perm &= ~b_perm; |
| 512 | 525 | else |
| @@ -533,6 +546,7 @@ static int tomoyo_update_path2_acl(const u8 perm, · 20 unmodified lines | ||
| 533 | 546 | .perm = perm |
| 534 | 547 | }; |
| 535 | 548 | int error; |
| 549 | + | |
| 536 | 550 | if (!tomoyo_parse_name_union(param, &e.name1) || |
| 537 | 551 | !tomoyo_parse_name_union(param, &e.name2)) |
| 538 | 552 | error = -EINVAL; |
| @@ -621,6 +635,7 @@ static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a, · 82 unmodified lines | ||
| 621 | 635 | head); |
| 622 | 636 | const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2), |
| 623 | 637 | head); |
| 638 | + | |
| 624 | 639 | return tomoyo_same_name_union(&p1->name, &p2->name) && |
| 625 | 640 | tomoyo_same_number_union(&p1->number, &p2->number); |
| 626 | 641 | } |
| @@ -643,6 +658,7 @@ static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a, · 16 unmodified lines | ||
| 643 | 658 | u8 perm = *a_perm; |
| 644 | 659 | const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head) |
| 645 | 660 | ->perm; |
| 661 | + | |
| 646 | 662 | if (is_delete) |
| 647 | 663 | perm &= ~b_perm; |
| 648 | 664 | else |
| @@ -667,6 +683,7 @@ static int tomoyo_update_path_number_acl(const u8 perm, · 18 unmodified lines | ||
| 667 | 683 | .perm = perm |
| 668 | 684 | }; |
| 669 | 685 | int error; |
| 686 | + | |
| 670 | 687 | if (!tomoyo_parse_name_union(param, &e.name) || |
| 671 | 688 | !tomoyo_parse_number_union(param, &e.number)) |
| 672 | 689 | error = -EINVAL; |
| @@ -947,6 +964,7 @@ static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a, · 274 unmodified lines | ||
| 947 | 964 | { |
| 948 | 965 | const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head); |
| 949 | 966 | const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head); |
| 967 | + | |
| 950 | 968 | return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) && |
| 951 | 969 | tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) && |
| 952 | 970 | tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) && |
| @@ -966,6 +984,7 @@ static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param) · 13 unmodified lines | ||
| 966 | 984 | { |
| 967 | 985 | struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL }; |
| 968 | 986 | int error; |
| 987 | + | |
| 969 | 988 | if (!tomoyo_parse_name_union(param, &e.dev_name) || |
| 970 | 989 | !tomoyo_parse_name_union(param, &e.dir_name) || |
| 971 | 990 | !tomoyo_parse_name_union(param, &e.fs_type) || |
| @@ -995,6 +1014,7 @@ int tomoyo_write_file(struct tomoyo_acl_param *param) · 23 unmodified lines | ||
| 995 | 1014 | u16 perm = 0; |
| 996 | 1015 | u8 type; |
| 997 | 1016 | const char *operation = tomoyo_read_token(param); |
| 1017 | + | |
| 998 | 1018 | for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) |
| 999 | 1019 | if (tomoyo_permstr(operation, tomoyo_path_keyword[type])) |
| 1000 | 1020 | perm |= 1 << type; |
| @@ -77,11 +77,13 @@ static bool tomoyo_name_used_by_io_buffer(const char *string) · 76 unmodified lines | ||
| 77 | 77 | spin_lock(&tomoyo_io_buffer_list_lock); |
| 78 | 78 | list_for_each_entry(head, &tomoyo_io_buffer_list, list) { |
| 79 | 79 | int i; |
| 80 | + | |
| 80 | 81 | head->users++; |
| 81 | 82 | spin_unlock(&tomoyo_io_buffer_list_lock); |
| 82 | 83 | mutex_lock(&head->io_sem); |
| 83 | 84 | for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) { |
| 84 | 85 | const char *w = head->r.w[i]; |
| 86 | + | |
| 85 | 87 | if (w < string || w > string + size) |
| 86 | 88 | continue; |
| 87 | 89 | in_use = true; |
| @@ -108,6 +110,7 @@ static inline void tomoyo_del_transition_control(struct list_head *element) · 20 unmodified lines | ||
| 108 | 110 | { |
| 109 | 111 | struct tomoyo_transition_control *ptr = |
| 110 | 112 | container_of(element, typeof(*ptr), head.list); |
| 113 | + | |
| 111 | 114 | tomoyo_put_name(ptr->domainname); |
| 112 | 115 | tomoyo_put_name(ptr->program); |
| 113 | 116 | } |
| @@ -123,6 +126,7 @@ static inline void tomoyo_del_aggregator(struct list_head *element) · 9 unmodified lines | ||
| 123 | 126 | { |
| 124 | 127 | struct tomoyo_aggregator *ptr = |
| 125 | 128 | container_of(element, typeof(*ptr), head.list); |
| 129 | + | |
| 126 | 130 | tomoyo_put_name(ptr->original_name); |
| 127 | 131 | tomoyo_put_name(ptr->aggregated_name); |
| 128 | 132 | } |
| @@ -138,6 +142,7 @@ static inline void tomoyo_del_manager(struct list_head *element) · 9 unmodified lines | ||
| 138 | 142 | { |
| 139 | 143 | struct tomoyo_manager *ptr = |
| 140 | 144 | container_of(element, typeof(*ptr), head.list); |
| 145 | + | |
| 141 | 146 | tomoyo_put_name(ptr->manager); |
| 142 | 147 | } |
| 143 | 148 | |
| @@ -152,6 +157,7 @@ static void tomoyo_del_acl(struct list_head *element) · 8 unmodified lines | ||
| 152 | 157 | { |
| 153 | 158 | struct tomoyo_acl_info *acl = |
| 154 | 159 | container_of(element, typeof(*acl), list); |
| 160 | + | |
| 155 | 161 | tomoyo_put_condition(acl->cond); |
| 156 | 162 | switch (acl->type) { |
| 157 | 163 | case TOMOYO_TYPE_PATH_ACL: |
| @@ -226,6 +232,7 @@ static void tomoyo_del_acl(struct list_head *element) · 68 unmodified lines | ||
| 226 | 232 | { |
| 227 | 233 | struct tomoyo_task_acl *entry = |
| 228 | 234 | container_of(acl, typeof(*entry), head); |
| 235 | + | |
| 229 | 236 | tomoyo_put_name(entry->domainname); |
| 230 | 237 | } |
| 231 | 238 | break; |
| @@ -247,6 +254,7 @@ static inline void tomoyo_del_domain(struct list_head *element) · 15 unmodified lines | ||
| 247 | 254 | container_of(element, typeof(*domain), list); |
| 248 | 255 | struct tomoyo_acl_info *acl; |
| 249 | 256 | struct tomoyo_acl_info *tmp; |
| 257 | + | |
| 250 | 258 | /* |
| 251 | 259 | * Since this domain is referenced from neither |
| 252 | 260 | * "struct tomoyo_io_buffer" nor "struct cred"->security, we can delete |
| @@ -286,6 +294,7 @@ void tomoyo_del_condition(struct list_head *element) · 33 unmodified lines | ||
| 286 | 294 | = (const struct tomoyo_argv *) (names_p + names_count); |
| 287 | 295 | const struct tomoyo_envp *envp |
| 288 | 296 | = (const struct tomoyo_envp *) (argv + argc); |
| 297 | + | |
| 289 | 298 | for (i = 0; i < numbers_count; i++) |
| 290 | 299 | tomoyo_put_number_union(numbers_p++); |
| 291 | 300 | for (i = 0; i < names_count; i++) |
| @@ -321,6 +330,7 @@ static inline void tomoyo_del_path_group(struct list_head *element) · 29 unmodified lines | ||
| 321 | 330 | { |
| 322 | 331 | struct tomoyo_path_group *member = |
| 323 | 332 | container_of(element, typeof(*member), head.list); |
| 333 | + | |
| 324 | 334 | tomoyo_put_name(member->member_name); |
| 325 | 335 | } |
| 326 | 336 | |
| @@ -335,6 +345,7 @@ static inline void tomoyo_del_group(struct list_head *element) · 8 unmodified lines | ||
| 335 | 345 | { |
| 336 | 346 | struct tomoyo_group *group = |
| 337 | 347 | container_of(element, typeof(*group), head.list); |
| 348 | + | |
| 338 | 349 | tomoyo_put_name(group->group_name); |
| 339 | 350 | } |
| 340 | 351 | |
| @@ -476,6 +487,7 @@ static void tomoyo_collect_member(const enum tomoyo_policy_id id, · 135 unmodified lines | ||
| 476 | 487 | { |
| 477 | 488 | struct tomoyo_acl_head *member; |
| 478 | 489 | struct tomoyo_acl_head *tmp; |
| 490 | + | |
| 479 | 491 | list_for_each_entry_safe(member, tmp, member_list, list) { |
| 480 | 492 | if (!member->is_deleted) |
| 481 | 493 | continue; |
| @@ -495,6 +507,7 @@ static void tomoyo_collect_acl(struct list_head *list) · 13 unmodified lines | ||
| 495 | 507 | { |
| 496 | 508 | struct tomoyo_acl_info *acl; |
| 497 | 509 | struct tomoyo_acl_info *tmp; |
| 510 | + | |
| 498 | 511 | list_for_each_entry_safe(acl, tmp, list, list) { |
| 499 | 512 | if (!acl->is_deleted) |
| 500 | 513 | continue; |
| @@ -513,10 +526,12 @@ static void tomoyo_collect_entry(void) · 12 unmodified lines | ||
| 513 | 526 | int i; |
| 514 | 527 | enum tomoyo_policy_id id; |
| 515 | 528 | struct tomoyo_policy_namespace *ns; |
| 529 | + | |
| 516 | 530 | mutex_lock(&tomoyo_policy_lock); |
| 517 | 531 | { |
| 518 | 532 | struct tomoyo_domain_info *domain; |
| 519 | 533 | struct tomoyo_domain_info *tmp; |
| 534 | + | |
| 520 | 535 | list_for_each_entry_safe(domain, tmp, &tomoyo_domain_list, |
| 521 | 536 | list) { |
| 522 | 537 | tomoyo_collect_acl(&domain->acl_info_list); |
| @@ -534,6 +549,7 @@ static void tomoyo_collect_entry(void) · 11 unmodified lines | ||
| 534 | 549 | { |
| 535 | 550 | struct tomoyo_shared_acl_head *ptr; |
| 536 | 551 | struct tomoyo_shared_acl_head *tmp; |
| 552 | + | |
| 537 | 553 | list_for_each_entry_safe(ptr, tmp, &tomoyo_condition_list, |
| 538 | 554 | list) { |
| 539 | 555 | if (atomic_read(&ptr->users) > 0) |
| @@ -547,6 +563,7 @@ static void tomoyo_collect_entry(void) · 7 unmodified lines | ||
| 547 | 563 | struct list_head *list = &ns->group_list[i]; |
| 548 | 564 | struct tomoyo_group *group; |
| 549 | 565 | struct tomoyo_group *tmp; |
| 566 | + | |
| 550 | 567 | switch (i) { |
| 551 | 568 | case 0: |
| 552 | 569 | id = TOMOYO_ID_PATH_GROUP; |
| @@ -574,6 +591,7 @@ static void tomoyo_collect_entry(void) · 21 unmodified lines | ||
| 574 | 591 | struct list_head *list = &tomoyo_name_list[i]; |
| 575 | 592 | struct tomoyo_shared_acl_head *ptr; |
| 576 | 593 | struct tomoyo_shared_acl_head *tmp; |
| 594 | + | |
| 577 | 595 | list_for_each_entry_safe(ptr, tmp, list, list) { |
| 578 | 596 | if (atomic_read(&ptr->users) > 0) |
| 579 | 597 | continue; |
| @@ -595,6 +613,7 @@ static int tomoyo_gc_thread(void *unused) · 15 unmodified lines | ||
| 595 | 613 | { |
| 596 | 614 | /* Garbage collector thread is exclusive. */ |
| 597 | 615 | static DEFINE_MUTEX(tomoyo_gc_mutex); |
| 616 | + | |
| 598 | 617 | if (!mutex_trylock(&tomoyo_gc_mutex)) |
| 599 | 618 | goto out; |
| 600 | 619 | tomoyo_collect_entry(); |
| @@ -75,11 +75,13 @@ int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type) · 74 unmodified lines | ||
| 75 | 75 | { |
| 76 | 76 | struct tomoyo_group *group = tomoyo_get_group(param, type); |
| 77 | 77 | int error = -EINVAL; |
| 78 | + | |
| 78 | 79 | if (!group) |
| 79 | 80 | return -ENOMEM; |
| 80 | 81 | param->list = &group->member_list; |
| 81 | 82 | if (type == TOMOYO_PATH_GROUP) { |
| 82 | 83 | struct tomoyo_path_group e = { }; |
| 84 | + | |
| 83 | 85 | e.member_name = tomoyo_get_name(tomoyo_read_token(param)); |
| 84 | 86 | if (!e.member_name) { |
| 85 | 87 | error = -ENOMEM; |
| @@ -90,6 +92,7 @@ int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type) · 4 unmodified lines | ||
| 90 | 92 | tomoyo_put_name(e.member_name); |
| 91 | 93 | } else if (type == TOMOYO_NUMBER_GROUP) { |
| 92 | 94 | struct tomoyo_number_group e = { }; |
| 95 | + | |
| 93 | 96 | if (param->data[0] == '@' || |
| 94 | 97 | !tomoyo_parse_number_union(param, &e.number)) |
| 95 | 98 | goto out; |
| @@ -129,6 +132,7 @@ tomoyo_path_matches_group(const struct tomoyo_path_info *pathname, · 33 unmodified lines | ||
| 129 | 132 | const struct tomoyo_group *group) |
| 130 | 133 | { |
| 131 | 134 | struct tomoyo_path_group *member; |
| 135 | + | |
| 132 | 136 | list_for_each_entry_rcu(member, &group->member_list, head.list) { |
| 133 | 137 | if (member->head.is_deleted) |
| 134 | 138 | continue; |
| @@ -156,6 +160,7 @@ bool tomoyo_number_matches_group(const unsigned long min, · 21 unmodified lines | ||
| 156 | 160 | { |
| 157 | 161 | struct tomoyo_number_group *member; |
| 158 | 162 | bool matched = false; |
| 163 | + | |
| 159 | 164 | list_for_each_entry_rcu(member, &group->member_list, head.list) { |
| 160 | 165 | if (member->head.is_deleted) |
| 161 | 166 | continue; |
| @@ -37,11 +37,12 @@ __setup("TOMOYO_loader=", tomoyo_loader_setup); · 36 unmodified lines | ||
| 37 | 37 | static bool tomoyo_policy_loader_exists(void) |
| 38 | 38 | { |
| 39 | 39 | struct path path; |
| 40 | + | |
| 40 | 41 | if (!tomoyo_loader) |
| 41 | 42 | tomoyo_loader = CONFIG_SECURITY_TOMOYO_POLICY_LOADER; |
| 42 | 43 | if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) { |
| 43 | − printk(KERN_INFO "Not activating Mandatory Access Control " | |
| 44 | − "as %s does not exist.\n", tomoyo_loader); | |
| 44 | + pr_info("Not activating Mandatory Access Control as %s does not exist.\n", | |
| 45 | + tomoyo_loader); | |
| 45 | 46 | return false; |
| 46 | 47 | } |
| 47 | 48 | path_put(&path); |
| @@ -96,8 +97,7 @@ void tomoyo_load_policy(const char *filename) · 48 unmodified lines | ||
| 96 | 97 | if (!tomoyo_policy_loader_exists()) |
| 97 | 98 | return; |
| 98 | 99 | done = true; |
| 99 | − printk(KERN_INFO "Calling %s to load policy. Please wait.\n", | |
| 100 | − tomoyo_loader); | |
| 100 | + pr_info("Calling %s to load policy. Please wait.\n", tomoyo_loader); | |
| 101 | 101 | argv[0] = (char *) tomoyo_loader; |
| 102 | 102 | argv[1] = NULL; |
| 103 | 103 | envp[0] = "HOME=/"; |
| @@ -19,9 +19,9 @@ void tomoyo_warn_oom(const char *function) · 18 unmodified lines | ||
| 19 | 19 | /* Reduce error messages. */ |
| 20 | 20 | static pid_t tomoyo_last_pid; |
| 21 | 21 | const pid_t pid = current->pid; |
| 22 | + | |
| 22 | 23 | if (tomoyo_last_pid != pid) { |
| 23 | − printk(KERN_WARNING "ERROR: Out of memory at %s.\n", | |
| 24 | − function); | |
| 24 | + pr_warn("ERROR: Out of memory at %s.\n", function); | |
| 25 | 25 | tomoyo_last_pid = pid; |
| 26 | 26 | } |
| 27 | 27 | if (!tomoyo_policy_loaded) |
| @@ -48,6 +48,7 @@ bool tomoyo_memory_ok(void *ptr) · 20 unmodified lines | ||
| 48 | 48 | { |
| 49 | 49 | if (ptr) { |
| 50 | 50 | const size_t s = ksize(ptr); |
| 51 | + | |
| 51 | 52 | tomoyo_memory_used[TOMOYO_MEMORY_POLICY] += s; |
| 52 | 53 | if (!tomoyo_memory_quota[TOMOYO_MEMORY_POLICY] || |
| 53 | 54 | tomoyo_memory_used[TOMOYO_MEMORY_POLICY] <= |
| @@ -73,6 +74,7 @@ bool tomoyo_memory_ok(void *ptr) · 19 unmodified lines | ||
| 73 | 74 | void *tomoyo_commit_ok(void *data, const unsigned int size) |
| 74 | 75 | { |
| 75 | 76 | void *ptr = kzalloc(size, GFP_NOFS); |
| 77 | + | |
| 76 | 78 | if (tomoyo_memory_ok(ptr)) { |
| 77 | 79 | memmove(ptr, data, size); |
| 78 | 80 | memset(data, 0, size); |
| @@ -98,6 +100,7 @@ struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, · 19 unmodified lines | ||
| 98 | 100 | struct list_head *list; |
| 99 | 101 | const char *group_name = tomoyo_read_token(param); |
| 100 | 102 | bool found = false; |
| 103 | + | |
| 101 | 104 | if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP) |
| 102 | 105 | return NULL; |
| 103 | 106 | e.group_name = tomoyo_get_name(group_name); |
| @@ -116,6 +119,7 @@ struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, · 12 unmodified lines | ||
| 116 | 119 | } |
| 117 | 120 | if (!found) { |
| 118 | 121 | struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e)); |
| 122 | + | |
| 119 | 123 | if (entry) { |
| 120 | 124 | INIT_LIST_HEAD(&entry->member_list); |
| 121 | 125 | atomic_set(&entry->head.users, 1); |
| @@ -191,6 +195,7 @@ struct tomoyo_policy_namespace tomoyo_kernel_namespace; · 69 unmodified lines | ||
| 191 | 195 | void __init tomoyo_mm_init(void) |
| 192 | 196 | { |
| 193 | 197 | int idx; |
| 198 | + | |
| 194 | 199 | for (idx = 0; idx < TOMOYO_MAX_HASH; idx++) |
| 195 | 200 | INIT_LIST_HEAD(&tomoyo_name_list[idx]); |
| 196 | 201 | tomoyo_kernel_namespace.name = "<kernel>"; |
| @@ -49,6 +49,7 @@ static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r, · 48 unmodified lines | ||
| 49 | 49 | { |
| 50 | 50 | const struct tomoyo_mount_acl *acl = |
| 51 | 51 | container_of(ptr, typeof(*acl), head); |
| 52 | + | |
| 52 | 53 | return tomoyo_compare_number_union(r->param.mount.flags, |
| 53 | 54 | &acl->flags) && |
| 54 | 55 | tomoyo_compare_name_union(r->param.mount.type, |
| @@ -89,6 +90,7 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r, · 34 unmodified lines | ||
| 89 | 90 | struct tomoyo_path_info rdir; |
| 90 | 91 | int need_dev = 0; |
| 91 | 92 | int error = -ENOMEM; |
| 93 | + | |
| 92 | 94 | r->obj = &obj; |
| 93 | 95 | |
| 94 | 96 | /* Get fstype. */ |
| @@ -94,11 +94,13 @@ static char *tomoyo_get_absolute_path(const struct path *path, char * const buff · 93 unmodified lines | ||
| 94 | 94 | const int buflen) |
| 95 | 95 | { |
| 96 | 96 | char *pos = ERR_PTR(-ENOMEM); |
| 97 | + | |
| 97 | 98 | if (buflen >= 256) { |
| 98 | 99 | /* go to whatever namespace root we are under */ |
| 99 | 100 | pos = d_absolute_path(path, buffer, buflen - 1); |
| 100 | 101 | if (!IS_ERR(pos) && *pos == '/' && pos[1]) { |
| 101 | 102 | struct inode *inode = d_backing_inode(path->dentry); |
| 103 | + | |
| 102 | 104 | if (inode && S_ISDIR(inode->i_mode)) { |
| 103 | 105 | buffer[buflen - 2] = '/'; |
| 104 | 106 | buffer[buflen - 1] = '\0'; |
| @@ -123,10 +125,12 @@ static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer, · 18 unmodified lines | ||
| 123 | 125 | const int buflen) |
| 124 | 126 | { |
| 125 | 127 | char *pos = ERR_PTR(-ENOMEM); |
| 128 | + | |
| 126 | 129 | if (buflen >= 256) { |
| 127 | 130 | pos = dentry_path_raw(dentry, buffer, buflen - 1); |
| 128 | 131 | if (!IS_ERR(pos) && *pos == '/' && pos[1]) { |
| 129 | 132 | struct inode *inode = d_backing_inode(dentry); |
| 133 | + | |
| 130 | 134 | if (inode && S_ISDIR(inode->i_mode)) { |
| 131 | 135 | buffer[buflen - 2] = '/'; |
| 132 | 136 | buffer[buflen - 1] = '\0'; |
| @@ -150,12 +154,14 @@ static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, · 17 unmodified lines | ||
| 150 | 154 | { |
| 151 | 155 | struct super_block *sb = dentry->d_sb; |
| 152 | 156 | char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen); |
| 157 | + | |
| 153 | 158 | if (IS_ERR(pos)) |
| 154 | 159 | return pos; |
| 155 | 160 | /* Convert from $PID to self if $PID is current thread. */ |
| 156 | 161 | if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') { |
| 157 | 162 | char *ep; |
| 158 | 163 | const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10); |
| 164 | + | |
| 159 | 165 | if (*ep == '/' && pid && pid == |
| 160 | 166 | task_tgid_nr_ns(current, sb->s_fs_info)) { |
| 161 | 167 | pos = ep - 5; |
| @@ -170,6 +176,7 @@ static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, · 8 unmodified lines | ||
| 170 | 176 | goto prepend_filesystem_name; |
| 171 | 177 | { |
| 172 | 178 | struct inode *inode = d_backing_inode(sb->s_root); |
| 179 | + | |
| 173 | 180 | /* |
| 174 | 181 | * Use filesystem name if filesystem does not support rename() |
| 175 | 182 | * operation. |
| @@ -182,6 +189,7 @@ static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, · 6 unmodified lines | ||
| 182 | 189 | char name[64]; |
| 183 | 190 | int name_len; |
| 184 | 191 | const dev_t dev = sb->s_dev; |
| 192 | + | |
| 185 | 193 | name[sizeof(name) - 1] = '\0'; |
| 186 | 194 | snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev), |
| 187 | 195 | MINOR(dev)); |
| @@ -197,6 +205,7 @@ prepend_filesystem_name: · 9 unmodified lines | ||
| 197 | 205 | { |
| 198 | 206 | const char *name = sb->s_type->name; |
| 199 | 207 | const int name_len = strlen(name); |
| 208 | + | |
| 200 | 209 | pos -= name_len + 1; |
| 201 | 210 | if (pos < buffer) |
| 202 | 211 | goto out; |
| @@ -223,10 +232,10 @@ static char *tomoyo_get_socket_name(const struct path *path, char * const buffer · 20 unmodified lines | ||
| 223 | 232 | struct inode *inode = d_backing_inode(path->dentry); |
| 224 | 233 | struct socket *sock = inode ? SOCKET_I(inode) : NULL; |
| 225 | 234 | struct sock *sk = sock ? sock->sk : NULL; |
| 235 | + | |
| 226 | 236 | if (sk) { |
| 227 | − snprintf(buffer, buflen, "socket:[family=%u:type=%u:" | |
| 228 | − "protocol=%u]", sk->sk_family, sk->sk_type, | |
| 229 | − sk->sk_protocol); | |
| 237 | + snprintf(buffer, buflen, "socket:[family=%u:type=%u:protocol=%u]", | |
| 238 | + sk->sk_family, sk->sk_type, sk->sk_protocol); | |
| 230 | 239 | } else { |
| 231 | 240 | snprintf(buffer, buflen, "socket:[unknown]"); |
| 232 | 241 | } |
| @@ -255,12 +264,14 @@ char *tomoyo_realpath_from_path(const struct path *path) · 22 unmodified lines | ||
| 255 | 264 | unsigned int buf_len = PAGE_SIZE / 2; |
| 256 | 265 | struct dentry *dentry = path->dentry; |
| 257 | 266 | struct super_block *sb; |
| 267 | + | |
| 258 | 268 | if (!dentry) |
| 259 | 269 | return NULL; |
| 260 | 270 | sb = dentry->d_sb; |
| 261 | 271 | while (1) { |
| 262 | 272 | char *pos; |
| 263 | 273 | struct inode *inode; |
| 274 | + | |
| 264 | 275 | buf_len <<= 1; |
| 265 | 276 | kfree(buf); |
| 266 | 277 | buf = kmalloc(buf_len, GFP_NOFS); |
| @@ -323,6 +334,7 @@ char *tomoyo_realpath_nofollow(const char *pathname) · 56 unmodified lines | ||
| 323 | 334 | |
| 324 | 335 | if (pathname && kern_path(pathname, 0, &path) == 0) { |
| 325 | 336 | char *buf = tomoyo_realpath_from_path(&path); |
| 337 | + | |
| 326 | 338 | path_put(&path); |
| 327 | 339 | return buf; |
| 328 | 340 | } |
| @@ -21,6 +21,7 @@ static bool tomoyo_check_task_acl(struct tomoyo_request_info *r, · 20 unmodified lines | ||
| 21 | 21 | { |
| 22 | 22 | const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl), |
| 23 | 23 | head); |
| 24 | + | |
| 24 | 25 | return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname); |
| 25 | 26 | } |
| 26 | 27 | |
| @@ -42,6 +43,7 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf, · 15 unmodified lines | ||
| 42 | 43 | { |
| 43 | 44 | char *data; |
| 44 | 45 | int error; |
| 46 | + | |
| 45 | 47 | if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10) |
| 46 | 48 | return -ENOMEM; |
| 47 | 49 | data = memdup_user_nul(buf, count); |
| @@ -52,6 +54,7 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf, · 4 unmodified lines | ||
| 52 | 54 | const int idx = tomoyo_read_lock(); |
| 53 | 55 | struct tomoyo_path_info name; |
| 54 | 56 | struct tomoyo_request_info r; |
| 57 | + | |
| 55 | 58 | name.name = data; |
| 56 | 59 | tomoyo_fill_path_info(&name); |
| 57 | 60 | /* Check "task manual_domain_transition" permission. */ |
| @@ -67,18 +70,14 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf, · 9 unmodified lines | ||
| 67 | 70 | if (!new_domain) { |
| 68 | 71 | error = -ENOENT; |
| 69 | 72 | } else { |
| 70 | − struct cred *cred = prepare_creds(); | |
| 71 | − if (!cred) { | |
| 72 | − error = -ENOMEM; | |
| 73 | − } else { | |
| 74 | − struct tomoyo_domain_info *old_domain = | |
| 75 | − cred->security; | |
| 76 | − cred->security = new_domain; | |
| 77 | − atomic_inc(&new_domain->users); | |
| 78 | − atomic_dec(&old_domain->users); | |
| 79 | − commit_creds(cred); | |
| 80 | − error = 0; | |
| 81 | − } | |
| 73 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 74 | + struct tomoyo_domain_info *old_domain = | |
| 75 | + s->domain_info; | |
| 76 | + | |
| 77 | + s->domain_info = new_domain; | |
| 78 | + atomic_inc(&new_domain->users); | |
| 79 | + atomic_dec(&old_domain->users); | |
| 80 | + error = 0; | |
| 82 | 81 | } |
| 83 | 82 | } |
| 84 | 83 | tomoyo_read_unlock(idx); |
| @@ -104,6 +103,7 @@ static ssize_t tomoyo_read_self(struct file *file, char __user *buf, · 19 unmodified lines | ||
| 104 | 103 | const char *domain = tomoyo_domain()->domainname->name; |
| 105 | 104 | loff_t len = strlen(domain); |
| 106 | 105 | loff_t pos = *ppos; |
| 106 | + | |
| 107 | 107 | if (pos >= len || !count) |
| 108 | 108 | return 0; |
| 109 | 109 | len -= pos; |
| @@ -234,10 +234,14 @@ static void __init tomoyo_create_entry(const char *name, const umode_t mode, · 124 unmodified lines | ||
| 234 | 234 | */ |
| 235 | 235 | static int __init tomoyo_initerface_init(void) |
| 236 | 236 | { |
| 237 | + struct tomoyo_domain_info *domain; | |
| 237 | 238 | struct dentry *tomoyo_dir; |
| 238 | 239 | |
| 240 | + if (!tomoyo_enabled) | |
| 241 | + return 0; | |
| 242 | + domain = tomoyo_domain(); | |
| 239 | 243 | /* Don't create securityfs entries unless registered. */ |
| 240 | − if (current_cred()->security != &tomoyo_kernel_domain) | |
| 244 | + if (domain != &tomoyo_kernel_domain) | |
| 241 | 245 | return 0; |
| 242 | 246 | |
| 243 | 247 | tomoyo_dir = securityfs_create_dir("tomoyo", NULL); |
| @@ -9,17 +9,19 @@ · 8 unmodified lines | ||
| 9 | 9 | #include "common.h" |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | − * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank(). | |
| 12 | + * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread. | |
| 13 | 13 | * |
| 14 | − * @new: Pointer to "struct cred". | |
| 15 | − * @gfp: Memory allocation flags. | |
| 16 | − * | |
| 17 | − * Returns 0. | |
| 14 | + * Returns pointer to "struct tomoyo_domain_info" for current thread. | |
| 18 | 15 | */ |
| 19 | −static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) | |
| 16 | +struct tomoyo_domain_info *tomoyo_domain(void) | |
| 20 | 17 | { |
| 21 | − new->security = NULL; | |
| 22 | − return 0; | |
| 18 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 19 | + | |
| 20 | + if (s->old_domain_info && !current->in_execve) { | |
| 21 | + atomic_dec(&s->old_domain_info->users); | |
| 22 | + s->old_domain_info = NULL; | |
| 23 | + } | |
| 24 | + return s->domain_info; | |
| 23 | 25 | } |
| 24 | 26 | |
| 25 | 27 | /** |
| @@ -34,42 +36,38 @@ static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) · 8 unmodified lines | ||
| 34 | 36 | static int tomoyo_cred_prepare(struct cred *new, const struct cred *old, |
| 35 | 37 | gfp_t gfp) |
| 36 | 38 | { |
| 37 | − struct tomoyo_domain_info *domain = old->security; | |
| 38 | − new->security = domain; | |
| 39 | − if (domain) | |
| 40 | − atomic_inc(&domain->users); | |
| 39 | + /* Restore old_domain_info saved by previous execve() request. */ | |
| 40 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 41 | + | |
| 42 | + if (s->old_domain_info && !current->in_execve) { | |
| 43 | + atomic_dec(&s->domain_info->users); | |
| 44 | + s->domain_info = s->old_domain_info; | |
| 45 | + s->old_domain_info = NULL; | |
| 46 | + } | |
| 41 | 47 | return 0; |
| 42 | 48 | } |
| 43 | 49 | |
| 44 | 50 | /** |
| 45 | − * tomoyo_cred_transfer - Target for security_transfer_creds(). | |
| 51 | + * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds(). | |
| 46 | 52 | * |
| 47 | − * @new: Pointer to "struct cred". | |
| 48 | − * @old: Pointer to "struct cred". | |
| 53 | + * @bprm: Pointer to "struct linux_binprm". | |
| 49 | 54 | */ |
| 50 | −static void tomoyo_cred_transfer(struct cred *new, const struct cred *old) | |
| 55 | +static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm) | |
| 51 | 56 | { |
| 52 | − tomoyo_cred_prepare(new, old, 0); | |
| 53 | −} | |
| 57 | + /* Clear old_domain_info saved by execve() request. */ | |
| 58 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 54 | 59 | |
| 55 | −/** | |
| 56 | − * tomoyo_cred_free - Target for security_cred_free(). | |
| 57 | − * | |
| 58 | − * @cred: Pointer to "struct cred". | |
| 59 | − */ | |
| 60 | −static void tomoyo_cred_free(struct cred *cred) | |
| 61 | −{ | |
| 62 | − struct tomoyo_domain_info *domain = cred->security; | |
| 63 | − if (domain) | |
| 64 | − atomic_dec(&domain->users); | |
| 60 | + atomic_dec(&s->old_domain_info->users); | |
| 61 | + s->old_domain_info = NULL; | |
| 65 | 62 | } |
| 66 | 63 | |
| 64 | +#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER | |
| 67 | 65 | /** |
| 68 | 66 | * tomoyo_bprm_set_creds - Target for security_bprm_set_creds(). |
| 69 | 67 | * |
| 70 | 68 | * @bprm: Pointer to "struct linux_binprm". |
| 71 | 69 | * |
| 72 | − * Returns 0 on success, negative value otherwise. | |
| 70 | + * Returns 0. | |
| 73 | 71 | */ |
| 74 | 72 | static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) |
| 75 | 73 | { |
| @@ -79,29 +77,15 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) · 3 unmodified lines | ||
| 79 | 77 | */ |
| 80 | 78 | if (bprm->called_set_creds) |
| 81 | 79 | return 0; |
| 82 | −#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER | |
| 83 | 80 | /* |
| 84 | 81 | * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested |
| 85 | 82 | * for the first time. |
| 86 | 83 | */ |
| 87 | 84 | if (!tomoyo_policy_loaded) |
| 88 | 85 | tomoyo_load_policy(bprm->filename); |
| 89 | −#endif | |
| 90 | − /* | |
| 91 | − * Release reference to "struct tomoyo_domain_info" stored inside | |
| 92 | − * "bprm->cred->security". New reference to "struct tomoyo_domain_info" | |
| 93 | − * stored inside "bprm->cred->security" will be acquired later inside | |
| 94 | − * tomoyo_find_next_domain(). | |
| 95 | − */ | |
| 96 | − atomic_dec(&((struct tomoyo_domain_info *) | |
| 97 | − bprm->cred->security)->users); | |
| 98 | − /* | |
| 99 | − * Tell tomoyo_bprm_check_security() is called for the first time of an | |
| 100 | − * execve operation. | |
| 101 | − */ | |
| 102 | − bprm->cred->security = NULL; | |
| 103 | 86 | return 0; |
| 104 | 87 | } |
| 88 | +#endif | |
| 105 | 89 | |
| 106 | 90 | /** |
| 107 | 91 | * tomoyo_bprm_check_security - Target for security_bprm_check(). |
| @@ -112,23 +96,24 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) · 4 unmodified lines | ||
| 112 | 96 | */ |
| 113 | 97 | static int tomoyo_bprm_check_security(struct linux_binprm *bprm) |
| 114 | 98 | { |
| 115 | − struct tomoyo_domain_info *domain = bprm->cred->security; | |
| 99 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 116 | 100 | |
| 117 | 101 | /* |
| 118 | 102 | * Execute permission is checked against pathname passed to do_execve() |
| 119 | 103 | * using current domain. |
| 120 | 104 | */ |
| 121 | − if (!domain) { | |
| 105 | + if (!s->old_domain_info) { | |
| 122 | 106 | const int idx = tomoyo_read_lock(); |
| 123 | 107 | const int err = tomoyo_find_next_domain(bprm); |
| 108 | + | |
| 124 | 109 | tomoyo_read_unlock(idx); |
| 125 | 110 | return err; |
| 126 | 111 | } |
| 127 | 112 | /* |
| 128 | 113 | * Read permission is checked against interpreters using next domain. |
| 129 | 114 | */ |
| 130 | − return tomoyo_check_open_permission(domain, &bprm->file->f_path, | |
| 131 | − O_RDONLY); | |
| 115 | + return tomoyo_check_open_permission(s->domain_info, | |
| 116 | + &bprm->file->f_path, O_RDONLY); | |
| 132 | 117 | } |
| 133 | 118 | |
| 134 | 119 | /** |
| @@ -167,6 +152,7 @@ static int tomoyo_path_truncate(const struct path *path) · 32 unmodified lines | ||
| 167 | 152 | static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry) |
| 168 | 153 | { |
| 169 | 154 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
| 155 | + | |
| 170 | 156 | return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL); |
| 171 | 157 | } |
| 172 | 158 | |
| @@ -183,6 +169,7 @@ static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry, · 10 unmodified lines | ||
| 183 | 169 | umode_t mode) |
| 184 | 170 | { |
| 185 | 171 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
| 172 | + | |
| 186 | 173 | return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path, |
| 187 | 174 | mode & S_IALLUGO); |
| 188 | 175 | } |
| @@ -198,6 +185,7 @@ static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry, · 9 unmodified lines | ||
| 198 | 185 | static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry) |
| 199 | 186 | { |
| 200 | 187 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
| 188 | + | |
| 201 | 189 | return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL); |
| 202 | 190 | } |
| 203 | 191 | |
| @@ -214,6 +202,7 @@ static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry, · 10 unmodified lines | ||
| 214 | 202 | const char *old_name) |
| 215 | 203 | { |
| 216 | 204 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
| 205 | + | |
| 217 | 206 | return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name); |
| 218 | 207 | } |
| 219 | 208 | |
| @@ -271,6 +260,7 @@ static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_di · 51 unmodified lines | ||
| 271 | 260 | { |
| 272 | 261 | struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry }; |
| 273 | 262 | struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry }; |
| 263 | + | |
| 274 | 264 | return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2); |
| 275 | 265 | } |
| 276 | 266 | |
| @@ -291,6 +281,7 @@ static int tomoyo_path_rename(const struct path *old_parent, · 14 unmodified lines | ||
| 291 | 281 | { |
| 292 | 282 | struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry }; |
| 293 | 283 | struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; |
| 284 | + | |
| 294 | 285 | return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2); |
| 295 | 286 | } |
| 296 | 287 | |
| @@ -322,11 +313,11 @@ static int tomoyo_file_fcntl(struct file *file, unsigned int cmd, · 25 unmodified lines | ||
| 322 | 313 | */ |
| 323 | 314 | static int tomoyo_file_open(struct file *f) |
| 324 | 315 | { |
| 325 | − int flags = f->f_flags; | |
| 326 | 316 | /* Don't check read permission here if called from do_execve(). */ |
| 327 | 317 | if (current->in_execve) |
| 328 | 318 | return 0; |
| 329 | − return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags); | |
| 319 | + return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, | |
| 320 | + f->f_flags); | |
| 330 | 321 | } |
| 331 | 322 | |
| 332 | 323 | /** |
| @@ -370,6 +361,7 @@ static int tomoyo_path_chmod(const struct path *path, umode_t mode) · 37 unmodified lines | ||
| 370 | 361 | static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
| 371 | 362 | { |
| 372 | 363 | int error = 0; |
| 364 | + | |
| 373 | 365 | if (uid_valid(uid)) |
| 374 | 366 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, |
| 375 | 367 | from_kuid(&init_user_ns, uid)); |
| @@ -419,6 +411,7 @@ static int tomoyo_sb_mount(const char *dev_name, const struct path *path, · 43 unmodified lines | ||
| 419 | 411 | static int tomoyo_sb_umount(struct vfsmount *mnt, int flags) |
| 420 | 412 | { |
| 421 | 413 | struct path path = { .mnt = mnt, .dentry = mnt->mnt_root }; |
| 414 | + | |
| 422 | 415 | return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL); |
| 423 | 416 | } |
| 424 | 417 | |
| @@ -493,16 +486,61 @@ static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg, · 68 unmodified lines | ||
| 493 | 486 | return tomoyo_socket_sendmsg_permission(sock, msg, size); |
| 494 | 487 | } |
| 495 | 488 | |
| 489 | +struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = { | |
| 490 | + .lbs_task = sizeof(struct tomoyo_task), | |
| 491 | +}; | |
| 492 | + | |
| 493 | +/** | |
| 494 | + * tomoyo_task_alloc - Target for security_task_alloc(). | |
| 495 | + * | |
| 496 | + * @task: Pointer to "struct task_struct". | |
| 497 | + * @flags: clone() flags. | |
| 498 | + * | |
| 499 | + * Returns 0. | |
| 500 | + */ | |
| 501 | +static int tomoyo_task_alloc(struct task_struct *task, | |
| 502 | + unsigned long clone_flags) | |
| 503 | +{ | |
| 504 | + struct tomoyo_task *old = tomoyo_task(current); | |
| 505 | + struct tomoyo_task *new = tomoyo_task(task); | |
| 506 | + | |
| 507 | + new->domain_info = old->domain_info; | |
| 508 | + atomic_inc(&new->domain_info->users); | |
| 509 | + new->old_domain_info = NULL; | |
| 510 | + return 0; | |
| 511 | +} | |
| 512 | + | |
| 513 | +/** | |
| 514 | + * tomoyo_task_free - Target for security_task_free(). | |
| 515 | + * | |
| 516 | + * @task: Pointer to "struct task_struct". | |
| 517 | + */ | |
| 518 | +static void tomoyo_task_free(struct task_struct *task) | |
| 519 | +{ | |
| 520 | + struct tomoyo_task *s = tomoyo_task(task); | |
| 521 | + | |
| 522 | + if (s->domain_info) { | |
| 523 | + atomic_dec(&s->domain_info->users); | |
| 524 | + s->domain_info = NULL; | |
| 525 | + } | |
| 526 | + if (s->old_domain_info) { | |
| 527 | + atomic_dec(&s->old_domain_info->users); | |
| 528 | + s->old_domain_info = NULL; | |
| 529 | + } | |
| 530 | +} | |
| 531 | + | |
| 496 | 532 | /* |
| 497 | 533 | * tomoyo_security_ops is a "struct security_operations" which is used for |
| 498 | 534 | * registering TOMOYO. |
| 499 | 535 | */ |
| 500 | 536 | static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = { |
| 501 | − LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank), | |
| 502 | 537 | LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare), |
| 503 | − LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer), | |
| 504 | − LSM_HOOK_INIT(cred_free, tomoyo_cred_free), | |
| 538 | + LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds), | |
| 539 | + LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc), | |
| 540 | + LSM_HOOK_INIT(task_free, tomoyo_task_free), | |
| 541 | +#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER | |
| 505 | 542 | LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds), |
| 543 | +#endif | |
| 506 | 544 | LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security), |
| 507 | 545 | LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl), |
| 508 | 546 | LSM_HOOK_INIT(file_open, tomoyo_file_open), |
| @@ -531,6 +569,8 @@ static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = { · 22 unmodified lines | ||
| 531 | 569 | /* Lock for GC. */ |
| 532 | 570 | DEFINE_SRCU(tomoyo_ss); |
| 533 | 571 | |
| 572 | +int tomoyo_enabled __lsm_ro_after_init = 1; | |
| 573 | + | |
| 534 | 574 | /** |
| 535 | 575 | * tomoyo_init - Register TOMOYO Linux as a LSM module. |
| 536 | 576 | * |
| @@ -538,19 +578,23 @@ DEFINE_SRCU(tomoyo_ss); · 1 unmodified line | ||
| 538 | 578 | */ |
| 539 | 579 | static int __init tomoyo_init(void) |
| 540 | 580 | { |
| 541 | − struct cred *cred = (struct cred *) current_cred(); | |
| 581 | + struct tomoyo_task *s = tomoyo_task(current); | |
| 542 | 582 | |
| 543 | − if (!security_module_enable("tomoyo")) | |
| 544 | − return 0; | |
| 545 | 583 | /* register ourselves with the security framework */ |
| 546 | 584 | security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo"); |
| 547 | − printk(KERN_INFO "TOMOYO Linux initialized\n"); | |
| 548 | − cred->security = &tomoyo_kernel_domain; | |
| 585 | + pr_info("TOMOYO Linux initialized\n"); | |
| 586 | + s->domain_info = &tomoyo_kernel_domain; | |
| 587 | + atomic_inc(&tomoyo_kernel_domain.users); | |
| 588 | + s->old_domain_info = NULL; | |
| 549 | 589 | tomoyo_mm_init(); |
| 590 | + | |
| 550 | 591 | return 0; |
| 551 | 592 | } |
| 552 | 593 | |
| 553 | 594 | DEFINE_LSM(tomoyo) = { |
| 554 | 595 | .name = "tomoyo", |
| 596 | + .enabled = &tomoyo_enabled, | |
| 597 | + .flags = LSM_FLAG_LEGACY_MAJOR, | |
| 598 | + .blobs = &tomoyo_blob_sizes, | |
| 555 | 599 | .init = tomoyo_init, |
| 556 | 600 | }; |
| @@ -91,6 +91,7 @@ const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = { · 90 unmodified lines | ||
| 91 | 91 | void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp) |
| 92 | 92 | { |
| 93 | 93 | struct tm tm; |
| 94 | + | |
| 94 | 95 | time64_to_tm(time64, 0, &tm); |
| 95 | 96 | stamp->sec = tm.tm_sec; |
| 96 | 97 | stamp->min = tm.tm_min; |
| @@ -113,6 +114,7 @@ void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp) · 16 unmodified lines | ||
| 113 | 114 | bool tomoyo_permstr(const char *string, const char *keyword) |
| 114 | 115 | { |
| 115 | 116 | const char *cp = strstr(string, keyword); |
| 117 | + | |
| 116 | 118 | if (cp) |
| 117 | 119 | return cp == string || *(cp - 1) == '/'; |
| 118 | 120 | return false; |
| @@ -132,6 +134,7 @@ char *tomoyo_read_token(struct tomoyo_acl_param *param) · 13 unmodified lines | ||
| 132 | 134 | { |
| 133 | 135 | char *pos = param->data; |
| 134 | 136 | char *del = strchr(pos, ' '); |
| 137 | + | |
| 135 | 138 | if (del) |
| 136 | 139 | *del++ = '\0'; |
| 137 | 140 | else |
| @@ -152,6 +155,7 @@ const struct tomoyo_path_info *tomoyo_get_domainname · 14 unmodified lines | ||
| 152 | 155 | { |
| 153 | 156 | char *start = param->data; |
| 154 | 157 | char *pos = start; |
| 158 | + | |
| 155 | 159 | while (*pos) { |
| 156 | 160 | if (*pos++ != ' ' || *pos++ == '/') |
| 157 | 161 | continue; |
| @@ -181,8 +185,10 @@ u8 tomoyo_parse_ulong(unsigned long *result, char **str) · 23 unmodified lines | ||
| 181 | 185 | const char *cp = *str; |
| 182 | 186 | char *ep; |
| 183 | 187 | int base = 10; |
| 188 | + | |
| 184 | 189 | if (*cp == '0') { |
| 185 | 190 | char c = *(cp + 1); |
| 191 | + | |
| 186 | 192 | if (c == 'x' || c == 'X') { |
| 187 | 193 | base = 16; |
| 188 | 194 | cp += 2; |
| @@ -240,6 +246,7 @@ bool tomoyo_parse_name_union(struct tomoyo_acl_param *param, · 51 unmodified lines | ||
| 240 | 246 | struct tomoyo_name_union *ptr) |
| 241 | 247 | { |
| 242 | 248 | char *filename; |
| 249 | + | |
| 243 | 250 | if (param->data[0] == '@') { |
| 244 | 251 | param->data++; |
| 245 | 252 | ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP); |
| @@ -266,6 +273,7 @@ bool tomoyo_parse_number_union(struct tomoyo_acl_param *param, · 20 unmodified lines | ||
| 266 | 273 | char *data; |
| 267 | 274 | u8 type; |
| 268 | 275 | unsigned long v; |
| 276 | + | |
| 269 | 277 | memset(ptr, 0, sizeof(*ptr)); |
| 270 | 278 | if (param->data[0] == '@') { |
| 271 | 279 | param->data++; |
| @@ -429,6 +437,7 @@ static bool tomoyo_correct_word2(const char *string, size_t len) · 157 unmodified lines | ||
| 429 | 437 | unsigned char c; |
| 430 | 438 | unsigned char d; |
| 431 | 439 | unsigned char e; |
| 440 | + | |
| 432 | 441 | if (!len) |
| 433 | 442 | goto out; |
| 434 | 443 | while (len--) { |
| @@ -533,6 +542,7 @@ bool tomoyo_correct_domain(const unsigned char *domainname) · 98 unmodified lines | ||
| 533 | 542 | return true; |
| 534 | 543 | while (1) { |
| 535 | 544 | const unsigned char *cp = strchr(domainname, ' '); |
| 545 | + | |
| 536 | 546 | if (!cp) |
| 537 | 547 | break; |
| 538 | 548 | if (*domainname != '/' || |
| @@ -554,6 +564,7 @@ bool tomoyo_domain_def(const unsigned char *buffer) · 15 unmodified lines | ||
| 554 | 564 | { |
| 555 | 565 | const unsigned char *cp; |
| 556 | 566 | int len; |
| 567 | + | |
| 557 | 568 | if (*buffer != '<') |
| 558 | 569 | return false; |
| 559 | 570 | cp = strchr(buffer, ' '); |
| @@ -668,6 +679,9 @@ static bool tomoyo_file_matches_pattern2(const char *filename, · 108 unmodified lines | ||
| 668 | 679 | { |
| 669 | 680 | while (filename < filename_end && pattern < pattern_end) { |
| 670 | 681 | char c; |
| 682 | + int i; | |
| 683 | + int j; | |
| 684 | + | |
| 671 | 685 | if (*pattern != '\\') { |
| 672 | 686 | if (*filename++ != *pattern++) |
| 673 | 687 | return false; |
| @@ -676,8 +690,6 @@ static bool tomoyo_file_matches_pattern2(const char *filename, · 2 unmodified lines | ||
| 676 | 690 | c = *filename; |
| 677 | 691 | pattern++; |
| 678 | 692 | switch (*pattern) { |
| 679 | − int i; | |
| 680 | − int j; | |
| 681 | 693 | case '?': |
| 682 | 694 | if (c == '/') { |
| 683 | 695 | return false; |
| @@ -985,6 +997,7 @@ int tomoyo_init_request_info(struct tomoyo_request_info *r, · 301 unmodified lines | ||
| 985 | 997 | struct tomoyo_domain_info *domain, const u8 index) |
| 986 | 998 | { |
| 987 | 999 | u8 profile; |
| 1000 | + | |
| 988 | 1001 | memset(r, 0, sizeof(*r)); |
| 989 | 1002 | if (!domain) |
| 990 | 1003 | domain = tomoyo_domain(); |
| @@ -1018,6 +1031,7 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r) · 27 unmodified lines | ||
| 1018 | 1031 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
| 1019 | 1032 | u16 perm; |
| 1020 | 1033 | u8 i; |
| 1034 | + | |
| 1021 | 1035 | if (ptr->is_deleted) |
| 1022 | 1036 | continue; |
| 1023 | 1037 | switch (ptr->type) { |
| @@ -1062,9 +1076,8 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r) · 38 unmodified lines | ||
| 1062 | 1076 | domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true; |
| 1063 | 1077 | /* r->granted = false; */ |
| 1064 | 1078 | tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]); |
| 1065 | − printk(KERN_WARNING "WARNING: " | |
| 1066 | − "Domain '%s' has too many ACLs to hold. " | |
| 1067 | − "Stopped learning mode.\n", domain->domainname->name); | |
| 1079 | + pr_warn("WARNING: Domain '%s' has too many ACLs to hold. Stopped learning mode.\n", | |
| 1080 | + domain->domainname->name); | |
| 1068 | 1081 | } |
| 1069 | 1082 | return false; |
| 1070 | 1083 | } |
| @@ -479,9 +479,15 @@ static void __init yama_init_sysctl(void) · 478 unmodified lines | ||
| 479 | 479 | static inline void yama_init_sysctl(void) { } |
| 480 | 480 | #endif /* CONFIG_SYSCTL */ |
| 481 | 481 | |
| 482 | −void __init yama_add_hooks(void) | |
| 482 | +static int __init yama_init(void) | |
| 483 | 483 | { |
| 484 | 484 | pr_info("Yama: becoming mindful.\n"); |
| 485 | 485 | security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama"); |
| 486 | 486 | yama_init_sysctl(); |
| 487 | + return 0; | |
| 487 | 488 | } |
| 489 | + | |
| 490 | +DEFINE_LSM(yama) = { | |
| 491 | + .name = "yama", | |
| 492 | + .init = yama_init, | |
| 493 | +}; | |
| @@ -0,0 +1 @@ | ||
| 1 | +safesetid-test | |
| @@ -0,0 +1,8 @@ | ||
| 1 | +# SPDX-License-Identifier: GPL-2.0 | |
| 2 | +# Makefile for mount selftests. | |
| 3 | +CFLAGS = -Wall -lcap -O2 | |
| 4 | + | |
| 5 | +TEST_PROGS := run_tests.sh | |
| 6 | +TEST_GEN_FILES := safesetid-test | |
| 7 | + | |
| 8 | +include ../lib.mk | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +CONFIG_SECURITY=y | |
| 2 | +CONFIG_SECURITYFS=y | |
| @@ -0,0 +1,334 @@ | ||
| 1 | +// SPDX-License-Identifier: GPL-2.0 | |
| 2 | +#define _GNU_SOURCE | |
| 3 | +#include <stdio.h> | |
| 4 | +#include <errno.h> | |
| 5 | +#include <pwd.h> | |
| 6 | +#include <string.h> | |
| 7 | +#include <syscall.h> | |
| 8 | +#include <sys/capability.h> | |
| 9 | +#include <sys/types.h> | |
| 10 | +#include <sys/mount.h> | |
| 11 | +#include <sys/prctl.h> | |
| 12 | +#include <sys/wait.h> | |
| 13 | +#include <stdlib.h> | |
| 14 | +#include <unistd.h> | |
| 15 | +#include <fcntl.h> | |
| 16 | +#include <stdbool.h> | |
| 17 | +#include <stdarg.h> | |
| 18 | + | |
| 19 | +#ifndef CLONE_NEWUSER | |
| 20 | +# define CLONE_NEWUSER 0x10000000 | |
| 21 | +#endif | |
| 22 | + | |
| 23 | +#define ROOT_USER 0 | |
| 24 | +#define RESTRICTED_PARENT 1 | |
| 25 | +#define ALLOWED_CHILD1 2 | |
| 26 | +#define ALLOWED_CHILD2 3 | |
| 27 | +#define NO_POLICY_USER 4 | |
| 28 | + | |
| 29 | +char* add_whitelist_policy_file = "/sys/kernel/security/safesetid/add_whitelist_policy"; | |
| 30 | + | |
| 31 | +static void die(char *fmt, ...) | |
| 32 | +{ | |
| 33 | + va_list ap; | |
| 34 | + va_start(ap, fmt); | |
| 35 | + vfprintf(stderr, fmt, ap); | |
| 36 | + va_end(ap); | |
| 37 | + exit(EXIT_FAILURE); | |
| 38 | +} | |
| 39 | + | |
| 40 | +static bool vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap) | |
| 41 | +{ | |
| 42 | + char buf[4096]; | |
| 43 | + int fd; | |
| 44 | + ssize_t written; | |
| 45 | + int buf_len; | |
| 46 | + | |
| 47 | + buf_len = vsnprintf(buf, sizeof(buf), fmt, ap); | |
| 48 | + if (buf_len < 0) { | |
| 49 | + printf("vsnprintf failed: %s\n", | |
| 50 | + strerror(errno)); | |
| 51 | + return false; | |
| 52 | + } | |
| 53 | + if (buf_len >= sizeof(buf)) { | |
| 54 | + printf("vsnprintf output truncated\n"); | |
| 55 | + return false; | |
| 56 | + } | |
| 57 | + | |
| 58 | + fd = open(filename, O_WRONLY); | |
| 59 | + if (fd < 0) { | |
| 60 | + if ((errno == ENOENT) && enoent_ok) | |
| 61 | + return true; | |
| 62 | + return false; | |
| 63 | + } | |
| 64 | + written = write(fd, buf, buf_len); | |
| 65 | + if (written != buf_len) { | |
| 66 | + if (written >= 0) { | |
| 67 | + printf("short write to %s\n", filename); | |
| 68 | + return false; | |
| 69 | + } else { | |
| 70 | + printf("write to %s failed: %s\n", | |
| 71 | + filename, strerror(errno)); | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + if (close(fd) != 0) { | |
| 76 | + printf("close of %s failed: %s\n", | |
| 77 | + filename, strerror(errno)); | |
| 78 | + return false; | |
| 79 | + } | |
| 80 | + return true; | |
| 81 | +} | |
| 82 | + | |
| 83 | +static bool write_file(char *filename, char *fmt, ...) | |
| 84 | +{ | |
| 85 | + va_list ap; | |
| 86 | + bool ret; | |
| 87 | + | |
| 88 | + va_start(ap, fmt); | |
| 89 | + ret = vmaybe_write_file(false, filename, fmt, ap); | |
| 90 | + va_end(ap); | |
| 91 | + | |
| 92 | + return ret; | |
| 93 | +} | |
| 94 | + | |
| 95 | +static void ensure_user_exists(uid_t uid) | |
| 96 | +{ | |
| 97 | + struct passwd p; | |
| 98 | + | |
| 99 | + FILE *fd; | |
| 100 | + char name_str[10]; | |
| 101 | + | |
| 102 | + if (getpwuid(uid) == NULL) { | |
| 103 | + memset(&p,0x00,sizeof(p)); | |
| 104 | + fd=fopen("/etc/passwd","a"); | |
| 105 | + if (fd == NULL) | |
| 106 | + die("couldn't open file\n"); | |
| 107 | + if (fseek(fd, 0, SEEK_END)) | |
| 108 | + die("couldn't fseek\n"); | |
| 109 | + snprintf(name_str, 10, "%d", uid); | |
| 110 | + p.pw_name=name_str; | |
| 111 | + p.pw_uid=uid; | |
| 112 | + p.pw_gecos="Test account"; | |
| 113 | + p.pw_dir="/dev/null"; | |
| 114 | + p.pw_shell="/bin/false"; | |
| 115 | + int value = putpwent(&p,fd); | |
| 116 | + if (value != 0) | |
| 117 | + die("putpwent failed\n"); | |
| 118 | + if (fclose(fd)) | |
| 119 | + die("fclose failed\n"); | |
| 120 | + } | |
| 121 | +} | |
| 122 | + | |
| 123 | +static void ensure_securityfs_mounted(void) | |
| 124 | +{ | |
| 125 | + int fd = open(add_whitelist_policy_file, O_WRONLY); | |
| 126 | + if (fd < 0) { | |
| 127 | + if (errno == ENOENT) { | |
| 128 | + // Need to mount securityfs | |
| 129 | + if (mount("securityfs", "/sys/kernel/security", | |
| 130 | + "securityfs", 0, NULL) < 0) | |
| 131 | + die("mounting securityfs failed\n"); | |
| 132 | + } else { | |
| 133 | + die("couldn't find securityfs for unknown reason\n"); | |
| 134 | + } | |
| 135 | + } else { | |
| 136 | + if (close(fd) != 0) { | |
| 137 | + die("close of %s failed: %s\n", | |
| 138 | + add_whitelist_policy_file, strerror(errno)); | |
| 139 | + } | |
| 140 | + } | |
| 141 | +} | |
| 142 | + | |
| 143 | +static void write_policies(void) | |
| 144 | +{ | |
| 145 | + ssize_t written; | |
| 146 | + int fd; | |
| 147 | + | |
| 148 | + fd = open(add_whitelist_policy_file, O_WRONLY); | |
| 149 | + if (fd < 0) | |
| 150 | + die("cant open add_whitelist_policy file\n"); | |
| 151 | + written = write(fd, "1:2", strlen("1:2")); | |
| 152 | + if (written != strlen("1:2")) { | |
| 153 | + if (written >= 0) { | |
| 154 | + die("short write to %s\n", add_whitelist_policy_file); | |
| 155 | + } else { | |
| 156 | + die("write to %s failed: %s\n", | |
| 157 | + add_whitelist_policy_file, strerror(errno)); | |
| 158 | + } | |
| 159 | + } | |
| 160 | + written = write(fd, "1:3", strlen("1:3")); | |
| 161 | + if (written != strlen("1:3")) { | |
| 162 | + if (written >= 0) { | |
| 163 | + die("short write to %s\n", add_whitelist_policy_file); | |
| 164 | + } else { | |
| 165 | + die("write to %s failed: %s\n", | |
| 166 | + add_whitelist_policy_file, strerror(errno)); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + if (close(fd) != 0) { | |
| 170 | + die("close of %s failed: %s\n", | |
| 171 | + add_whitelist_policy_file, strerror(errno)); | |
| 172 | + } | |
| 173 | +} | |
| 174 | + | |
| 175 | +static bool test_userns(bool expect_success) | |
| 176 | +{ | |
| 177 | + uid_t uid; | |
| 178 | + char map_file_name[32]; | |
| 179 | + size_t sz = sizeof(map_file_name); | |
| 180 | + pid_t cpid; | |
| 181 | + bool success; | |
| 182 | + | |
| 183 | + uid = getuid(); | |
| 184 | + | |
| 185 | + int clone_flags = CLONE_NEWUSER; | |
| 186 | + cpid = syscall(SYS_clone, clone_flags, NULL); | |
| 187 | + if (cpid == -1) { | |
| 188 | + printf("clone failed"); | |
| 189 | + return false; | |
| 190 | + } | |
| 191 | + | |
| 192 | + if (cpid == 0) { /* Code executed by child */ | |
| 193 | + // Give parent 1 second to write map file | |
| 194 | + sleep(1); | |
| 195 | + exit(EXIT_SUCCESS); | |
| 196 | + } else { /* Code executed by parent */ | |
| 197 | + if(snprintf(map_file_name, sz, "/proc/%d/uid_map", cpid) < 0) { | |
| 198 | + printf("preparing file name string failed"); | |
| 199 | + return false; | |
| 200 | + } | |
| 201 | + success = write_file(map_file_name, "0 0 1", uid); | |
| 202 | + return success == expect_success; | |
| 203 | + } | |
| 204 | + | |
| 205 | + printf("should not reach here"); | |
| 206 | + return false; | |
| 207 | +} | |
| 208 | + | |
| 209 | +static void test_setuid(uid_t child_uid, bool expect_success) | |
| 210 | +{ | |
| 211 | + pid_t cpid, w; | |
| 212 | + int wstatus; | |
| 213 | + | |
| 214 | + cpid = fork(); | |
| 215 | + if (cpid == -1) { | |
| 216 | + die("fork\n"); | |
| 217 | + } | |
| 218 | + | |
| 219 | + if (cpid == 0) { /* Code executed by child */ | |
| 220 | + setuid(child_uid); | |
| 221 | + if (getuid() == child_uid) | |
| 222 | + exit(EXIT_SUCCESS); | |
| 223 | + else | |
| 224 | + exit(EXIT_FAILURE); | |
| 225 | + } else { /* Code executed by parent */ | |
| 226 | + do { | |
| 227 | + w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED); | |
| 228 | + if (w == -1) { | |
| 229 | + die("waitpid\n"); | |
| 230 | + } | |
| 231 | + | |
| 232 | + if (WIFEXITED(wstatus)) { | |
| 233 | + if (WEXITSTATUS(wstatus) == EXIT_SUCCESS) { | |
| 234 | + if (expect_success) { | |
| 235 | + return; | |
| 236 | + } else { | |
| 237 | + die("unexpected success\n"); | |
| 238 | + } | |
| 239 | + } else { | |
| 240 | + if (expect_success) { | |
| 241 | + die("unexpected failure\n"); | |
| 242 | + } else { | |
| 243 | + return; | |
| 244 | + } | |
| 245 | + } | |
| 246 | + } else if (WIFSIGNALED(wstatus)) { | |
| 247 | + if (WTERMSIG(wstatus) == 9) { | |
| 248 | + if (expect_success) | |
| 249 | + die("killed unexpectedly\n"); | |
| 250 | + else | |
| 251 | + return; | |
| 252 | + } else { | |
| 253 | + die("unexpected signal: %d\n", wstatus); | |
| 254 | + } | |
| 255 | + } else { | |
| 256 | + die("unexpected status: %d\n", wstatus); | |
| 257 | + } | |
| 258 | + } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); | |
| 259 | + } | |
| 260 | + | |
| 261 | + die("should not reach here\n"); | |
| 262 | +} | |
| 263 | + | |
| 264 | +static void ensure_users_exist(void) | |
| 265 | +{ | |
| 266 | + ensure_user_exists(ROOT_USER); | |
| 267 | + ensure_user_exists(RESTRICTED_PARENT); | |
| 268 | + ensure_user_exists(ALLOWED_CHILD1); | |
| 269 | + ensure_user_exists(ALLOWED_CHILD2); | |
| 270 | + ensure_user_exists(NO_POLICY_USER); | |
| 271 | +} | |
| 272 | + | |
| 273 | +static void drop_caps(bool setid_retained) | |
| 274 | +{ | |
| 275 | + cap_value_t cap_values[] = {CAP_SETUID, CAP_SETGID}; | |
| 276 | + cap_t caps; | |
| 277 | + | |
| 278 | + caps = cap_get_proc(); | |
| 279 | + if (setid_retained) | |
| 280 | + cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET); | |
| 281 | + else | |
| 282 | + cap_clear(caps); | |
| 283 | + cap_set_proc(caps); | |
| 284 | + cap_free(caps); | |
| 285 | +} | |
| 286 | + | |
| 287 | +int main(int argc, char **argv) | |
| 288 | +{ | |
| 289 | + ensure_users_exist(); | |
| 290 | + ensure_securityfs_mounted(); | |
| 291 | + write_policies(); | |
| 292 | + | |
| 293 | + if (prctl(PR_SET_KEEPCAPS, 1L)) | |
| 294 | + die("Error with set keepcaps\n"); | |
| 295 | + | |
| 296 | + // First test to make sure we can write userns mappings from a user | |
| 297 | + // that doesn't have any restrictions (as long as it has CAP_SETUID); | |
| 298 | + setuid(NO_POLICY_USER); | |
| 299 | + setgid(NO_POLICY_USER); | |
| 300 | + | |
| 301 | + // Take away all but setid caps | |
| 302 | + drop_caps(true); | |
| 303 | + | |
| 304 | + // Need PR_SET_DUMPABLE flag set so we can write /proc/[pid]/uid_map | |
| 305 | + // from non-root parent process. | |
| 306 | + if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) | |
| 307 | + die("Error with set dumpable\n"); | |
| 308 | + | |
| 309 | + if (!test_userns(true)) { | |
| 310 | + die("test_userns failed when it should work\n"); | |
| 311 | + } | |
| 312 | + | |
| 313 | + setuid(RESTRICTED_PARENT); | |
| 314 | + setgid(RESTRICTED_PARENT); | |
| 315 | + | |
| 316 | + test_setuid(ROOT_USER, false); | |
| 317 | + test_setuid(ALLOWED_CHILD1, true); | |
| 318 | + test_setuid(ALLOWED_CHILD2, true); | |
| 319 | + test_setuid(NO_POLICY_USER, false); | |
| 320 | + | |
| 321 | + if (!test_userns(false)) { | |
| 322 | + die("test_userns worked when it should fail\n"); | |
| 323 | + } | |
| 324 | + | |
| 325 | + // Now take away all caps | |
| 326 | + drop_caps(false); | |
| 327 | + test_setuid(2, false); | |
| 328 | + test_setuid(3, false); | |
| 329 | + test_setuid(4, false); | |
| 330 | + | |
| 331 | + // NOTE: this test doesn't clean up users that were created in | |
| 332 | + // /etc/passwd or flush policies that were added to the LSM. | |
| 333 | + return EXIT_SUCCESS; | |
| 334 | +} | |
| @@ -0,0 +1,26 @@ | ||
| 1 | +#!/bin/bash | |
| 2 | + | |
| 3 | +TCID="safesetid-test.sh" | |
| 4 | +errcode=0 | |
| 5 | + | |
| 6 | +# Kselftest framework requirement - SKIP code is 4. | |
| 7 | +ksft_skip=4 | |
| 8 | + | |
| 9 | +check_root() | |
| 10 | +{ | |
| 11 | + uid=$(id -u) | |
| 12 | + if [ $uid -ne 0 ]; then | |
| 13 | + echo $TCID: must be run as root >&2 | |
| 14 | + exit $ksft_skip | |
| 15 | + fi | |
| 16 | +} | |
| 17 | + | |
| 18 | +main_function() | |
| 19 | +{ | |
| 20 | + check_root | |
| 21 | + ./safesetid-test | |
| 22 | +} | |
| 23 | + | |
| 24 | +main_function | |
| 25 | +echo "$TCID: done" | |
| 26 | +exit $errcode | |