History

Merge branch 'next-general' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security

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 ...

Linus Torvalds committed 7 years ago

Commit ae5906c2 parents 1fc1cd8 468e91c78 files · +2674 −1090Browse files

Unified · Split

78 files
  1. Documentation/admin-guide/LSM/SafeSetID.rst +107 −0
  2. Documentation/admin-guide/LSM/index.rst +11 −3
  3. Documentation/admin-guide/kernel-parameters.txt +7 −5
  4. MAINTAINERS +5 −6
  5. fs/proc/base.c +55 −9
  6. fs/proc/internal.h +1 −0
  7. include/linux/capability.h +5 −0
  8. include/linux/cred.h +0 −1
  9. include/linux/lsm_hooks.h +30 −15
  10. include/linux/security.h +24 −19
  11. include/linux/selinux.h +0 −35
  12. kernel/capability.c +33 −12
  13. kernel/cred.c +0 −13
  14. kernel/seccomp.c +2 −2
  15. kernel/sys.c +5 −5
  16. security/Kconfig +9 −36
  17. security/Makefile +2 −0
  18. security/apparmor/Kconfig +0 −16
  19. security/apparmor/capability.c +7 −7
  20. security/apparmor/domain.c +2 −2
  21. security/apparmor/include/capability.h +1 −1
  22. security/apparmor/include/cred.h +15 −1
  23. security/apparmor/include/file.h +4 −1
  24. security/apparmor/include/lib.h +4 −0
  25. security/apparmor/include/task.h +4 −14
  26. security/apparmor/ipc.c +2 −1
  27. security/apparmor/lsm.c +31 −36
  28. security/apparmor/resource.c +1 −1
  29. security/apparmor/task.c +3 −3
  30. security/commoncap.c +18 −10
  31. security/integrity/ima/ima_appraise.c +1 −0
  32. security/integrity/ima/ima_policy.c +4 −0
  33. security/integrity/ima/ima_template_lib.c +1 −0
  34. security/keys/keyctl.c +1 −1
  35. security/keys/keyring.c +1 −0
  36. security/keys/process_keys.c +3 −0
  37. security/keys/request_key.c +4 −0
  38. security/loadpin/loadpin.c +7 −1
  39. security/safesetid/Kconfig +14 −0
  40. security/safesetid/Makefile +7 −0
  41. security/safesetid/lsm.c +277 −0
  42. security/safesetid/lsm.h +33 −0
  43. security/safesetid/securityfs.c +193 −0
  44. security/security.c +580 −68
  45. security/selinux/Kconfig +0 −15
  46. security/selinux/Makefile +1 −1
  47. security/selinux/exports.c +0 −23
  48. security/selinux/hooks.c +97 −265
  49. security/selinux/include/audit.h +0 −3
  50. security/selinux/include/objsec.h +34 −4
  51. security/selinux/selinuxfs.c +2 −2
  52. security/selinux/ss/services.c +0 −1
  53. security/selinux/xfrm.c +2 −2
  54. security/smack/smack.h +39 −5
  55. security/smack/smack_access.c +3 −3
  56. security/smack/smack_lsm.c +106 −211
  57. security/smack/smackfs.c +9 −9
  58. security/tomoyo/audit.c +20 −11
  59. security/tomoyo/common.c +139 −60
  60. security/tomoyo/common.h +27 −24
  61. security/tomoyo/condition.c +43 −16
  62. security/tomoyo/domain.c +51 −25
  63. security/tomoyo/file.c +20 −0
  64. security/tomoyo/gc.c +19 −0
  65. security/tomoyo/group.c +5 −0
  66. security/tomoyo/load_policy.c +4 −4
  67. security/tomoyo/memory.c +7 −2
  68. security/tomoyo/mount.c +2 −0
  69. security/tomoyo/realpath.c +15 −3
  70. security/tomoyo/securityfs_if.c +17 −13
  71. security/tomoyo/tomoyo.c +102 −58
  72. security/tomoyo/util.c +18 −5
  73. security/yama/yama_lsm.c +7 −1
  74. tools/testing/selftests/safesetid/.gitignore +1 −0
  75. tools/testing/selftests/safesetid/Makefile +8 −0
  76. tools/testing/selftests/safesetid/config +2 −0
  77. tools/testing/selftests/safesetid/safesetid-test.c +334 −0
  78. tools/testing/selftests/safesetid/safesetid-test.sh +26 −0
Documentation/admin-guide/LSM/SafeSetID.rst+107−0
@@ -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.
Documentation/admin-guide/LSM/index.rst+11−3
@@ -17,9 +17,8 @@ MAC extensions, other extensions can be built using the LSM to provide · 16 unmodified lines
1717 specific changes to system operation when these tweaks are not available
1818 in the core functionality of Linux itself.
1919
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.
2322 For more details on capabilities, see ``capabilities(7)`` in the Linux
2423 man-pages project.
2524
@@ -30,6 +29,14 @@ order in which checks are made. The capability module will always · 4 unmodified lines
3029 be first, followed by any "minor" modules (e.g. Yama) and then
3130 the one "major" module (e.g. SELinux) if there is one configured.
3231
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+
3340 .. toctree::
3441 :maxdepth: 1
3542
@@ -39,3 +46,4 @@ the one "major" module (e.g. SELinux) if there is one configured. · 3 unmodified lines
3946 Smack
4047 tomoyo
4148 Yama
49+ SafeSetID
Documentation/admin-guide/kernel-parameters.txt+7−5
@@ -2333,6 +2333,10 @@ · 2332 unmodified lines
23332333
23342334 lsm.debug [SECURITY] Enable LSM initialization debugging output.
23352335
2336+ lsm=lsm1,...,lsmN
2337+ [SECURITY] Choose order of LSM initialization. This
2338+ overrides CONFIG_LSM, and the "security=" parameter.
2339+
23362340 machvec= [IA-64] Force the use of a particular machine-vector
23372341 (machvec) in a generic kernel.
23382342 Example: machvec=hpzx1_swiotlb
@@ -4110,11 +4114,9 @@ · 1771 unmodified lines
41104114 Note: increases power consumption, thus should only be
41114115 enabled if running jitter sensitive (HPC/RT) workloads.
41124116
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.
41184120
41194121 selinux= [SELINUX] Disable or enable SELinux at boot time.
41204122 Format: { "0" | "1" }
MAINTAINERS+5−6
@@ -15557,12 +15557,11 @@ F: mm/shmem.c · 15556 unmodified lines
1555715557 TOMOYO SECURITY MODULE
1555815558 M: Kentaro Takeda <takedakn@nttdata.co.jp>
1555915559 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/
1556615565 S: Maintained
1556715566 F: security/tomoyo/
1556815567
fs/proc/base.c+55−9
@@ -140,9 +140,13 @@ struct pid_entry { · 139 unmodified lines
140140 #define REG(NAME, MODE, fops) \
141141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142142 #define ONE(NAME, MODE, show) \
143− NOD(NAME, (S_IFREG|(MODE)), \
143+ NOD(NAME, (S_IFREG|(MODE)), \
144144 NULL, &proc_single_file_operations, \
145145 { .proc_show = show } )
146+#define ATTR(LSM, NAME, MODE) \
147+ NOD(NAME, (S_IFREG|(MODE)), \
148+ NULL, &proc_pid_attr_operations, \
149+ { .lsm = LSM })
146150
147151 /*
148152 * 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
25212525 if (!task)
25222526 return -ESRCH;
25232527
2524− length = security_getprocattr(task,
2528+ length = security_getprocattr(task, PROC_I(inode)->op.lsm,
25252529 (char*)file->f_path.dentry->d_name.name,
25262530 &p);
25272531 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
25702574 if (rv < 0)
25712575 goto out_free;
25722576
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);
25742580 mutex_unlock(&current->signal->cred_guard_mutex);
25752581 out_free:
25762582 kfree(page);
@@ -2584,13 +2590,53 @@ static const struct file_operations proc_pid_attr_operations = { · 7 unmodified lines
25842590 .llseek = generic_file_llseek,
25852591 };
25862592
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+
25872629 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
25942640 };
25952641
25962642 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
fs/proc/internal.h+1−0
@@ -82,6 +82,7 @@ union proc_op { · 81 unmodified lines
8282 int (*proc_show)(struct seq_file *m,
8383 struct pid_namespace *ns, struct pid *pid,
8484 struct task_struct *task);
85+ const char *lsm;
8586 };
8687
8788 struct proc_inode {
include/linux/capability.h+5−0
@@ -209,6 +209,7 @@ extern bool has_ns_capability_noaudit(struct task_struct *t, · 208 unmodified lines
209209 extern bool capable(int cap);
210210 extern bool ns_capable(struct user_namespace *ns, int cap);
211211 extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
212+extern bool ns_capable_setid(struct user_namespace *ns, int cap);
212213 #else
213214 static inline bool has_capability(struct task_struct *t, int cap)
214215 {
@@ -240,6 +241,10 @@ static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap) · 25 unmodified lines
240241 {
241242 return true;
242243 }
244+static inline bool ns_capable_setid(struct user_namespace *ns, int cap)
245+{
246+ return true;
247+}
243248 #endif /* CONFIG_MULTIUSER */
244249 extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode);
245250 extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
include/linux/cred.h+0−1
@@ -15,7 +15,6 @@ · 14 unmodified lines
1515 #include <linux/capability.h>
1616 #include <linux/init.h>
1717 #include <linux/key.h>
18−#include <linux/selinux.h>
1918 #include <linux/atomic.h>
2019 #include <linux/uidgid.h>
2120 #include <linux/sched.h>
include/linux/lsm_hooks.h+30−15
@@ -1270,7 +1270,7 @@ · 1269 unmodified lines
12701270 * @cred contains the credentials to use.
12711271 * @ns contains the user namespace we want the capability in
12721272 * @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>
12741274 * Return 0 if the capability is granted for @tsk.
12751275 * @syslog:
12761276 * Check permission before accessing the kernel message ring or changing
@@ -1446,8 +1446,10 @@ union security_list_options { · 169 unmodified lines
14461446 const kernel_cap_t *effective,
14471447 const kernel_cap_t *inheritable,
14481448 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);
14511453 int (*quotactl)(int cmds, int type, int id, struct super_block *sb);
14521454 int (*quota_on)(struct dentry *dentry);
14531455 int (*syslog)(int type);
@@ -2027,6 +2029,18 @@ struct security_hook_list { · 573 unmodified lines
20272029 char *lsm;
20282030 } __randomize_layout;
20292031
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+
20302044 /*
20312045 * Initializing a security_hook_list structure takes
20322046 * up a lot of space in a source file. This macro takes
@@ -2042,9 +2056,21 @@ extern char *lsm_names; · 9 unmodified lines
20422056 extern void security_add_hooks(struct security_hook_list *hooks, int count,
20432057 char *lsm);
20442058
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+
20452067 struct lsm_info {
20462068 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 */
20472072 int (*init)(void); /* Required. */
2073+ struct lsm_blob_sizes *blobs; /* Optional: for blob sharing. */
20482074 };
20492075
20502076 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
20842110 #define __lsm_ro_after_init __ro_after_init
20852111 #endif /* CONFIG_SECURITY_WRITABLE_HOOKS */
20862112
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);
20992114
21002115 #endif /* ! __LINUX_LSM_HOOKS_H */
include/linux/security.h+24−19
@@ -54,9 +54,12 @@ struct xattr; · 53 unmodified lines
5454 struct xfrm_sec_ctx;
5555 struct mm_struct;
5656
57+/* Default (no) options for the capable function */
58+#define CAP_OPT_NONE 0x0
5759 /* 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)
6063
6164 /* LSM Agnostic defines for sb_set_mnt_opts */
6265 #define SECURITY_LSM_NATIVE_LABELS 1
@@ -72,7 +75,7 @@ enum lsm_event { · 9 unmodified lines
7275
7376 /* These functions are in security/commoncap.c */
7477 extern int cap_capable(const struct cred *cred, struct user_namespace *ns,
75− int cap, int audit);
78+ int cap, unsigned int opts);
7679 extern int cap_settime(const struct timespec64 *ts, const struct timezone *tz);
7780 extern int cap_ptrace_access_check(struct task_struct *child, unsigned int mode);
7881 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
207210 const kernel_cap_t *effective,
208211 const kernel_cap_t *inheritable,
209212 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);
214217 int security_quotactl(int cmds, int type, int id, struct super_block *sb);
215218 int security_quota_on(struct dentry *dentry);
216219 int security_syslog(int type);
@@ -366,8 +369,10 @@ int security_sem_semctl(struct kern_ipc_perm *sma, int cmd); · 149 unmodified lines
366369 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
367370 unsigned nsops, int alter);
368371 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);
371376 int security_netlink_send(struct sock *sk, struct sk_buff *skb);
372377 int security_ismaclabel(const char *name);
373378 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
462467 }
463468
464469 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)
466473 {
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);
473475 }
474476
475477 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
11121114 return 0;
11131115 }
11141116
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)
11161119 { }
11171120
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)
11191123 {
11201124 return -EINVAL;
11211125 }
11221126
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)
11241129 {
11251130 return -EINVAL;
11261131 }
include/linux/selinux.h+0−35
@@ -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 */
kernel/capability.c+33−12
@@ -93,9 +93,7 @@ static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy) · 92 unmodified lines
9393 break;
9494 case _LINUX_CAPABILITY_VERSION_2:
9595 warn_deprecated_v2();
96− /*
97− * fall through - v3 is otherwise equivalent to v2.
98− */
96+ /* fall through - v3 is otherwise equivalent to v2. */
9997 case _LINUX_CAPABILITY_VERSION_3:
10098 *tocopy = _LINUX_CAPABILITY_U32S_3;
10199 break;
@@ -299,7 +297,7 @@ bool has_ns_capability(struct task_struct *t, · 197 unmodified lines
299297 int ret;
300298
301299 rcu_read_lock();
302− ret = security_capable(__task_cred(t), ns, cap);
300+ ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
303301 rcu_read_unlock();
304302
305303 return (ret == 0);
@@ -340,7 +338,7 @@ bool has_ns_capability_noaudit(struct task_struct *t, · 34 unmodified lines
340338 int ret;
341339
342340 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);
344342 rcu_read_unlock();
345343
346344 return (ret == 0);
@@ -363,7 +361,9 @@ bool has_capability_noaudit(struct task_struct *t, int cap) · 16 unmodified lines
363361 return has_ns_capability_noaudit(t, &init_user_ns, cap);
364362 }
365363
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)
367367 {
368368 int capable;
369369
@@ -372,8 +372,7 @@ static bool ns_capable_common(struct user_namespace *ns, int cap, bool audit) · 2 unmodified lines
372372 BUG();
373373 }
374374
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);
377376 if (capable == 0) {
378377 current->flags |= PF_SUPERPRIV;
379378 return true;
@@ -394,7 +393,7 @@ static bool ns_capable_common(struct user_namespace *ns, int cap, bool audit) · 14 unmodified lines
394393 */
395394 bool ns_capable(struct user_namespace *ns, int cap)
396395 {
397− return ns_capable_common(ns, cap, true);
396+ return ns_capable_common(ns, cap, CAP_OPT_NONE);
398397 }
399398 EXPORT_SYMBOL(ns_capable);
400399
@@ -412,10 +411,29 @@ EXPORT_SYMBOL(ns_capable); · 11 unmodified lines
412411 */
413412 bool ns_capable_noaudit(struct user_namespace *ns, int cap)
414413 {
415− return ns_capable_common(ns, cap, false);
414+ return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
416415 }
417416 EXPORT_SYMBOL(ns_capable_noaudit);
418417
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+
419437 /**
420438 * capable - Determine if the current task has a superior capability in effect
421439 * @cap: The capability to be tested for
@@ -448,10 +466,11 @@ EXPORT_SYMBOL(capable); · 26 unmodified lines
448466 bool file_ns_capable(const struct file *file, struct user_namespace *ns,
449467 int cap)
450468 {
469+
451470 if (WARN_ON_ONCE(!cap_valid(cap)))
452471 return false;
453472
454− if (security_capable(file->f_cred, ns, cap) == 0)
473+ if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
455474 return true;
456475
457476 return false;
@@ -500,10 +519,12 @@ bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns) · 42 unmodified lines
500519 {
501520 int ret = 0; /* An absent tracer adds no restrictions */
502521 const struct cred *cred;
522+
503523 rcu_read_lock();
504524 cred = rcu_dereference(tsk->ptracer_cred);
505525 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);
507528 rcu_read_unlock();
508529 return (ret == 0);
509530 }
kernel/cred.c+0−13
@@ -760,19 +760,6 @@ bool creds_are_invalid(const struct cred *cred) · 759 unmodified lines
760760 {
761761 if (cred->magic != CRED_MAGIC)
762762 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
776763 return false;
777764 }
778765 EXPORT_SYMBOL(creds_are_invalid);
kernel/seccomp.c+2−2
@@ -445,8 +445,8 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) · 444 unmodified lines
445445 * behavior of privileged children.
446446 */
447447 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)
450450 return ERR_PTR(-EACCES);
451451
452452 /* Allocate a new seccomp_filter */
kernel/sys.c+5−5
@@ -516,7 +516,7 @@ long __sys_setreuid(uid_t ruid, uid_t euid) · 515 unmodified lines
516516 new->uid = kruid;
517517 if (!uid_eq(old->uid, kruid) &&
518518 !uid_eq(old->euid, kruid) &&
519− !ns_capable(old->user_ns, CAP_SETUID))
519+ !ns_capable_setid(old->user_ns, CAP_SETUID))
520520 goto error;
521521 }
522522
@@ -525,7 +525,7 @@ long __sys_setreuid(uid_t ruid, uid_t euid) · 2 unmodified lines
525525 if (!uid_eq(old->uid, keuid) &&
526526 !uid_eq(old->euid, keuid) &&
527527 !uid_eq(old->suid, keuid) &&
528− !ns_capable(old->user_ns, CAP_SETUID))
528+ !ns_capable_setid(old->user_ns, CAP_SETUID))
529529 goto error;
530530 }
531531
@@ -584,7 +584,7 @@ long __sys_setuid(uid_t uid) · 52 unmodified lines
584584 old = current_cred();
585585
586586 retval = -EPERM;
587− if (ns_capable(old->user_ns, CAP_SETUID)) {
587+ if (ns_capable_setid(old->user_ns, CAP_SETUID)) {
588588 new->suid = new->uid = kuid;
589589 if (!uid_eq(kuid, old->uid)) {
590590 retval = set_user(new);
@@ -646,7 +646,7 @@ long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid) · 55 unmodified lines
646646 old = current_cred();
647647
648648 retval = -EPERM;
649− if (!ns_capable(old->user_ns, CAP_SETUID)) {
649+ if (!ns_capable_setid(old->user_ns, CAP_SETUID)) {
650650 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
651651 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
652652 goto error;
@@ -814,7 +814,7 @@ long __sys_setfsuid(uid_t uid) · 161 unmodified lines
814814
815815 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
816816 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)) {
818818 if (!uid_eq(kuid, old->fsuid)) {
819819 new->fsuid = kuid;
820820 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
security/Kconfig+9−36
@@ -40,8 +40,7 @@ config SECURITYFS · 39 unmodified lines
4040 bool "Enable the securityfs filesystem"
4141 help
4242 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).
4544
4645 If you are unsure how to answer this question, answer N.
4746
@@ -236,45 +235,19 @@ source "security/tomoyo/Kconfig" · 188 unmodified lines
236235 source "security/apparmor/Kconfig"
237236 source "security/loadpin/Kconfig"
238237 source "security/yama/Kconfig"
238+source "security/safesetid/Kconfig"
239239
240240 source "security/integrity/Kconfig"
241241
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"
250245 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.
270249
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.
278251
279252 endmenu
280253
security/Makefile+2−0
@@ -10,6 +10,7 @@ subdir-$(CONFIG_SECURITY_TOMOYO) += tomoyo · 9 unmodified lines
1010 subdir-$(CONFIG_SECURITY_APPARMOR) += apparmor
1111 subdir-$(CONFIG_SECURITY_YAMA) += yama
1212 subdir-$(CONFIG_SECURITY_LOADPIN) += loadpin
13+subdir-$(CONFIG_SECURITY_SAFESETID) += safesetid
1314
1415 # always enable default capabilities
1516 obj-y += commoncap.o
@@ -25,6 +26,7 @@ obj-$(CONFIG_SECURITY_TOMOYO) += tomoyo/ · 9 unmodified lines
2526 obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/
2627 obj-$(CONFIG_SECURITY_YAMA) += yama/
2728 obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/
29+obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/
2830 obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o
2931
3032 # Object integrity file lists
security/apparmor/Kconfig+0−16
@@ -14,22 +14,6 @@ config SECURITY_APPARMOR · 13 unmodified lines
1414
1515 If you are unsure how to answer this question, answer N.
1616
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−
3317 config SECURITY_APPARMOR_HASH
3418 bool "Enable introspection of sha1 hashes for loaded profiles"
3519 depends on SECURITY_APPARMOR
security/apparmor/capability.c+7−7
@@ -110,13 +110,13 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile, · 109 unmodified lines
110110 * profile_capable - test if profile allows use of capability @cap
111111 * @profile: profile being enforced (NOT NULL, NOT unconfined)
112112 * @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
114114 * @sa: audit data (MAY BE NULL indicating no auditing)
115115 *
116116 * Returns: 0 if allowed else -EPERM
117117 */
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)
120120 {
121121 int error;
122122
@@ -126,7 +126,7 @@ static int profile_capable(struct aa_profile *profile, int cap, int audit, · 3 unmodified lines
126126 else
127127 error = -EPERM;
128128
129− if (audit == SECURITY_CAP_NOAUDIT) {
129+ if (opts & CAP_OPT_NOAUDIT) {
130130 if (!COMPLAIN_MODE(profile))
131131 return error;
132132 /* 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
142142 * aa_capable - test permission to use capability
143143 * @label: label being tested for capability (NOT NULL)
144144 * @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
146146 *
147147 * Look up capability in profile capability set.
148148 *
149149 * Returns: 0 on success, or else an error code.
150150 */
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)
152152 {
153153 struct aa_profile *profile;
154154 int error = 0;
@@ -156,7 +156,7 @@ int aa_capable(struct aa_label *label, int cap, int audit) · 1 unmodified line
156156
157157 sa.u.cap = cap;
158158 error = fn_for_each_confined(label, profile,
159− profile_capable(profile, cap, audit, &sa));
159+ profile_capable(profile, cap, opts, &sa));
160160
161161 return error;
162162 }
security/apparmor/domain.c+2−2
@@ -572,7 +572,7 @@ static struct aa_label *x_to_label(struct aa_profile *profile, · 571 unmodified lines
572572 stack = NULL;
573573 break;
574574 }
575− /* fall through to X_NAME */
575+ /* fall through - to X_NAME */
576576 case AA_X_NAME:
577577 if (xindex & AA_X_CHILD)
578578 /* released by caller */
@@ -975,7 +975,7 @@ int apparmor_bprm_set_creds(struct linux_binprm *bprm) · 396 unmodified lines
975975 }
976976 aa_put_label(cred_label(bprm->cred));
977977 /* transfer reference, released when cred is freed */
978− cred_label(bprm->cred) = new;
978+ set_cred_label(bprm->cred, new);
979979
980980 done:
981981 aa_put_label(label);
security/apparmor/include/capability.h+1−1
@@ -40,7 +40,7 @@ struct aa_caps { · 39 unmodified lines
4040
4141 extern struct aa_sfs_entry aa_sfs_entry_caps[];
4242
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);
4444
4545 static inline void aa_free_cap_rules(struct aa_caps *caps)
4646 {
security/apparmor/include/cred.h+15−1
@@ -23,8 +23,22 @@ · 22 unmodified lines
2323 #include "policy_ns.h"
2424 #include "task.h"
2525
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+}
2733
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+}
2842
2943 /**
3044 * aa_cred_raw_label - obtain cred's label
security/apparmor/include/file.h+4−1
@@ -32,7 +32,10 @@ struct path; · 31 unmodified lines
3232 AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
3333 AA_EXEC_MMAP | AA_MAY_LINK)
3434
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+}
3639
3740 /* struct aa_file_ctx - the AppArmor context the file was opened in
3841 * @lock: lock to update the ctx
security/apparmor/include/lib.h+4−0
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616
1717 #include <linux/slab.h>
1818 #include <linux/fs.h>
19+#include <linux/lsm_hooks.h>
1920
2021 #include "match.h"
2122
@@ -55,6 +56,9 @@ const char *aa_splitn_fqname(const char *fqname, size_t n, const char **ns_name, · 33 unmodified lines
5556 size_t *ns_len);
5657 void aa_info_message(const char *str);
5758
59+/* Security blob offsets */
60+extern struct lsm_blob_sizes apparmor_blob_sizes;
61+
5862 /**
5963 * aa_strneq - compare null terminated @str to a non null terminated substring
6064 * @str: a null terminated string
security/apparmor/include/task.h+4−14
@@ -14,7 +14,10 @@ · 13 unmodified lines
1414 #ifndef __AA_TASK_H
1515 #define __AA_TASK_H
1616
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+}
1821
1922 /*
2023 * 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
3639 int aa_restore_previous_label(u64 cookie);
3740 struct aa_label *aa_get_task_label(struct task_struct *task);
3841
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−
5042 /**
5143 * aa_free_task_ctx - free a task_ctx
5244 * @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
5749 aa_put_label(ctx->nnp);
5850 aa_put_label(ctx->previous);
5951 aa_put_label(ctx->onexec);
60−
61− kzfree(ctx);
6252 }
6353 }
6454
security/apparmor/ipc.c+2−1
@@ -107,7 +107,8 @@ static int profile_tracer_perm(struct aa_profile *tracer, · 106 unmodified lines
107107 aad(sa)->label = &tracer->label;
108108 aad(sa)->peer = tracee;
109109 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);
111112
112113 return aa_audit(AUDIT_APPARMOR_AUTO, tracer, sa, audit_ptrace_cb);
113114 }
security/apparmor/lsm.c+31−36
@@ -60,7 +60,7 @@ DEFINE_PER_CPU(struct aa_buffers, aa_buffers); · 59 unmodified lines
6060 static void apparmor_cred_free(struct cred *cred)
6161 {
6262 aa_put_label(cred_label(cred));
63− cred_label(cred) = NULL;
63+ set_cred_label(cred, NULL);
6464 }
6565
6666 /*
@@ -68,7 +68,7 @@ static void apparmor_cred_free(struct cred *cred) · 1 unmodified line
6868 */
6969 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
7070 {
71− cred_label(cred) = NULL;
71+ set_cred_label(cred, NULL);
7272 return 0;
7373 }
7474
@@ -78,7 +78,7 @@ static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) · 3 unmodified lines
7878 static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
7979 gfp_t gfp)
8080 {
81− cred_label(new) = aa_get_newest_label(cred_label(old));
81+ set_cred_label(new, aa_get_newest_label(cred_label(old)));
8282 return 0;
8383 }
8484
@@ -87,26 +87,21 @@ static int apparmor_cred_prepare(struct cred *new, const struct cred *old, · 2 unmodified lines
8787 */
8888 static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
8989 {
90− cred_label(new) = aa_get_newest_label(cred_label(old));
90+ set_cred_label(new, aa_get_newest_label(cred_label(old)));
9191 }
9292
9393 static void apparmor_task_free(struct task_struct *task)
9494 {
9595
9696 aa_free_task_ctx(task_ctx(task));
97− task_ctx(task) = NULL;
9897 }
9998
10099 static int apparmor_task_alloc(struct task_struct *task,
101100 unsigned long clone_flags)
102101 {
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);
107103
108104 aa_dup_task_ctx(new, task_ctx(current));
109− task_ctx(task) = new;
110105
111106 return 0;
112107 }
@@ -177,14 +172,14 @@ static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, · 64 unmodified lines
177172 }
178173
179174 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
180− int cap, int audit)
175+ int cap, unsigned int opts)
181176 {
182177 struct aa_label *label;
183178 int error = 0;
184179
185180 label = aa_get_newest_cred_label(cred);
186181 if (!unconfined(label))
187− error = aa_capable(label, cap, audit);
182+ error = aa_capable(label, cap, opts);
188183 aa_put_label(label);
189184
190185 return error;
@@ -434,21 +429,21 @@ static int apparmor_file_open(struct file *file) · 243 unmodified lines
434429
435430 static int apparmor_file_alloc_security(struct file *file)
436431 {
437− int error = 0;
438−
439− /* freed by apparmor_file_free_security */
432+ struct aa_file_ctx *ctx = file_ctx(file);
440433 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);
445434
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;
447439 }
448440
449441 static void apparmor_file_free_security(struct file *file)
450442 {
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));
452447 }
453448
454449 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
11511146 }
11521147 #endif
11531148
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+
11541158 static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
11551159 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
11561160 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
@@ -1333,8 +1337,8 @@ bool aa_g_paranoid_load = true; · 176 unmodified lines
13331337 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
13341338
13351339 /* 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);
13381342
13391343 static int __init apparmor_enabled_setup(char *str)
13401344 {
@@ -1479,14 +1483,8 @@ static int param_set_mode(const char *val, const struct kernel_param *kp) · 138 unmodified lines
14791483 static int __init set_init_ctx(void)
14801484 {
14811485 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;
14871486
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)));
14901488
14911489 return 0;
14921490 }
@@ -1665,12 +1663,6 @@ static int __init apparmor_init(void) · 172 unmodified lines
16651663 {
16661664 int error;
16671665
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−
16741666 aa_secids_init();
16751667
16761668 error = aa_setup_dfa_engine();
@@ -1731,5 +1723,8 @@ alloc_out: · 54 unmodified lines
17311723
17321724 DEFINE_LSM(apparmor) = {
17331725 .name = "apparmor",
1726+ .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1727+ .enabled = &apparmor_enabled,
1728+ .blobs = &apparmor_blob_sizes,
17341729 .init = apparmor_init,
17351730 };
security/apparmor/resource.c+1−1
@@ -124,7 +124,7 @@ int aa_task_setrlimit(struct aa_label *label, struct task_struct *task, · 123 unmodified lines
124124 */
125125
126126 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)
128128 error = fn_for_each(label, profile,
129129 audit_resource(profile, resource,
130130 new_rlim->rlim_max, peer,
security/apparmor/task.c+3−3
@@ -81,7 +81,7 @@ int aa_replace_current_label(struct aa_label *label) · 80 unmodified lines
8181 */
8282 aa_get_label(label);
8383 aa_put_label(cred_label(new));
84− cred_label(new) = label;
84+ set_cred_label(new, label);
8585
8686 commit_creds(new);
8787 return 0;
@@ -138,7 +138,7 @@ int aa_set_current_hat(struct aa_label *label, u64 token) · 50 unmodified lines
138138 return -EACCES;
139139 }
140140
141− cred_label(new) = aa_get_newest_label(label);
141+ set_cred_label(new, aa_get_newest_label(label));
142142 /* clear exec on switching context */
143143 aa_put_label(ctx->onexec);
144144 ctx->onexec = NULL;
@@ -172,7 +172,7 @@ int aa_restore_previous_label(u64 token) · 27 unmodified lines
172172 return -ENOMEM;
173173
174174 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));
176176 AA_BUG(!cred_label(new));
177177 /* clear exec && prev information when restoring to previous context */
178178 aa_clear_task_ctx_trans(ctx);
security/commoncap.c+18−10
@@ -57,7 +57,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname) · 56 unmodified lines
5757 * @cred: The credentials to use
5858 * @ns: The user namespace in which we need the capability
5959 * @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
6161 *
6262 * Determine whether the nominated task has the specified capability amongst
6363 * 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
6868 * kernel's capable() and has_capability() returns 1 for this case.
6969 */
7070 int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
71− int cap, int audit)
71+ int cap, unsigned int opts)
7272 {
7373 struct user_namespace *ns = targ_ns;
7474
@@ -222,12 +222,11 @@ int cap_capget(struct task_struct *target, kernel_cap_t *effective, · 147 unmodified lines
222222 */
223223 static inline int cap_inh_is_capped(void)
224224 {
225−
226225 /* they are so limited unless the current task has the CAP_SETPCAP
227226 * capability
228227 */
229228 if (cap_capable(current_cred(), current_cred()->user_ns,
230− CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
229+ CAP_SETPCAP, CAP_OPT_NONE) == 0)
231230 return 0;
232231 return 1;
233232 }
@@ -1208,8 +1207,9 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3, · 974 unmodified lines
12081207 || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
12091208 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
12101209 || (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]*/
12131213 /*
12141214 * [1] no changing of bits that are locked
12151215 * [2] no unlocking of locks
@@ -1304,9 +1304,10 @@ int cap_vm_enough_memory(struct mm_struct *mm, long pages) · 88 unmodified lines
13041304 {
13051305 int cap_sys_admin = 0;
13061306
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)
13091309 cap_sys_admin = 1;
1310+
13101311 return cap_sys_admin;
13111312 }
13121313
@@ -1325,7 +1326,7 @@ int cap_mmap_addr(unsigned long addr) · 12 unmodified lines
13251326
13261327 if (addr < dac_mmap_min_addr) {
13271328 ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
1328− SECURITY_CAP_AUDIT);
1329+ CAP_OPT_NONE);
13291330 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
13301331 if (ret == 0)
13311332 current->flags |= PF_SUPERPRIV;
@@ -1362,10 +1363,17 @@ struct security_hook_list capability_hooks[] __lsm_ro_after_init = { · 30 unmodified lines
13621363 LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory),
13631364 };
13641365
1365−void __init capability_add_hooks(void)
1366+static int __init capability_init(void)
13661367 {
13671368 security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks),
13681369 "capability");
1370+ return 0;
13691371 }
13701372
1373+DEFINE_LSM(capability) = {
1374+ .name = "capability",
1375+ .order = LSM_ORDER_FIRST,
1376+ .init = capability_init,
1377+};
1378+
13711379 #endif /* CONFIG_SECURITY */
security/integrity/ima/ima_appraise.c+1−0
@@ -114,6 +114,7 @@ static void ima_set_cache_status(struct integrity_iint_cache *iint, · 113 unmodified lines
114114 break;
115115 case CREDS_CHECK:
116116 iint->ima_creds_status = status;
117+ break;
117118 case FILE_CHECK:
118119 case POST_SETATTR:
119120 iint->ima_file_status = status;
security/integrity/ima/ima_policy.c+4−0
@@ -938,10 +938,12 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) · 937 unmodified lines
938938 case Opt_uid_gt:
939939 case Opt_euid_gt:
940940 entry->uid_op = &uid_gt;
941+ /* fall through */
941942 case Opt_uid_lt:
942943 case Opt_euid_lt:
943944 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
944945 entry->uid_op = &uid_lt;
946+ /* fall through */
945947 case Opt_uid_eq:
946948 case Opt_euid_eq:
947949 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
970972 break;
971973 case Opt_fowner_gt:
972974 entry->fowner_op = &uid_gt;
975+ /* fall through */
973976 case Opt_fowner_lt:
974977 if (token == Opt_fowner_lt)
975978 entry->fowner_op = &uid_lt;
979+ /* fall through */
976980 case Opt_fowner_eq:
977981 ima_log_string_op(ab, "fowner", args[0].from,
978982 entry->fowner_op);
security/integrity/ima/ima_template_lib.c+1−0
@@ -83,6 +83,7 @@ static void ima_show_template_data_ascii(struct seq_file *m, · 82 unmodified lines
8383 /* skip ':' and '\0' */
8484 buf_ptr += 2;
8585 buflen -= buf_ptr - field_data->data;
86+ /* fall through */
8687 case DATA_FMT_DIGEST:
8788 case DATA_FMT_HEX:
8889 if (!buflen)
security/keys/keyctl.c+1−1
@@ -1752,7 +1752,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, · 1751 unmodified lines
17521752 return -EINVAL;
17531753 return keyctl_pkey_query((key_serial_t)arg2,
17541754 (const char __user *)arg4,
1755− (struct keyctl_pkey_query *)arg5);
1755+ (struct keyctl_pkey_query __user *)arg5);
17561756
17571757 case KEYCTL_PKEY_ENCRYPT:
17581758 case KEYCTL_PKEY_DECRYPT:
security/keys/keyring.c+1−0
@@ -246,6 +246,7 @@ static unsigned long keyring_get_key_chunk(const void *data, int level) · 245 unmodified lines
246246 (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8));
247247 n--;
248248 offset = 1;
249+ /* fall through */
249250 default:
250251 offset += sizeof(chunk) - 1;
251252 offset += (level - 3) * sizeof(chunk);
security/keys/process_keys.c+3−0
@@ -380,6 +380,7 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 379 unmodified lines
380380 case -EAGAIN: /* no key */
381381 if (ret)
382382 break;
383+ /* fall through */
383384 case -ENOKEY: /* negative key */
384385 ret = key_ref;
385386 break;
@@ -404,6 +405,7 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 18 unmodified lines
404405 case -EAGAIN: /* no key */
405406 if (ret)
406407 break;
408+ /* fall through */
407409 case -ENOKEY: /* negative key */
408410 ret = key_ref;
409411 break;
@@ -424,6 +426,7 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 14 unmodified lines
424426 case -EAGAIN: /* no key */
425427 if (ret)
426428 break;
429+ /* fall through */
427430 case -ENOKEY: /* negative key */
428431 ret = key_ref;
429432 break;
security/keys/request_key.c+4−0
@@ -273,16 +273,19 @@ static int construct_get_dest_keyring(struct key **_dest_keyring) · 272 unmodified lines
273273 }
274274 }
275275
276+ /* fall through */
276277 case KEY_REQKEY_DEFL_THREAD_KEYRING:
277278 dest_keyring = key_get(cred->thread_keyring);
278279 if (dest_keyring)
279280 break;
280281
282+ /* fall through */
281283 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
282284 dest_keyring = key_get(cred->process_keyring);
283285 if (dest_keyring)
284286 break;
285287
288+ /* fall through */
286289 case KEY_REQKEY_DEFL_SESSION_KEYRING:
287290 rcu_read_lock();
288291 dest_keyring = key_get(
@@ -292,6 +295,7 @@ static int construct_get_dest_keyring(struct key **_dest_keyring) · 3 unmodified lines
292295 if (dest_keyring)
293296 break;
294297
298+ /* fall through */
295299 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
296300 dest_keyring =
297301 key_get(cred->user->session_keyring);
security/loadpin/loadpin.c+7−1
@@ -187,13 +187,19 @@ static struct security_hook_list loadpin_hooks[] __lsm_ro_after_init = { · 186 unmodified lines
187187 LSM_HOOK_INIT(kernel_load_data, loadpin_load_data),
188188 };
189189
190−void __init loadpin_add_hooks(void)
190+static int __init loadpin_init(void)
191191 {
192192 pr_info("ready to pin (currently %senforcing)\n",
193193 enforce ? "" : "not ");
194194 security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
195+ return 0;
195196 }
196197
198+DEFINE_LSM(loadpin) = {
199+ .name = "loadpin",
200+ .init = loadpin_init,
201+};
202+
197203 /* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
198204 module_param(enforce, int, 0);
199205 MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
security/safesetid/Kconfig+14−0
@@ -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.
security/safesetid/Makefile+7−0
@@ -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
security/safesetid/lsm.c+277−0
@@ -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+};
security/safesetid/lsm.h+33−0
@@ -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 */
security/safesetid/securityfs.c+193−0
@@ -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);
security/security.c+580−68
@@ -30,20 +30,32 @@ · 29 unmodified lines
3030 #include <linux/personality.h>
3131 #include <linux/backing-dev.h>
3232 #include <linux/string.h>
33+#include <linux/msg.h>
3334 #include <net/flow.h>
3435
3536 #define MAX_LSM_EVM_XATTR 2
3637
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)
3940
4041 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
4142 static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
4243
44+static struct kmem_cache *lsm_file_cache;
45+static struct kmem_cache *lsm_inode_cache;
46+
4347 char *lsm_names;
48+static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init;
49+
4450 /* 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;
4759
4860 static __initdata bool debug;
4961 #define init_debug(...) \
@@ -52,18 +64,269 @@ static __initdata bool debug; · 2 unmodified lines
5264 pr_info(__VA_ARGS__); \
5365 } while (0)
5466
55−static void __init major_lsm_init(void)
67+static bool __init is_enabled(struct lsm_info *lsm)
5668 {
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;
59202
60− for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
61203 init_debug("initializing %s\n", lsm->name);
62204 ret = lsm->init();
63205 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
64206 }
65207 }
66208
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+
67330 /**
68331 * security_init - initializes the security framework
69332 *
@@ -80,28 +343,27 @@ int __init security_init(void) · 10 unmodified lines
80343 i++)
81344 INIT_HLIST_HEAD(&list[i]);
82345
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();
94348
95349 return 0;
96350 }
97351
98352 /* 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)
100362 {
101− strncpy(chosen_lsm, str, SECURITY_NAME_MAX);
363+ chosen_lsm_order = str;
102364 return 1;
103365 }
104−__setup("security=", choose_lsm);
366+__setup("lsm=", choose_lsm_order);
105367
106368 /* Enable LSM order debugging. */
107369 static int __init enable_debug(char *str)
@@ -147,29 +409,6 @@ static int lsm_append(char *new, char **result) · 39 unmodified lines
147409 return 0;
148410 }
149411
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−
173412 /**
174413 * security_add_hooks - Add a modules hooks to the hook lists.
175414 * @hooks: the hooks to add
@@ -209,6 +448,161 @@ int unregister_lsm_notifier(struct notifier_block *nb) · 33 unmodified lines
209448 }
210449 EXPORT_SYMBOL(unregister_lsm_notifier);
211450
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+
212606 /*
213607 * Hook list operation macros.
214608 *
@@ -294,16 +688,12 @@ int security_capset(struct cred *new, const struct cred *old, · 79 unmodified lines
294688 effective, inheritable, permitted);
295689 }
296690
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)
299695 {
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);
307697 }
308698
309699 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
468858
469859 int security_inode_alloc(struct inode *inode)
470860 {
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);
473877 }
474878
475879 void security_inode_free(struct inode *inode)
476880 {
477881 integrity_inode_free(inode);
478882 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);
479895 }
480896
481897 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
9051321
9061322 int security_file_alloc(struct file *file)
9071323 {
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;
9091332 }
9101333
9111334 void security_file_free(struct file *file)
9121335 {
1336+ void *blob;
1337+
9131338 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+ }
9141345 }
9151346
9161347 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
10121443
10131444 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
10141445 {
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;
10161454 }
10171455
10181456 void security_task_free(struct task_struct *task)
10191457 {
10201458 call_void_hook(task_free, task);
1459+
1460+ kfree(task->security);
1461+ task->security = NULL;
10211462 }
10221463
10231464 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
10241465 {
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;
10261475 }
10271476
10281477 void security_cred_free(struct cred *cred)
@@ -1035,11 +1484,22 @@ void security_cred_free(struct cred *cred) · 6 unmodified lines
10351484 return;
10361485
10371486 call_void_hook(cred_free, cred);
1487+
1488+ kfree(cred->security);
1489+ cred->security = NULL;
10381490 }
10391491
10401492 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
10411493 {
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;
10431503 }
10441504
10451505 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
12201680
12211681 int security_msg_msg_alloc(struct msg_msg *msg)
12221682 {
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;
12241691 }
12251692
12261693 void security_msg_msg_free(struct msg_msg *msg)
12271694 {
12281695 call_void_hook(msg_msg_free_security, msg);
1696+ kfree(msg->security);
1697+ msg->security = NULL;
12291698 }
12301699
12311700 int security_msg_queue_alloc(struct kern_ipc_perm *msq)
12321701 {
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;
12341710 }
12351711
12361712 void security_msg_queue_free(struct kern_ipc_perm *msq)
12371713 {
12381714 call_void_hook(msg_queue_free_security, msq);
1715+ kfree(msq->security);
1716+ msq->security = NULL;
12391717 }
12401718
12411719 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
12621740
12631741 int security_shm_alloc(struct kern_ipc_perm *shp)
12641742 {
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;
12661751 }
12671752
12681753 void security_shm_free(struct kern_ipc_perm *shp)
12691754 {
12701755 call_void_hook(shm_free_security, shp);
1756+ kfree(shp->security);
1757+ shp->security = NULL;
12711758 }
12721759
12731760 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
12871774
12881775 int security_sem_alloc(struct kern_ipc_perm *sma)
12891776 {
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;
12911785 }
12921786
12931787 void security_sem_free(struct kern_ipc_perm *sma)
12941788 {
12951789 call_void_hook(sem_free_security, sma);
1790+ kfree(sma->security);
1791+ sma->security = NULL;
12961792 }
12971793
12981794 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
13191815 }
13201816 EXPORT_SYMBOL(security_d_instantiate);
13211817
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)
13231820 {
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;
13251829 }
13261830
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)
13281833 {
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;
13301842 }
13311843
13321844 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
security/selinux/Kconfig+0−15
@@ -22,21 +22,6 @@ config SECURITY_SELINUX_BOOTPARAM · 21 unmodified lines
2222
2323 If you are unsure how to answer this question, answer N.
2424
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−
4025 config SECURITY_SELINUX_DISABLE
4126 bool "NSA SELinux runtime disable"
4227 depends on SECURITY_SELINUX
security/selinux/Makefile+1−1
@@ -6,7 +6,7 @@ · 5 unmodified lines
66 obj-$(CONFIG_SECURITY_SELINUX) := selinux.o
77
88 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 \
1010 ss/ebitmap.o ss/hashtab.o ss/symtab.o ss/sidtab.o ss/avtab.o \
1111 ss/policydb.o ss/services.o ss/conditional.o ss/mls.o ss/status.o
1212
security/selinux/exports.c+0−23
@@ -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);
security/selinux/hooks.c+97−265
@@ -79,7 +79,6 @@ · 78 unmodified lines
7979 #include <linux/personality.h>
8080 #include <linux/audit.h>
8181 #include <linux/string.h>
82−#include <linux/selinux.h>
8382 #include <linux/mutex.h>
8483 #include <linux/posix-timers.h>
8584 #include <linux/syslog.h>
@@ -121,9 +120,8 @@ __setup("enforcing=", enforcing_setup); · 35 unmodified lines
121120 #define selinux_enforcing_boot 1
122121 #endif
123122
123+int selinux_enabled __lsm_ro_after_init = 1;
124124 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
125−int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
126−
127125 static int __init selinux_enabled_setup(char *str)
128126 {
129127 unsigned long enabled;
@@ -132,8 +130,6 @@ static int __init selinux_enabled_setup(char *str) · 2 unmodified lines
132130 return 1;
133131 }
134132 __setup("selinux=", selinux_enabled_setup);
135−#else
136−int selinux_enabled = 1;
137133 #endif
138134
139135 static unsigned int selinux_checkreqprot_boot =
@@ -149,9 +145,6 @@ static int __init checkreqprot_setup(char *str) · 9 unmodified lines
149145 }
150146 __setup("checkreqprot=", checkreqprot_setup);
151147
152−static struct kmem_cache *sel_inode_cache;
153−static struct kmem_cache *file_security_cache;
154−
155148 /**
156149 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157150 *
@@ -214,12 +207,8 @@ static void cred_init_security(void) · 56 unmodified lines
214207 struct cred *cred = (struct cred *) current->real_cred;
215208 struct task_security_struct *tsec;
216209
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);
221211 tsec->osid = tsec->sid = SECINITSID_KERNEL;
222− cred->security = tsec;
223212 }
224213
225214 /*
@@ -229,7 +218,7 @@ static inline u32 cred_sid(const struct cred *cred) · 3 unmodified lines
229218 {
230219 const struct task_security_struct *tsec;
231220
232− tsec = cred->security;
221+ tsec = selinux_cred(cred);
233222 return tsec->sid;
234223 }
235224
@@ -250,13 +239,9 @@ static inline u32 task_sid(const struct task_struct *task) · 14 unmodified lines
250239
251240 static int inode_alloc_security(struct inode *inode)
252241 {
253− struct inode_security_struct *isec;
242+ struct inode_security_struct *isec = selinux_inode(inode);
254243 u32 sid = current_sid();
255244
256− isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
257− if (!isec)
258− return -ENOMEM;
259−
260245 spin_lock_init(&isec->lock);
261246 INIT_LIST_HEAD(&isec->list);
262247 isec->inode = inode;
@@ -264,7 +249,6 @@ static int inode_alloc_security(struct inode *inode) · 1 unmodified line
264249 isec->sclass = SECCLASS_FILE;
265250 isec->task_sid = sid;
266251 isec->initialized = LABEL_INVALID;
267− inode->i_security = isec;
268252
269253 return 0;
270254 }
@@ -281,7 +265,7 @@ static int __inode_security_revalidate(struct inode *inode, · 10 unmodified lines
281265 struct dentry *dentry,
282266 bool may_sleep)
283267 {
284− struct inode_security_struct *isec = inode->i_security;
268+ struct inode_security_struct *isec = selinux_inode(inode);
285269
286270 might_sleep_if(may_sleep);
287271
@@ -302,7 +286,7 @@ static int __inode_security_revalidate(struct inode *inode, · 14 unmodified lines
302286
303287 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
304288 {
305− return inode->i_security;
289+ return selinux_inode(inode);
306290 }
307291
308292 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
312296 error = __inode_security_revalidate(inode, NULL, !rcu);
313297 if (error)
314298 return ERR_PTR(error);
315− return inode->i_security;
299+ return selinux_inode(inode);
316300 }
317301
318302 /*
@@ -321,14 +305,14 @@ static struct inode_security_struct *inode_security_rcu(struct inode *inode, boo · 2 unmodified lines
321305 static struct inode_security_struct *inode_security(struct inode *inode)
322306 {
323307 __inode_security_revalidate(inode, NULL, true);
324− return inode->i_security;
308+ return selinux_inode(inode);
325309 }
326310
327311 static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
328312 {
329313 struct inode *inode = d_backing_inode(dentry);
330314
331− return inode->i_security;
315+ return selinux_inode(inode);
332316 }
333317
334318 /*
@@ -339,22 +323,17 @@ static struct inode_security_struct *backing_inode_security(struct dentry *dentr · 4 unmodified lines
339323 struct inode *inode = d_backing_inode(dentry);
340324
341325 __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);
351327 }
352328
353329 static void inode_free_security(struct inode *inode)
354330 {
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;
357333
334+ if (!isec)
335+ return;
336+ sbsec = inode->i_sb->s_security;
358337 /*
359338 * As not all inode security structures are in a list, we check for
360339 * 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
370349 list_del_init(&isec->list);
371350 spin_unlock(&sbsec->isec_lock);
372351 }
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);
384352 }
385353
386354 static int file_alloc_security(struct file *file)
387355 {
388− struct file_security_struct *fsec;
356+ struct file_security_struct *fsec = selinux_file(file);
389357 u32 sid = current_sid();
390358
391− fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
392− if (!fsec)
393− return -ENOMEM;
394−
395359 fsec->sid = sid;
396360 fsec->fown_sid = sid;
397− file->f_security = fsec;
398361
399362 return 0;
400363 }
401364
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−
409365 static int superblock_alloc_security(struct super_block *sb)
410366 {
411367 struct superblock_security_struct *sbsec;
@@ -501,7 +457,7 @@ static int may_context_mount_sb_relabel(u32 sid, · 89 unmodified lines
501457 struct superblock_security_struct *sbsec,
502458 const struct cred *cred)
503459 {
504− const struct task_security_struct *tsec = cred->security;
460+ const struct task_security_struct *tsec = selinux_cred(cred);
505461 int rc;
506462
507463 rc = avc_has_perm(&selinux_state,
@@ -520,7 +476,7 @@ static int may_context_mount_inode_relabel(u32 sid, · 12 unmodified lines
520476 struct superblock_security_struct *sbsec,
521477 const struct cred *cred)
522478 {
523− const struct task_security_struct *tsec = cred->security;
479+ const struct task_security_struct *tsec = selinux_cred(cred);
524480 int rc;
525481 rc = avc_has_perm(&selinux_state,
526482 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
@@ -1374,7 +1330,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry, · 847 unmodified lines
13741330 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
13751331 {
13761332 struct superblock_security_struct *sbsec = NULL;
1377− struct inode_security_struct *isec = inode->i_security;
1333+ struct inode_security_struct *isec = selinux_inode(inode);
13781334 u32 task_sid, sid = 0;
13791335 u16 sclass;
13801336 struct dentry *dentry;
@@ -1621,7 +1577,7 @@ static inline u32 signal_to_av(int sig) · 240 unmodified lines
16211577
16221578 /* Check whether a task is allowed to use a capability. */
16231579 static int cred_has_capability(const struct cred *cred,
1624− int cap, int audit, bool initns)
1580+ int cap, unsigned int opts, bool initns)
16251581 {
16261582 struct common_audit_data ad;
16271583 struct av_decision avd;
@@ -1648,7 +1604,7 @@ static int cred_has_capability(const struct cred *cred, · 20 unmodified lines
16481604
16491605 rc = avc_has_perm_noaudit(&selinux_state,
16501606 sid, sid, sclass, av, 0, &avd);
1651− if (audit == SECURITY_CAP_AUDIT) {
1607+ if (!(opts & CAP_OPT_NOAUDIT)) {
16521608 int rc2 = avc_audit(&selinux_state,
16531609 sid, sid, sclass, av, &avd, rc, &ad, 0);
16541610 if (rc2)
@@ -1674,7 +1630,7 @@ static int inode_has_perm(const struct cred *cred, · 19 unmodified lines
16741630 return 0;
16751631
16761632 sid = cred_sid(cred);
1677− isec = inode->i_security;
1633+ isec = selinux_inode(inode);
16781634
16791635 return avc_has_perm(&selinux_state,
16801636 sid, isec->sid, isec->sclass, perms, adp);
@@ -1740,7 +1696,7 @@ static int file_has_perm(const struct cred *cred, · 59 unmodified lines
17401696 struct file *file,
17411697 u32 av)
17421698 {
1743− struct file_security_struct *fsec = file->f_security;
1699+ struct file_security_struct *fsec = selinux_file(file);
17441700 struct inode *inode = file_inode(file);
17451701 struct common_audit_data ad;
17461702 u32 sid = cred_sid(cred);
@@ -1806,7 +1762,7 @@ static int may_create(struct inode *dir, · 59 unmodified lines
18061762 struct dentry *dentry,
18071763 u16 tclass)
18081764 {
1809− const struct task_security_struct *tsec = current_security();
1765+ const struct task_security_struct *tsec = selinux_cred(current_cred());
18101766 struct inode_security_struct *dsec;
18111767 struct superblock_security_struct *sbsec;
18121768 u32 sid, newsid;
@@ -1828,7 +1784,7 @@ static int may_create(struct inode *dir, · 15 unmodified lines
18281784 if (rc)
18291785 return rc;
18301786
1831− rc = selinux_determine_inode_label(current_security(), dir,
1787+ rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
18321788 &dentry->d_name, tclass, &newsid);
18331789 if (rc)
18341790 return rc;
@@ -2084,7 +2040,7 @@ static int selinux_binder_transfer_file(struct task_struct *from, · 249 unmodified lines
20842040 struct file *file)
20852041 {
20862042 u32 sid = task_sid(to);
2087− struct file_security_struct *fsec = file->f_security;
2043+ struct file_security_struct *fsec = selinux_file(file);
20882044 struct dentry *dentry = file->f_path.dentry;
20892045 struct inode_security_struct *isec;
20902046 struct common_audit_data ad;
@@ -2168,9 +2124,9 @@ static int selinux_capset(struct cred *new, const struct cred *old, · 77 unmodified lines
21682124 */
21692125
21702126 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2171− int cap, int audit)
2127+ int cap, unsigned int opts)
21722128 {
2173− return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
2129+ return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
21742130 }
21752131
21762132 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
22442200 int rc, cap_sys_admin = 0;
22452201
22462202 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2247− SECURITY_CAP_NOAUDIT, true);
2203+ CAP_OPT_NOAUDIT, true);
22482204 if (rc == 0)
22492205 cap_sys_admin = 1;
22502206
@@ -2335,8 +2291,8 @@ static int selinux_bprm_set_creds(struct linux_binprm *bprm) · 84 unmodified lines
23352291 if (bprm->called_set_creds)
23362292 return 0;
23372293
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);
23402296 isec = inode_security(inode);
23412297
23422298 /* Default to the current task SID. */
@@ -2500,7 +2456,7 @@ static void selinux_bprm_committing_creds(struct linux_binprm *bprm) · 157 unmodified lines
25002456 struct rlimit *rlim, *initrlim;
25012457 int rc, i;
25022458
2503− new_tsec = bprm->cred->security;
2459+ new_tsec = selinux_cred(bprm->cred);
25042460 if (new_tsec->sid == new_tsec->osid)
25052461 return;
25062462
@@ -2543,7 +2499,7 @@ static void selinux_bprm_committing_creds(struct linux_binprm *bprm) · 36 unmodified lines
25432499 */
25442500 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
25452501 {
2546− const struct task_security_struct *tsec = current_security();
2502+ const struct task_security_struct *tsec = selinux_cred(current_cred());
25472503 struct itimerval itimer;
25482504 u32 osid, sid;
25492505 int rc, i;
@@ -2780,7 +2736,7 @@ static int selinux_dentry_init_security(struct dentry *dentry, int mode, · 230 unmodified lines
27802736 u32 newsid;
27812737 int rc;
27822738
2783− rc = selinux_determine_inode_label(current_security(),
2739+ rc = selinux_determine_inode_label(selinux_cred(current_cred()),
27842740 d_inode(dentry->d_parent), name,
27852741 inode_mode_to_security_class(mode),
27862742 &newsid);
@@ -2800,14 +2756,14 @@ static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, · 13 unmodified lines
28002756 int rc;
28012757 struct task_security_struct *tsec;
28022758
2803− rc = selinux_determine_inode_label(old->security,
2759+ rc = selinux_determine_inode_label(selinux_cred(old),
28042760 d_inode(dentry->d_parent), name,
28052761 inode_mode_to_security_class(mode),
28062762 &newsid);
28072763 if (rc)
28082764 return rc;
28092765
2810− tsec = new->security;
2766+ tsec = selinux_cred(new);
28112767 tsec->create_sid = newsid;
28122768 return 0;
28132769 }
@@ -2817,7 +2773,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 3 unmodified lines
28172773 const char **name,
28182774 void **value, size_t *len)
28192775 {
2820− const struct task_security_struct *tsec = current_security();
2776+ const struct task_security_struct *tsec = selinux_cred(current_cred());
28212777 struct superblock_security_struct *sbsec;
28222778 u32 newsid, clen;
28232779 int rc;
@@ -2827,7 +2783,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 3 unmodified lines
28272783
28282784 newsid = tsec->create_sid;
28292785
2830− rc = selinux_determine_inode_label(current_security(),
2786+ rc = selinux_determine_inode_label(selinux_cred(current_cred()),
28312787 dir, qstr,
28322788 inode_mode_to_security_class(inode->i_mode),
28332789 &newsid);
@@ -2836,7 +2792,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, · 2 unmodified lines
28362792
28372793 /* Possibly defer initialization to selinux_complete_init. */
28382794 if (sbsec->flags & SE_SBINITIALIZED) {
2839− struct inode_security_struct *isec = inode->i_security;
2795+ struct inode_security_struct *isec = selinux_inode(inode);
28402796 isec->sclass = inode_mode_to_security_class(inode->i_mode);
28412797 isec->sid = newsid;
28422798 isec->initialized = LABEL_INITIALIZED;
@@ -2936,7 +2892,7 @@ static noinline int audit_inode_permission(struct inode *inode, · 93 unmodified lines
29362892 unsigned flags)
29372893 {
29382894 struct common_audit_data ad;
2939− struct inode_security_struct *isec = inode->i_security;
2895+ struct inode_security_struct *isec = selinux_inode(inode);
29402896 int rc;
29412897
29422898 ad.type = LSM_AUDIT_DATA_INODE;
@@ -3031,11 +2987,11 @@ static int selinux_inode_getattr(const struct path *path) · 88 unmodified lines
30312987 static bool has_cap_mac_admin(bool audit)
30322988 {
30332989 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;
30352991
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))
30372993 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))
30392995 return false;
30402996 return true;
30412997 }
@@ -3289,7 +3245,7 @@ static int selinux_inode_copy_up(struct dentry *src, struct cred **new) · 247 unmodified lines
32893245 return -ENOMEM;
32903246 }
32913247
3292− tsec = new_creds->security;
3248+ tsec = selinux_cred(new_creds);
32933249 /* Get label from overlay inode and set it in create_sid */
32943250 selinux_inode_getsecid(d_inode(src), &sid);
32953251 tsec->create_sid = sid;
@@ -3330,7 +3286,7 @@ static int selinux_revalidate_file_permission(struct file *file, int mask) · 34 unmodified lines
33303286 static int selinux_file_permission(struct file *file, int mask)
33313287 {
33323288 struct inode *inode = file_inode(file);
3333− struct file_security_struct *fsec = file->f_security;
3289+ struct file_security_struct *fsec = selinux_file(file);
33343290 struct inode_security_struct *isec;
33353291 u32 sid = current_sid();
33363292
@@ -3352,11 +3308,6 @@ static int selinux_file_alloc_security(struct file *file) · 15 unmodified lines
33523308 return file_alloc_security(file);
33533309 }
33543310
3355−static void selinux_file_free_security(struct file *file)
3356−{
3357− file_free_security(file);
3358−}
3359−
33603311 /*
33613312 * Check whether a task has the ioctl permission and cmd
33623313 * operation to an inode.
@@ -3365,7 +3316,7 @@ static int ioctl_has_perm(const struct cred *cred, struct file *file, · 2 unmodified lines
33653316 u32 requested, u16 cmd)
33663317 {
33673318 struct common_audit_data ad;
3368− struct file_security_struct *fsec = file->f_security;
3319+ struct file_security_struct *fsec = selinux_file(file);
33693320 struct inode *inode = file_inode(file);
33703321 struct inode_security_struct *isec;
33713322 struct lsm_ioctlop_audit ioctl;
@@ -3435,7 +3386,7 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd, · 63 unmodified lines
34353386 case KDSKBENT:
34363387 case KDSKBSENT:
34373388 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3438− SECURITY_CAP_AUDIT, true);
3389+ CAP_OPT_NONE, true);
34393390 break;
34403391
34413392 /* default case assumes that the command will go
@@ -3617,7 +3568,7 @@ static void selinux_file_set_fowner(struct file *file) · 175 unmodified lines
36173568 {
36183569 struct file_security_struct *fsec;
36193570
3620− fsec = file->f_security;
3571+ fsec = selinux_file(file);
36213572 fsec->fown_sid = current_sid();
36223573 }
36233574
@@ -3632,7 +3583,7 @@ static int selinux_file_send_sigiotask(struct task_struct *tsk, · 8 unmodified lines
36323583 /* struct fown_struct is never outside the context of a struct file */
36333584 file = container_of(fown, struct file, f_owner);
36343585
3635− fsec = file->f_security;
3586+ fsec = selinux_file(file);
36363587
36373588 if (!signum)
36383589 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
36563607 struct file_security_struct *fsec;
36573608 struct inode_security_struct *isec;
36583609
3659− fsec = file->f_security;
3610+ fsec = selinux_file(file);
36603611 isec = inode_security(file_inode(file));
36613612 /*
36623613 * Save inode label and policy sequence number
@@ -3689,53 +3640,16 @@ static int selinux_task_alloc(struct task_struct *task, · 26 unmodified lines
36893640 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
36903641 }
36913642
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−
37233643 /*
37243644 * prepare a new set of credentials for modification
37253645 */
37263646 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
37273647 gfp_t gfp)
37283648 {
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);
37333651
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;
37393653 return 0;
37403654 }
37413655
@@ -3744,8 +3658,8 @@ static int selinux_cred_prepare(struct cred *new, const struct cred *old, · 2 unmodified lines
37443658 */
37453659 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
37463660 {
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);
37493663
37503664 *tsec = *old_tsec;
37513665 }
@@ -3761,7 +3675,7 @@ static void selinux_cred_getsecid(const struct cred *c, u32 *secid) · 9 unmodified lines
37613675 */
37623676 static int selinux_kernel_act_as(struct cred *new, u32 secid)
37633677 {
3764− struct task_security_struct *tsec = new->security;
3678+ struct task_security_struct *tsec = selinux_cred(new);
37653679 u32 sid = current_sid();
37663680 int ret;
37673681
@@ -3786,7 +3700,7 @@ static int selinux_kernel_act_as(struct cred *new, u32 secid) · 18 unmodified lines
37863700 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
37873701 {
37883702 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);
37903704 u32 sid = current_sid();
37913705 int ret;
37923706
@@ -3832,7 +3746,7 @@ static int selinux_kernel_module_from_file(struct file *file) · 39 unmodified lines
38323746 ad.type = LSM_AUDIT_DATA_FILE;
38333747 ad.u.file = file;
38343748
3835− fsec = file->f_security;
3749+ fsec = selinux_file(file);
38363750 if (sid != fsec->sid) {
38373751 rc = avc_has_perm(&selinux_state,
38383752 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
39983912 static void selinux_task_to_inode(struct task_struct *p,
39993913 struct inode *inode)
40003914 {
4001− struct inode_security_struct *isec = inode->i_security;
3915+ struct inode_security_struct *isec = selinux_inode(inode);
40023916 u32 sid = task_sid(p);
40033917
40043918 spin_lock(&isec->lock);
@@ -4335,7 +4249,7 @@ static int sock_has_perm(struct sock *sk, u32 perms) · 330 unmodified lines
43354249 static int selinux_socket_create(int family, int type,
43364250 int protocol, int kern)
43374251 {
4338− const struct task_security_struct *tsec = current_security();
4252+ const struct task_security_struct *tsec = selinux_cred(current_cred());
43394253 u32 newsid;
43404254 u16 secclass;
43414255 int rc;
@@ -4355,7 +4269,7 @@ static int selinux_socket_create(int family, int type, · 13 unmodified lines
43554269 static int selinux_socket_post_create(struct socket *sock, int family,
43564270 int type, int protocol, int kern)
43574271 {
4358− const struct task_security_struct *tsec = current_security();
4272+ const struct task_security_struct *tsec = selinux_cred(current_cred());
43594273 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
43604274 struct sk_security_struct *sksec;
43614275 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
52365150 const struct task_security_struct *__tsec;
52375151 u32 tsid;
52385152
5239− __tsec = current_security();
5153+ __tsec = selinux_cred(current_cred());
52405154 tsid = __tsec->sid;
52415155
52425156 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
57115625 return selinux_nlmsg_perm(sk, skb);
57125626 }
57135627
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)
57165629 {
5717− struct ipc_security_struct *isec;
5718−
5719− isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5720− if (!isec)
5721− return -ENOMEM;
5722−
57235630 isec->sclass = sclass;
57245631 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);
57355632 }
57365633
57375634 static int msg_msg_alloc_security(struct msg_msg *msg)
57385635 {
57395636 struct msg_security_struct *msec;
57405637
5741− msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
5742− if (!msec)
5743− return -ENOMEM;
5744−
5638+ msec = selinux_msg_msg(msg);
57455639 msec->sid = SECINITSID_UNLABELED;
5746− msg->security = msec;
57475640
57485641 return 0;
57495642 }
57505643
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−
57595644 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
57605645 u32 perms)
57615646 {
@@ -5763,7 +5648,7 @@ static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, · 1 unmodified line
57635648 struct common_audit_data ad;
57645649 u32 sid = current_sid();
57655650
5766− isec = ipc_perms->security;
5651+ isec = selinux_ipc(ipc_perms);
57675652
57685653 ad.type = LSM_AUDIT_DATA_IPC;
57695654 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
57775662 return msg_msg_alloc_security(msg);
57785663 }
57795664
5780−static void selinux_msg_msg_free_security(struct msg_msg *msg)
5781−{
5782− msg_msg_free_security(msg);
5783−}
5784−
57855665 /* message queue security operations */
57865666 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
57875667 {
@@ -5790,11 +5670,8 @@ static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) · 2 unmodified lines
57905670 u32 sid = current_sid();
57915671 int rc;
57925672
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);
57985675
57995676 ad.type = LSM_AUDIT_DATA_IPC;
58005677 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
58025679 rc = avc_has_perm(&selinux_state,
58035680 sid, isec->sid, SECCLASS_MSGQ,
58045681 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;
58155683 }
58165684
58175685 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
58205688 struct common_audit_data ad;
58215689 u32 sid = current_sid();
58225690
5823− isec = msq->security;
5691+ isec = selinux_ipc(msq);
58245692
58255693 ad.type = LSM_AUDIT_DATA_IPC;
58265694 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
58695737 u32 sid = current_sid();
58705738 int rc;
58715739
5872− isec = msq->security;
5873− msec = msg->security;
5740+ isec = selinux_ipc(msq);
5741+ msec = selinux_msg_msg(msg);
58745742
58755743 /*
58765744 * 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
59175785 u32 sid = task_sid(target);
59185786 int rc;
59195787
5920− isec = msq->security;
5921− msec = msg->security;
5788+ isec = selinux_ipc(msq);
5789+ msec = selinux_msg_msg(msg);
59225790
59235791 ad.type = LSM_AUDIT_DATA_IPC;
59245792 ad.u.ipc_id = msq->key;
@@ -5941,11 +5809,8 @@ static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) · 16 unmodified lines
59415809 u32 sid = current_sid();
59425810 int rc;
59435811
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);
59495814
59505815 ad.type = LSM_AUDIT_DATA_IPC;
59515816 ad.u.ipc_id = shp->key;
@@ -5953,16 +5818,7 @@ static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) · 1 unmodified line
59535818 rc = avc_has_perm(&selinux_state,
59545819 sid, isec->sid, SECCLASS_SHM,
59555820 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;
59665822 }
59675823
59685824 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
59715827 struct common_audit_data ad;
59725828 u32 sid = current_sid();
59735829
5974− isec = shp->security;
5830+ isec = selinux_ipc(shp);
59755831
59765832 ad.type = LSM_AUDIT_DATA_IPC;
59775833 ad.u.ipc_id = shp->key;
@@ -6038,11 +5894,8 @@ static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) · 60 unmodified lines
60385894 u32 sid = current_sid();
60395895 int rc;
60405896
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);
60465899
60475900 ad.type = LSM_AUDIT_DATA_IPC;
60485901 ad.u.ipc_id = sma->key;
@@ -6050,16 +5903,7 @@ static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) · 1 unmodified line
60505903 rc = avc_has_perm(&selinux_state,
60515904 sid, isec->sid, SECCLASS_SEM,
60525905 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;
60635907 }
60645908
60655909 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
60685912 struct common_audit_data ad;
60695913 u32 sid = current_sid();
60705914
6071− isec = sma->security;
5915+ isec = selinux_ipc(sma);
60725916
60735917 ad.type = LSM_AUDIT_DATA_IPC;
60745918 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
61545998
61555999 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
61566000 {
6157− struct ipc_security_struct *isec = ipcp->security;
6001+ struct ipc_security_struct *isec = selinux_ipc(ipcp);
61586002 *secid = isec->sid;
61596003 }
61606004
@@ -6173,7 +6017,7 @@ static int selinux_getprocattr(struct task_struct *p, · 12 unmodified lines
61736017 unsigned len;
61746018
61756019 rcu_read_lock();
6176− __tsec = __task_cred(p)->security;
6020+ __tsec = selinux_cred(__task_cred(p));
61776021
61786022 if (current != p) {
61796023 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
62966140 operation. See selinux_bprm_set_creds for the execve
62976141 checks and may_create for the file creation checks. The
62986142 operation will then fail if the context is not permitted. */
6299− tsec = new->security;
6143+ tsec = selinux_cred(new);
63006144 if (!strcmp(name, "exec")) {
63016145 tsec->exec_sid = sid;
63026146 } else if (!strcmp(name, "fscreate")) {
@@ -6380,7 +6224,7 @@ static void selinux_release_secctx(char *secdata, u32 seclen) · 77 unmodified lines
63806224
63816225 static void selinux_inode_invalidate_secctx(struct inode *inode)
63826226 {
6383− struct inode_security_struct *isec = inode->i_security;
6227+ struct inode_security_struct *isec = selinux_inode(inode);
63846228
63856229 spin_lock(&isec->lock);
63866230 isec->initialized = LABEL_INVALID;
@@ -6425,7 +6269,7 @@ static int selinux_key_alloc(struct key *k, const struct cred *cred, · 38 unmodified lines
64256269 if (!ksec)
64266270 return -ENOMEM;
64276271
6428− tsec = cred->security;
6272+ tsec = selinux_cred(cred);
64296273 if (tsec->keycreate_sid)
64306274 ksec->sid = tsec->keycreate_sid;
64316275 else
@@ -6688,6 +6532,14 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) · 256 unmodified lines
66886532 }
66896533 #endif
66906534
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+
66916543 static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
66926544 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
66936545 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
67576609
67586610 LSM_HOOK_INIT(file_permission, selinux_file_permission),
67596611 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6760− LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
67616612 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
67626613 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
67636614 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
67716622 LSM_HOOK_INIT(file_open, selinux_file_open),
67726623
67736624 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),
67766625 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
67776626 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
67786627 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
68006649 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
68016650
68026651 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),
68046652
68056653 LSM_HOOK_INIT(msg_queue_alloc_security,
68066654 selinux_msg_queue_alloc_security),
6807− LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
68086655 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
68096656 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
68106657 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
68116658 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
68126659
68136660 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6814− LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
68156661 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
68166662 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
68176663 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
68186664
68196665 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6820− LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
68216666 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
68226667 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
68236668 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
69286773
69296774 static __init int selinux_init(void)
69306775 {
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−
69416776 pr_info("SELinux: Initializing.\n");
69426777
69436778 memset(&selinux_state, 0, sizeof(selinux_state));
@@ -6951,12 +6786,6 @@ static __init int selinux_init(void) · 7 unmodified lines
69516786
69526787 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
69536788
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);
69606789 avc_init();
69616790
69626791 avtab_cache_init();
@@ -6999,6 +6828,9 @@ void selinux_complete_init(void) · 36 unmodified lines
69996828 all processes and objects when they are created. */
70006829 DEFINE_LSM(selinux) = {
70016830 .name = "selinux",
6831+ .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
6832+ .enabled = &selinux_enabled,
6833+ .blobs = &selinux_blob_sizes,
70026834 .init = selinux_init,
70036835 };
70046836
security/selinux/include/audit.h+0−3
@@ -1,9 +1,6 @@
11 /*
22 * SELinux support for the Audit LSM hooks
33 *
4− * Most of below header was moved from include/linux/selinux.h which
5− * is released under below copyrights:
6− *
74 * Author: James Morris <jmorris@redhat.com>
85 *
96 * Copyright (C) 2005 Red Hat, Inc., James Morris <jmorris@redhat.com>
security/selinux/include/objsec.h+34−4
@@ -25,6 +25,8 @@ · 24 unmodified lines
2525 #include <linux/binfmts.h>
2626 #include <linux/in.h>
2727 #include <linux/spinlock.h>
28+#include <linux/lsm_hooks.h>
29+#include <linux/msg.h>
2830 #include <net/net_namespace.h>
2931 #include "flask.h"
3032 #include "avc.h"
@@ -56,10 +58,7 @@ enum label_initialized { · 25 unmodified lines
5658
5759 struct inode_security_struct {
5860 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 */
6362 u32 task_sid; /* SID of creating task */
6463 u32 sid; /* SID of this object */
6564 u16 sclass; /* security class of this object */
@@ -158,4 +157,35 @@ struct bpf_security_struct { · 92 unmodified lines
158157 u32 sid; /*SID of bpf obj creater*/
159158 };
160159
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+
161191 #endif /* _SELINUX_OBJSEC_H_ */
security/selinux/selinuxfs.c+2−2
@@ -1378,7 +1378,7 @@ static int sel_make_bools(struct selinux_fs_info *fsi) · 1377 unmodified lines
13781378 goto out;
13791379 }
13801380
1381− isec = (struct inode_security_struct *)inode->i_security;
1381+ isec = selinux_inode(inode);
13821382 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
13831383 SECCLASS_FILE, &sid);
13841384 if (ret) {
@@ -1953,7 +1953,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) · 568 unmodified lines
19531953 }
19541954
19551955 inode->i_ino = ++fsi->last_ino;
1956− isec = (struct inode_security_struct *)inode->i_security;
1956+ isec = selinux_inode(inode);
19571957 isec->sid = SECINITSID_DEVNULL;
19581958 isec->sclass = SECCLASS_CHR_FILE;
19591959 isec->initialized = LABEL_INITIALIZED;
security/selinux/ss/services.c+0−1
@@ -49,7 +49,6 @@ · 48 unmodified lines
4949 #include <linux/sched.h>
5050 #include <linux/audit.h>
5151 #include <linux/mutex.h>
52−#include <linux/selinux.h>
5352 #include <linux/flex_array.h>
5453 #include <linux/vmalloc.h>
5554 #include <net/netlabel.h>
security/selinux/xfrm.c+2−2
@@ -79,7 +79,7 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp, · 78 unmodified lines
7979 gfp_t gfp)
8080 {
8181 int rc;
82− const struct task_security_struct *tsec = current_security();
82+ const struct task_security_struct *tsec = selinux_cred(current_cred());
8383 struct xfrm_sec_ctx *ctx = NULL;
8484 u32 str_len;
8585
@@ -138,7 +138,7 @@ static void selinux_xfrm_free(struct xfrm_sec_ctx *ctx) · 52 unmodified lines
138138 */
139139 static int selinux_xfrm_delete(struct xfrm_sec_ctx *ctx)
140140 {
141− const struct task_security_struct *tsec = current_security();
141+ const struct task_security_struct *tsec = selinux_cred(current_cred());
142142
143143 if (!ctx)
144144 return 0;
security/smack/smack.h+39−5
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 #include <linux/list.h>
2525 #include <linux/rculist.h>
2626 #include <linux/lsm_audit.h>
27+#include <linux/msg.h>
2728
2829 /*
2930 * Use IPv6 port labeling if IPv6 is enabled and secmarks
@@ -336,6 +337,7 @@ extern struct smack_known *smack_syslog_label; · 306 unmodified lines
336337 extern struct smack_known *smack_unconfined;
337338 #endif
338339 extern int smack_ptrace_rule;
340+extern struct lsm_blob_sizes smack_blob_sizes;
339341
340342 extern struct smack_known smack_known_floor;
341343 extern struct smack_known smack_known_hat;
@@ -356,12 +358,38 @@ extern struct list_head smack_onlycap_list; · 14 unmodified lines
356358 #define SMACK_HASH_SLOTS 16
357359 extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
358360
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+
359387 /*
360388 * Is the directory transmuting?
361389 */
362390 static inline int smk_inode_transmutable(const struct inode *isp)
363391 {
364− struct inode_smack *sip = isp->i_security;
392+ struct inode_smack *sip = smack_inode(isp);
365393 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
366394 }
367395
@@ -370,7 +398,7 @@ static inline int smk_inode_transmutable(const struct inode *isp) · 2 unmodified lines
370398 */
371399 static inline struct smack_known *smk_of_inode(const struct inode *isp)
372400 {
373− struct inode_smack *sip = isp->i_security;
401+ struct inode_smack *sip = smack_inode(isp);
374402 return sip->smk_inode;
375403 }
376404
@@ -382,13 +410,19 @@ static inline struct smack_known *smk_of_task(const struct task_smack *tsp) · 5 unmodified lines
382410 return tsp->smk_task;
383411 }
384412
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)
386415 {
387416 struct smack_known *skp;
417+ const struct cred *cred;
388418
389419 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+
391424 rcu_read_unlock();
425+
392426 return skp;
393427 }
394428
@@ -405,7 +439,7 @@ static inline struct smack_known *smk_of_forked(const struct task_smack *tsp) · 10 unmodified lines
405439 */
406440 static inline struct smack_known *smk_of_current(void)
407441 {
408− return smk_of_task(current_security());
442+ return smk_of_task(smack_cred(current_cred()));
409443 }
410444
411445 /*
security/smack/smack_access.c+3−3
@@ -275,7 +275,7 @@ out_audit: · 274 unmodified lines
275275 int smk_curacc(struct smack_known *obj_known,
276276 u32 mode, struct smk_audit_info *a)
277277 {
278− struct task_smack *tsp = current_security();
278+ struct task_smack *tsp = smack_cred(current_cred());
279279
280280 return smk_tskacc(tsp, obj_known, mode, a);
281281 }
@@ -635,12 +635,12 @@ DEFINE_MUTEX(smack_onlycap_lock); · 353 unmodified lines
635635 */
636636 bool smack_privileged_cred(int cap, const struct cred *cred)
637637 {
638− struct task_smack *tsp = cred->security;
638+ struct task_smack *tsp = smack_cred(cred);
639639 struct smack_known *skp = tsp->smk_task;
640640 struct smack_known_list_elem *sklep;
641641 int rc;
642642
643− rc = cap_capable(cred, &init_user_ns, cap, SECURITY_CAP_AUDIT);
643+ rc = cap_capable(cred, &init_user_ns, cap, CAP_OPT_NONE);
644644 if (rc)
645645 return false;
646646
security/smack/smack_lsm.c+106−211
@@ -139,7 +139,7 @@ static int smk_bu_note(char *note, struct smack_known *sskp, · 138 unmodified lines
139139 static int smk_bu_current(char *note, struct smack_known *oskp,
140140 int mode, int rc)
141141 {
142− struct task_smack *tsp = current_security();
142+ struct task_smack *tsp = smack_cred(current_cred());
143143 char acc[SMK_NUM_ACCESS_TYPE + 1];
144144
145145 if (rc <= 0)
@@ -160,7 +160,7 @@ static int smk_bu_current(char *note, struct smack_known *oskp, · 14 unmodified lines
160160 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
161161 static int smk_bu_task(struct task_struct *otp, int mode, int rc)
162162 {
163− struct task_smack *tsp = current_security();
163+ struct task_smack *tsp = smack_cred(current_cred());
164164 struct smack_known *smk_task = smk_of_task_struct(otp);
165165 char acc[SMK_NUM_ACCESS_TYPE + 1];
166166
@@ -182,8 +182,8 @@ static int smk_bu_task(struct task_struct *otp, int mode, int rc) · 15 unmodified lines
182182 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
183183 static int smk_bu_inode(struct inode *inode, int mode, int rc)
184184 {
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);
187187 char acc[SMK_NUM_ACCESS_TYPE + 1];
188188
189189 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
212212 #ifdef CONFIG_SECURITY_SMACK_BRINGUP
213213 static int smk_bu_file(struct file *file, int mode, int rc)
214214 {
215− struct task_smack *tsp = current_security();
215+ struct task_smack *tsp = smack_cred(current_cred());
216216 struct smack_known *sskp = tsp->smk_task;
217217 struct inode *inode = file_inode(file);
218− struct inode_smack *isp = inode->i_security;
218+ struct inode_smack *isp = smack_inode(inode);
219219 char acc[SMK_NUM_ACCESS_TYPE + 1];
220220
221221 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
242242 static int smk_bu_credfile(const struct cred *cred, struct file *file,
243243 int mode, int rc)
244244 {
245− struct task_smack *tsp = cred->security;
245+ struct task_smack *tsp = smack_cred(cred);
246246 struct smack_known *sskp = tsp->smk_task;
247247 struct inode *inode = file_inode(file);
248− struct inode_smack *isp = inode->i_security;
248+ struct inode_smack *isp = smack_inode(inode);
249249 char acc[SMK_NUM_ACCESS_TYPE + 1];
250250
251251 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
305305 }
306306
307307 /**
308− * new_inode_smack - allocate an inode security blob
308+ * init_inode_smack - initialize an inode security blob
309+ * @isp: the blob to initialize
309310 * @skp: a pointer to the Smack label entry to use in the blob
310311 *
311− * Returns the new blob or NULL if there's no memory available
312312 */
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)
314314 {
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);
320316
321317 isp->smk_inode = skp;
322318 isp->smk_flags = 0;
323319 mutex_init(&isp->smk_lock);
324−
325− return isp;
326320 }
327321
328322 /**
329− * new_task_smack - allocate a task security blob
323+ * init_task_smack - initialize a task security blob
324+ * @tsp: blob to initialize
330325 * @task: a pointer to the Smack label for the running task
331326 * @forked: a pointer to the Smack label for the forked task
332− * @gfp: type of the memory for the allocation
333327 *
334− * Returns the new blob or NULL if there's no memory available
335328 */
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)
338331 {
339− struct task_smack *tsp;
340−
341− tsp = kzalloc(sizeof(struct task_smack), gfp);
342− if (tsp == NULL)
343− return NULL;
344−
345332 tsp->smk_task = task;
346333 tsp->smk_forked = forked;
347334 INIT_LIST_HEAD(&tsp->smk_rules);
348335 INIT_LIST_HEAD(&tsp->smk_relabel);
349336 mutex_init(&tsp->smk_rules_lock);
350−
351− return tsp;
352337 }
353338
354339 /**
@@ -448,7 +433,7 @@ static int smk_ptrace_rule_check(struct task_struct *tracer, · 93 unmodified lines
448433
449434 rcu_read_lock();
450435 tracercred = __task_cred(tracer);
451− tsp = tracercred->security;
436+ tsp = smack_cred(tracercred);
452437 tracer_known = smk_of_task(tsp);
453438
454439 if ((mode & PTRACE_MODE_ATTACH) &&
@@ -515,7 +500,7 @@ static int smack_ptrace_traceme(struct task_struct *ptp) · 60 unmodified lines
515500 int rc;
516501 struct smack_known *skp;
517502
518− skp = smk_of_task(current_security());
503+ skp = smk_of_task(smack_cred(current_cred()));
519504
520505 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
521506 return rc;
@@ -718,6 +703,13 @@ static int smack_set_mnt_opts(struct super_block *sb, · 196 unmodified lines
718703 if (sp->smk_flags & SMK_SB_INITIALIZED)
719704 return 0;
720705
706+ if (inode->i_security == NULL) {
707+ int rc = lsm_inode_alloc(inode);
708+
709+ if (rc)
710+ return rc;
711+ }
712+
721713 if (!smack_privileged(CAP_MAC_ADMIN)) {
722714 /*
723715 * 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
782774 /*
783775 * Initialize the root inode.
784776 */
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);
793778
794− if (transmute)
779+ if (transmute) {
780+ isp = smack_inode(inode);
795781 isp->smk_flags |= SMK_INODE_TRANSMUTE;
782+ }
796783
797784 return 0;
798785 }
@@ -831,7 +818,7 @@ static int smack_sb_statfs(struct dentry *dentry) · 32 unmodified lines
831818 static int smack_bprm_set_creds(struct linux_binprm *bprm)
832819 {
833820 struct inode *inode = file_inode(bprm->file);
834− struct task_smack *bsp = bprm->cred->security;
821+ struct task_smack *bsp = smack_cred(bprm->cred);
835822 struct inode_smack *isp;
836823 struct superblock_smack *sbsp;
837824 int rc;
@@ -839,7 +826,7 @@ static int smack_bprm_set_creds(struct linux_binprm *bprm) · 1 unmodified line
839826 if (bprm->called_set_creds)
840827 return 0;
841828
842− isp = inode->i_security;
829+ isp = smack_inode(inode);
843830 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
844831 return 0;
845832
@@ -890,48 +877,10 @@ static int smack_inode_alloc_security(struct inode *inode) · 44 unmodified lines
890877 {
891878 struct smack_known *skp = smk_of_current();
892879
893− inode->i_security = new_inode_smack(skp);
894− if (inode->i_security == NULL)
895− return -ENOMEM;
880+ init_inode_smack(inode, skp);
896881 return 0;
897882 }
898883
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−
935884 /**
936885 * smack_inode_init_security - copy out the smack from an inode
937886 * @inode: the newly created inode
@@ -947,7 +896,7 @@ static int smack_inode_init_security(struct inode *inode, struct inode *dir, · 9 unmodified lines
947896 const struct qstr *qstr, const char **name,
948897 void **value, size_t *len)
949898 {
950− struct inode_smack *issp = inode->i_security;
899+ struct inode_smack *issp = smack_inode(inode);
951900 struct smack_known *skp = smk_of_current();
952901 struct smack_known *isp = smk_of_inode(inode);
953902 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
12851234 const void *value, size_t size, int flags)
12861235 {
12871236 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));
12891238
12901239 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
12911240 isp->smk_flags |= SMK_INODE_TRANSMUTE;
@@ -1366,7 +1315,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name) · 74 unmodified lines
13661315 if (rc != 0)
13671316 return rc;
13681317
1369− isp = d_backing_inode(dentry)->i_security;
1318+ isp = smack_inode(d_backing_inode(dentry));
13701319 /*
13711320 * Don't do anything special for these.
13721321 * XATTR_NAME_SMACKIPIN
@@ -1498,24 +1447,12 @@ static void smack_inode_getsecid(struct inode *inode, u32 *secid) · 125 unmodified lines
14981447 */
14991448 static int smack_file_alloc_security(struct file *file)
15001449 {
1501− struct smack_known *skp = smk_of_current();
1450+ struct smack_known **blob = smack_file(file);
15021451
1503− file->f_security = skp;
1452+ *blob = smk_of_current();
15041453 return 0;
15051454 }
15061455
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−
15191456 /**
15201457 * smack_file_ioctl - Smack check on ioctls
15211458 * @file: the object
@@ -1653,7 +1590,7 @@ static int smack_mmap_file(struct file *file, · 131 unmodified lines
16531590 if (unlikely(IS_PRIVATE(file_inode(file))))
16541591 return 0;
16551592
1656− isp = file_inode(file)->i_security;
1593+ isp = smack_inode(file_inode(file));
16571594 if (isp->smk_mmap == NULL)
16581595 return 0;
16591596 sbsp = file_inode(file)->i_sb->s_security;
@@ -1662,7 +1599,7 @@ static int smack_mmap_file(struct file *file, · 2 unmodified lines
16621599 return -EACCES;
16631600 mkp = isp->smk_mmap;
16641601
1665− tsp = current_security();
1602+ tsp = smack_cred(current_cred());
16661603 skp = smk_of_current();
16671604 rc = 0;
16681605
@@ -1740,7 +1677,9 @@ static int smack_mmap_file(struct file *file, · 71 unmodified lines
17401677 */
17411678 static void smack_file_set_fowner(struct file *file)
17421679 {
1743− file->f_security = smk_of_current();
1680+ struct smack_known **blob = smack_file(file);
1681+
1682+ *blob = smk_of_current();
17441683 }
17451684
17461685 /**
@@ -1757,8 +1696,9 @@ static void smack_file_set_fowner(struct file *file) · 10 unmodified lines
17571696 static int smack_file_send_sigiotask(struct task_struct *tsk,
17581697 struct fown_struct *fown, int signum)
17591698 {
1699+ struct smack_known **blob;
17601700 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));
17621702 const struct cred *tcred;
17631703 struct file *file;
17641704 int rc;
@@ -1770,7 +1710,8 @@ static int smack_file_send_sigiotask(struct task_struct *tsk, · 5 unmodified lines
17701710 file = container_of(fown, struct file, f_owner);
17711711
17721712 /* we don't log here as rc can be overriden */
1773− skp = file->f_security;
1713+ blob = smack_file(file);
1714+ skp = *blob;
17741715 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
17751716 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
17761717
@@ -1811,7 +1752,7 @@ static int smack_file_receive(struct file *file) · 34 unmodified lines
18111752 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
18121753 sock = SOCKET_I(inode);
18131754 ssp = sock->sk->sk_security;
1814− tsp = current_security();
1755+ tsp = smack_cred(current_cred());
18151756 /*
18161757 * If the receiving process can't write to the
18171758 * passed socket or if the passed socket can't
@@ -1853,7 +1794,7 @@ static int smack_file_receive(struct file *file) · 35 unmodified lines
18531794 */
18541795 static int smack_file_open(struct file *file)
18551796 {
1856− struct task_smack *tsp = file->f_cred->security;
1797+ struct task_smack *tsp = smack_cred(file->f_cred);
18571798 struct inode *inode = file_inode(file);
18581799 struct smk_audit_info ad;
18591800 int rc;
@@ -1881,14 +1822,7 @@ static int smack_file_open(struct file *file) · 21 unmodified lines
18811822 */
18821823 static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
18831824 {
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);
18921826 return 0;
18931827 }
18941828
@@ -1900,15 +1834,11 @@ static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) · 5 unmodified lines
19001834 */
19011835 static void smack_cred_free(struct cred *cred)
19021836 {
1903− struct task_smack *tsp = cred->security;
1837+ struct task_smack *tsp = smack_cred(cred);
19041838 struct smack_rule *rp;
19051839 struct list_head *l;
19061840 struct list_head *n;
19071841
1908− if (tsp == NULL)
1909− return;
1910− cred->security = NULL;
1911−
19121842 smk_destroy_label_list(&tsp->smk_relabel);
19131843
19141844 list_for_each_safe(l, n, &tsp->smk_rules) {
@@ -1916,7 +1846,6 @@ static void smack_cred_free(struct cred *cred) · 1 unmodified line
19161846 list_del(&rp->list);
19171847 kfree(rp);
19181848 }
1919− kfree(tsp);
19201849 }
19211850
19221851 /**
@@ -1930,15 +1859,11 @@ static void smack_cred_free(struct cred *cred) · 7 unmodified lines
19301859 static int smack_cred_prepare(struct cred *new, const struct cred *old,
19311860 gfp_t gfp)
19321861 {
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);
19351864 int rc;
19361865
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);
19421867
19431868 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
19441869 if (rc != 0)
@@ -1946,10 +1871,7 @@ static int smack_cred_prepare(struct cred *new, const struct cred *old, · 1 unmodified line
19461871
19471872 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
19481873 gfp);
1949− if (rc != 0)
1950− return rc;
1951−
1952− return 0;
1874+ return rc;
19531875 }
19541876
19551877 /**
@@ -1961,15 +1883,14 @@ static int smack_cred_prepare(struct cred *new, const struct cred *old, · 5 unmodified lines
19611883 */
19621884 static void smack_cred_transfer(struct cred *new, const struct cred *old)
19631885 {
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);
19661888
19671889 new_tsp->smk_task = old_tsp->smk_task;
19681890 new_tsp->smk_forked = old_tsp->smk_task;
19691891 mutex_init(&new_tsp->smk_rules_lock);
19701892 INIT_LIST_HEAD(&new_tsp->smk_rules);
19711893
1972−
19731894 /* cbs copy rule list */
19741895 }
19751896
@@ -1980,12 +1901,12 @@ static void smack_cred_transfer(struct cred *new, const struct cred *old) · 4 unmodified lines
19801901 *
19811902 * Sets the secid to contain a u32 version of the smack label.
19821903 */
1983−static void smack_cred_getsecid(const struct cred *c, u32 *secid)
1904+static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
19841905 {
19851906 struct smack_known *skp;
19861907
19871908 rcu_read_lock();
1988− skp = smk_of_task(c->security);
1909+ skp = smk_of_task(smack_cred(cred));
19891910 *secid = skp->smk_secid;
19901911 rcu_read_unlock();
19911912 }
@@ -1999,7 +1920,7 @@ static void smack_cred_getsecid(const struct cred *c, u32 *secid) · 7 unmodified lines
19991920 */
20001921 static int smack_kernel_act_as(struct cred *new, u32 secid)
20011922 {
2002− struct task_smack *new_tsp = new->security;
1923+ struct task_smack *new_tsp = smack_cred(new);
20031924
20041925 new_tsp->smk_task = smack_from_secid(secid);
20051926 return 0;
@@ -2016,8 +1937,8 @@ static int smack_kernel_act_as(struct cred *new, u32 secid) · 10 unmodified lines
20161937 static int smack_kernel_create_files_as(struct cred *new,
20171938 struct inode *inode)
20181939 {
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);
20211942
20221943 tsp->smk_forked = isp->smk_inode;
20231944 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
22012122 * specific behavior. This is not clean. For one thing
22022123 * we can't take privilege into account.
22032124 */
2204− skp = smk_of_task(cred->security);
2125+ skp = smk_of_task(smack_cred(cred));
22052126 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
22062127 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
22072128 return rc;
@@ -2216,7 +2137,7 @@ static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info, · 8 unmodified lines
22162137 */
22172138 static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
22182139 {
2219− struct inode_smack *isp = inode->i_security;
2140+ struct inode_smack *isp = smack_inode(inode);
22202141 struct smack_known *skp = smk_of_task_struct(p);
22212142
22222143 isp->smk_inode = skp;
@@ -2679,7 +2600,7 @@ static int smack_inode_setsecurity(struct inode *inode, const char *name, · 456 unmodified lines
26792600 const void *value, size_t size, int flags)
26802601 {
26812602 struct smack_known *skp;
2682− struct inode_smack *nsp = inode->i_security;
2603+ struct inode_smack *nsp = smack_inode(inode);
26832604 struct socket_smack *ssp;
26842605 struct socket *sock;
26852606 int rc = 0;
@@ -2888,23 +2809,12 @@ static int smack_flags_to_may(int flags) · 202 unmodified lines
28882809 */
28892810 static int smack_msg_msg_alloc_security(struct msg_msg *msg)
28902811 {
2891− struct smack_known *skp = smk_of_current();
2812+ struct smack_known **blob = smack_msg_msg(msg);
28922813
2893− msg->security = skp;
2814+ *blob = smk_of_current();
28942815 return 0;
28952816 }
28962817
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−
29082818 /**
29092819 * smack_of_ipc - the smack pointer for the ipc
29102820 * @isp: the object
@@ -2913,7 +2823,9 @@ static void smack_msg_msg_free_security(struct msg_msg *msg) · 2 unmodified lines
29132823 */
29142824 static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
29152825 {
2916− return (struct smack_known *)isp->security;
2826+ struct smack_known **blob = smack_ipc(isp);
2827+
2828+ return *blob;
29172829 }
29182830
29192831 /**
@@ -2924,23 +2836,12 @@ static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp) · 4 unmodified lines
29242836 */
29252837 static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
29262838 {
2927− struct smack_known *skp = smk_of_current();
2839+ struct smack_known **blob = smack_ipc(isp);
29282840
2929− isp->security = skp;
2841+ *blob = smk_of_current();
29302842 return 0;
29312843 }
29322844
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−
29442845 /**
29452846 * smk_curacc_shm : check if current has access on shm
29462847 * @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
32383139 */
32393140 static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
32403141 {
3241− struct smack_known *iskp = ipp->security;
3142+ struct smack_known **blob = smack_ipc(ipp);
3143+ struct smack_known *iskp = *blob;
32423144 int may = smack_flags_to_may(flag);
32433145 struct smk_audit_info ad;
32443146 int rc;
@@ -3259,7 +3161,8 @@ static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) · 14 unmodified lines
32593161 */
32603162 static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
32613163 {
3262− struct smack_known *iskp = ipp->security;
3164+ struct smack_known **blob = smack_ipc(ipp);
3165+ struct smack_known *iskp = *blob;
32633166
32643167 *secid = iskp->smk_secid;
32653168 }
@@ -3287,7 +3190,7 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) · 21 unmodified lines
32873190 if (inode == NULL)
32883191 return;
32893192
3290− isp = inode->i_security;
3193+ isp = smack_inode(inode);
32913194
32923195 mutex_lock(&isp->smk_lock);
32933196 /*
@@ -3390,13 +3293,12 @@ static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) · 96 unmodified lines
33903293 */
33913294 final = &smack_known_star;
33923295 /*
3393− * Fall through.
3394− *
33953296 * If a smack value has been set we want to use it,
33963297 * but since tmpfs isn't giving us the opportunity
33973298 * to set mount options simulate setting the
33983299 * superblock default.
33993300 */
3301+ /* Fall through */
34003302 default:
34013303 /*
34023304 * 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
35283430 */
35293431 static int smack_setprocattr(const char *name, void *value, size_t size)
35303432 {
3531− struct task_smack *tsp = current_security();
3433+ struct task_smack *tsp = smack_cred(current_cred());
35323434 struct cred *new;
35333435 struct smack_known *skp;
35343436 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
35693471 if (new == NULL)
35703472 return -ENOMEM;
35713473
3572− tsp = new->security;
3474+ tsp = smack_cred(new);
35733475 tsp->smk_task = skp;
35743476 /*
35753477 * process can change its label only once
@@ -4214,7 +4116,7 @@ static void smack_inet_csk_clone(struct sock *sk, · 638 unmodified lines
42144116 static int smack_key_alloc(struct key *key, const struct cred *cred,
42154117 unsigned long flags)
42164118 {
4217− struct smack_known *skp = smk_of_task(cred->security);
4119+ struct smack_known *skp = smk_of_task(smack_cred(cred));
42184120
42194121 key->security = skp;
42204122 return 0;
@@ -4245,7 +4147,7 @@ static int smack_key_permission(key_ref_t key_ref, · 24 unmodified lines
42454147 {
42464148 struct key *keyp;
42474149 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));
42494151 int request = 0;
42504152 int rc;
42514153
@@ -4520,12 +4422,12 @@ static int smack_inode_copy_up(struct dentry *dentry, struct cred **new) · 268 unmodified lines
45204422 return -ENOMEM;
45214423 }
45224424
4523− tsp = new_creds->security;
4425+ tsp = smack_cred(new_creds);
45244426
45254427 /*
45264428 * Get label from overlay inode and set it in create_sid
45274429 */
4528− isp = d_inode(dentry->d_parent)->i_security;
4430+ isp = smack_inode(d_inode(dentry->d_parent));
45294431 skp = isp->smk_inode;
45304432 tsp->smk_task = skp;
45314433 *new = new_creds;
@@ -4548,8 +4450,8 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode, · 16 unmodified lines
45484450 const struct cred *old,
45494451 struct cred *new)
45504452 {
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);
45534455 struct inode_smack *isp;
45544456 int may;
45554457
@@ -4562,7 +4464,7 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode, · 6 unmodified lines
45624464 /*
45634465 * the attribute of the containing directory
45644466 */
4565− isp = d_inode(dentry->d_parent)->i_security;
4467+ isp = smack_inode(d_inode(dentry->d_parent));
45664468
45674469 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
45684470 rcu_read_lock();
@@ -4582,6 +4484,14 @@ static int smack_dentry_create_files_as(struct dentry *dentry, int mode, · 13 unmodified lines
45824484 return 0;
45834485 }
45844486
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+
45854495 static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
45864496 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
45874497 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
45974507 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
45984508
45994509 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4600− LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
46014510 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
46024511 LSM_HOOK_INIT(inode_link, smack_inode_link),
46034512 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
46164525 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
46174526
46184527 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4619− LSM_HOOK_INIT(file_free_security, smack_file_free_security),
46204528 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
46214529 LSM_HOOK_INIT(file_lock, smack_file_lock),
46224530 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
46524560 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
46534561
46544562 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),
46564563
46574564 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4658− LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
46594565 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
46604566 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
46614567 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
46624568 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
46634569
46644570 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4665− LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
46664571 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
46674572 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
46684573 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
46694574
46704575 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4671− LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
46724576 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
46734577 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
46744578 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
@@ -4759,23 +4663,23 @@ static __init void init_smack_known_list(void) · 84 unmodified lines
47594663 */
47604664 static __init int smack_init(void)
47614665 {
4762− struct cred *cred;
4666+ struct cred *cred = (struct cred *) current->cred;
47634667 struct task_smack *tsp;
47644668
4765− if (!security_module_enable("smack"))
4766− return 0;
4767−
47684669 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
47694670 if (!smack_inode_cache)
47704671 return -ENOMEM;
47714672
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);
47784678
4679+ /*
4680+ * Register with LSM
4681+ */
4682+ security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
47794683 smack_enabled = 1;
47804684
47814685 pr_info("Smack: Initializing.\n");
@@ -4789,20 +4693,9 @@ static __init int smack_init(void) · 7 unmodified lines
47894693 pr_info("Smack: IPv6 Netfilter enabled.\n");
47904694 #endif
47914695
4792− /*
4793− * Set the security state for the initial task.
4794− */
4795− cred = (struct cred *) current->cred;
4796− cred->security = tsp;
4797−
47984696 /* initialize the smack_known_list */
47994697 init_smack_known_list();
48004698
4801− /*
4802− * Register with LSM
4803− */
4804− security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4805−
48064699 return 0;
48074700 }
48084701
@@ -4812,5 +4705,7 @@ static __init int smack_init(void) · 3 unmodified lines
48124705 */
48134706 DEFINE_LSM(smack) = {
48144707 .name = "smack",
4708+ .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
4709+ .blobs = &smack_blob_sizes,
48154710 .init = smack_init,
48164711 };
security/smack/smackfs.c+9−9
@@ -2208,14 +2208,14 @@ static const struct file_operations smk_logging_ops = { · 2207 unmodified lines
22082208
22092209 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
22102210 {
2211− struct task_smack *tsp = current_security();
2211+ struct task_smack *tsp = smack_cred(current_cred());
22122212
22132213 return smk_seq_start(s, pos, &tsp->smk_rules);
22142214 }
22152215
22162216 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
22172217 {
2218− struct task_smack *tsp = current_security();
2218+ struct task_smack *tsp = smack_cred(current_cred());
22192219
22202220 return smk_seq_next(s, v, pos, &tsp->smk_rules);
22212221 }
@@ -2262,7 +2262,7 @@ static int smk_open_load_self(struct inode *inode, struct file *file) · 40 unmodified lines
22622262 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
22632263 size_t count, loff_t *ppos)
22642264 {
2265− struct task_smack *tsp = current_security();
2265+ struct task_smack *tsp = smack_cred(current_cred());
22662266
22672267 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
22682268 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
@@ -2414,14 +2414,14 @@ static const struct file_operations smk_load2_ops = { · 145 unmodified lines
24142414
24152415 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
24162416 {
2417− struct task_smack *tsp = current_security();
2417+ struct task_smack *tsp = smack_cred(current_cred());
24182418
24192419 return smk_seq_start(s, pos, &tsp->smk_rules);
24202420 }
24212421
24222422 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
24232423 {
2424− struct task_smack *tsp = current_security();
2424+ struct task_smack *tsp = smack_cred(current_cred());
24252425
24262426 return smk_seq_next(s, v, pos, &tsp->smk_rules);
24272427 }
@@ -2467,7 +2467,7 @@ static int smk_open_load_self2(struct inode *inode, struct file *file) · 39 unmodified lines
24672467 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
24682468 size_t count, loff_t *ppos)
24692469 {
2470− struct task_smack *tsp = current_security();
2470+ struct task_smack *tsp = smack_cred(current_cred());
24712471
24722472 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
24732473 &tsp->smk_rules_lock, SMK_LONG_FMT);
@@ -2681,14 +2681,14 @@ static const struct file_operations smk_syslog_ops = { · 207 unmodified lines
26812681
26822682 static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
26832683 {
2684− struct task_smack *tsp = current_security();
2684+ struct task_smack *tsp = smack_cred(current_cred());
26852685
26862686 return smk_seq_start(s, pos, &tsp->smk_relabel);
26872687 }
26882688
26892689 static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
26902690 {
2691− struct task_smack *tsp = current_security();
2691+ struct task_smack *tsp = smack_cred(current_cred());
26922692
26932693 return smk_seq_next(s, v, pos, &tsp->smk_relabel);
26942694 }
@@ -2736,7 +2736,7 @@ static int smk_open_relabel_self(struct inode *inode, struct file *file) · 41 unmodified lines
27362736 static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
27372737 size_t count, loff_t *ppos)
27382738 {
2739− struct task_smack *tsp = current_security();
2739+ struct task_smack *tsp = smack_cred(current_cred());
27402740 char *data;
27412741 int rc;
27422742 LIST_HEAD(list_tmp);
security/tomoyo/audit.c+20−11
@@ -32,6 +32,7 @@ static char *tomoyo_print_bprm(struct linux_binprm *bprm, · 31 unmodified lines
3232 int argv_count = bprm->argc;
3333 int envp_count = bprm->envc;
3434 bool truncated = false;
35+
3536 if (!buffer)
3637 return NULL;
3738 len = snprintf(buffer, tomoyo_buffer_len - 1, "argv[]={ ");
@@ -49,6 +50,7 @@ static char *tomoyo_print_bprm(struct linux_binprm *bprm, · 11 unmodified lines
4950 while (offset < PAGE_SIZE) {
5051 const char *kaddr = dump->data;
5152 const unsigned char c = kaddr[offset++];
53+
5254 if (cp == last_start)
5355 *cp++ = '"';
5456 if (cp >= buffer + tomoyo_buffer_len - 32) {
@@ -154,19 +156,18 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 99 unmodified lines
154156 char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
155157 int pos;
156158 u8 i;
159+
157160 if (!buffer)
158161 return NULL;
159162
160163 tomoyo_convert_time(ktime_get_real_seconds(), &stamp);
161164
162165 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(),
170171 from_kuid(&init_user_ns, current_uid()),
171172 from_kgid(&init_user_ns, current_gid()),
172173 from_kuid(&init_user_ns, current_euid()),
@@ -185,6 +186,7 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 12 unmodified lines
185186 struct tomoyo_mini_stat *stat;
186187 unsigned int dev;
187188 umode_t mode;
189+
188190 if (!obj->stat_valid[i])
189191 continue;
190192 stat = &obj->stat[i];
@@ -193,8 +195,8 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 2 unmodified lines
193195 if (i & 1) {
194196 pos += snprintf(buffer + pos,
195197 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,
198200 from_kuid(&init_user_ns, stat->uid),
199201 from_kgid(&init_user_ns, stat->gid),
200202 (unsigned long)stat->ino,
@@ -202,8 +204,8 @@ static char *tomoyo_print_header(struct tomoyo_request_info *r) · 1 unmodified line
202204 continue;
203205 }
204206 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,
207209 from_kuid(&init_user_ns, stat->uid),
208210 from_kgid(&init_user_ns, stat->gid),
209211 (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
249251 const char *symlink = NULL;
250252 int pos;
251253 const char *domainname = r->domain->domainname->name;
254+
252255 header = tomoyo_print_header(r);
253256 if (!header)
254257 return NULL;
@@ -256,6 +259,7 @@ char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt, · 1 unmodified line
256259 len += strlen(domainname) + strlen(header) + 10;
257260 if (r->ee) {
258261 struct file *file = r->ee->bprm->file;
262+
259263 realpath = tomoyo_realpath_from_path(&file->f_path);
260264 bprm_info = tomoyo_print_bprm(r->ee->bprm, &r->ee->dump);
261265 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
275279 pos = snprintf(buf, len, "%s", header);
276280 if (realpath) {
277281 struct linux_binprm *bprm = r->ee->bprm;
282+
278283 pos += snprintf(buf + pos, len - pos,
279284 " exec={ realpath=\"%s\" argc=%d envc=%d %s }",
280285 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
328333 const u8 category = tomoyo_index2category[index] +
329334 TOMOYO_MAX_MAC_INDEX;
330335 struct tomoyo_profile *p;
336+
331337 if (!tomoyo_policy_loaded)
332338 return false;
333339 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
362368 char *buf;
363369 struct tomoyo_log *entry;
364370 bool quota_exceeded = false;
371+
365372 if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type,
366373 r->matched_acl, r->granted))
367374 goto out;
@@ -413,6 +420,7 @@ void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...) · 45 unmodified lines
413420 {
414421 va_list args;
415422 int len;
423+
416424 va_start(args, fmt);
417425 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
418426 va_end(args);
@@ -431,6 +439,7 @@ void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...) · 12 unmodified lines
431439 void tomoyo_read_log(struct tomoyo_io_buffer *head)
432440 {
433441 struct tomoyo_log *ptr = NULL;
442+
434443 if (head->r.w_pos)
435444 return;
436445 kfree(head->read_buf);
security/tomoyo/common.c+139−60
@@ -197,6 +197,7 @@ static void tomoyo_addprintf(char *buffer, int len, const char *fmt, ...) · 196 unmodified lines
197197 {
198198 va_list args;
199199 const int pos = strlen(buffer);
200+
200201 va_start(args, fmt);
201202 vsnprintf(buffer + pos, len - pos - 1, fmt, args);
202203 va_end(args);
@@ -214,6 +215,7 @@ static bool tomoyo_flush(struct tomoyo_io_buffer *head) · 11 unmodified lines
214215 while (head->r.w_pos) {
215216 const char *w = head->r.w[0];
216217 size_t len = strlen(w);
218+
217219 if (len) {
218220 if (len > head->read_user_buf_avail)
219221 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
279281 size_t len;
280282 size_t pos = head->r.avail;
281283 int size = head->readbuf_size - pos;
284+
282285 if (size <= 0)
283286 return;
284287 va_start(args, fmt);
@@ -344,13 +347,14 @@ static bool tomoyo_namespace_enabled; · 59 unmodified lines
344347 void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns)
345348 {
346349 unsigned int idx;
350+
347351 for (idx = 0; idx < TOMOYO_MAX_ACL_GROUPS; idx++)
348352 INIT_LIST_HEAD(&ns->acl_group[idx]);
349353 for (idx = 0; idx < TOMOYO_MAX_GROUP; idx++)
350354 INIT_LIST_HEAD(&ns->group_list[idx]);
351355 for (idx = 0; idx < TOMOYO_MAX_POLICY; idx++)
352356 INIT_LIST_HEAD(&ns->policy_list[idx]);
353− ns->profile_version = 20110903;
357+ ns->profile_version = 20150505;
354358 tomoyo_namespace_enabled = !list_empty(&tomoyo_namespace_list);
355359 list_add_tail_rcu(&ns->namespace_list, &tomoyo_namespace_list);
356360 }
@@ -433,6 +437,7 @@ static void tomoyo_print_number_union_nospace · 76 unmodified lines
433437 u8 min_type = ptr->value_type[0];
434438 const u8 max_type = ptr->value_type[1];
435439 char buffer[128];
440+
436441 buffer[0] = '\0';
437442 for (i = 0; i < 2; i++) {
438443 switch (min_type) {
@@ -487,6 +492,7 @@ static struct tomoyo_profile *tomoyo_assign_profile · 48 unmodified lines
487492 {
488493 struct tomoyo_profile *ptr;
489494 struct tomoyo_profile *entry;
495+
490496 if (profile >= TOMOYO_MAX_PROFILES)
491497 return NULL;
492498 ptr = ns->profile_ptr[profile];
@@ -530,6 +536,7 @@ struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns, · 37 unmodified lines
530536 {
531537 static struct tomoyo_profile tomoyo_null_profile;
532538 struct tomoyo_profile *ptr = ns->profile_ptr[profile];
539+
533540 if (!ptr)
534541 ptr = &tomoyo_null_profile;
535542 return ptr;
@@ -546,6 +553,7 @@ struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns, · 10 unmodified lines
546553 static s8 tomoyo_find_yesno(const char *string, const char *find)
547554 {
548555 const char *cp = strstr(string, find);
556+
549557 if (cp) {
550558 cp += strlen(find);
551559 if (!strncmp(cp, "=yes", 4))
@@ -569,6 +577,7 @@ static void tomoyo_set_uint(unsigned int *i, const char *string, · 17 unmodified lines
569577 const char *find)
570578 {
571579 const char *cp = strstr(string, find);
580+
572581 if (cp)
573582 sscanf(cp + strlen(find), "=%u", i);
574583 }
@@ -587,6 +596,7 @@ static int tomoyo_set_mode(char *name, const char *value, · 12 unmodified lines
587596 {
588597 u8 i;
589598 u8 config;
599+
590600 if (!strcmp(name, "CONFIG")) {
591601 i = TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX;
592602 config = profile->default_config;
@@ -595,10 +605,12 @@ static int tomoyo_set_mode(char *name, const char *value, · 2 unmodified lines
595605 for (i = 0; i < TOMOYO_MAX_MAC_INDEX
596606 + TOMOYO_MAX_MAC_CATEGORY_INDEX; i++) {
597607 int len = 0;
608+
598609 if (i < TOMOYO_MAX_MAC_INDEX) {
599610 const u8 c = tomoyo_index2category[i];
600611 const char *category =
601612 tomoyo_category_keywords[c];
613+
602614 len = strlen(category);
603615 if (strncmp(name, category, len) ||
604616 name[len++] != ':' || name[len++] != ':')
@@ -618,6 +630,7 @@ static int tomoyo_set_mode(char *name, const char *value, · 13 unmodified lines
618630 config = TOMOYO_CONFIG_USE_DEFAULT;
619631 } else {
620632 u8 mode;
633+
621634 for (mode = 0; mode < 4; mode++)
622635 if (strstr(value, tomoyo_mode[mode]))
623636 /*
@@ -664,6 +677,7 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head) · 40 unmodified lines
664677 unsigned int i;
665678 char *cp;
666679 struct tomoyo_profile *profile;
680+
667681 if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
668682 == 1)
669683 return 0;
@@ -683,6 +697,7 @@ static int tomoyo_write_profile(struct tomoyo_io_buffer *head) · 13 unmodified lines
683697 const struct tomoyo_path_info *new_comment
684698 = tomoyo_get_name(cp);
685699 const struct tomoyo_path_info *old_comment;
700+
686701 if (!new_comment)
687702 return -ENOMEM;
688703 spin_lock(&lock);
@@ -732,6 +747,7 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head) · 43 unmodified lines
732747 struct tomoyo_policy_namespace *ns =
733748 container_of(head->r.ns, typeof(*ns), namespace_list);
734749 const struct tomoyo_profile *profile;
750+
735751 if (head->r.eof)
736752 return;
737753 next:
@@ -760,6 +776,7 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head) · 22 unmodified lines
760776 u8 i;
761777 const struct tomoyo_path_info *comment =
762778 profile->comment;
779+
763780 tomoyo_print_namespace(head);
764781 tomoyo_io_printf(head, "%u-COMMENT=", index);
765782 tomoyo_set_string(head, comment ? comment->name : "");
@@ -788,6 +805,7 @@ static void tomoyo_read_profile(struct tomoyo_io_buffer *head) · 22 unmodified lines
788805 + TOMOYO_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
789806 const u8 i = head->r.bit;
790807 const u8 config = profile->config[i];
808+
791809 if (config == TOMOYO_CONFIG_USE_DEFAULT)
792810 continue;
793811 tomoyo_print_namespace(head);
@@ -847,10 +865,10 @@ static int tomoyo_update_manager_entry(const char *manager, · 53 unmodified lines
847865 struct tomoyo_acl_param param = {
848866 /* .ns = &tomoyo_kernel_namespace, */
849867 .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],
852869 };
853870 int error = is_delete ? -ENOENT : -ENOMEM;
871+
854872 if (!tomoyo_correct_domain(manager) &&
855873 !tomoyo_correct_word(manager))
856874 return -EINVAL;
@@ -894,10 +912,10 @@ static void tomoyo_read_manager(struct tomoyo_io_buffer *head) · 37 unmodified lines
894912 {
895913 if (head->r.eof)
896914 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]) {
899916 struct tomoyo_manager *ptr =
900917 list_entry(head->r.acl, typeof(*ptr), head.list);
918+
901919 if (ptr->head.is_deleted)
902920 continue;
903921 if (!tomoyo_flush(head))
@@ -933,8 +951,7 @@ static bool tomoyo_manager(void) · 29 unmodified lines
933951 exe = tomoyo_get_exe();
934952 if (!exe)
935953 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) {
938955 if (!ptr->head.is_deleted &&
939956 (!tomoyo_pathcmp(domainname, ptr->manager) ||
940957 !strcmp(exe, ptr->manager->name))) {
@@ -945,9 +962,10 @@ static bool tomoyo_manager(void) · 4 unmodified lines
945962 if (!found) { /* Reduce error messages. */
946963 static pid_t last_pid;
947964 const pid_t pid = current->pid;
965+
948966 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);
951969 last_pid = pid;
952970 }
953971 }
@@ -974,19 +992,21 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head, · 20 unmodified lines
974992 unsigned int pid;
975993 struct tomoyo_domain_info *domain = NULL;
976994 bool global_pid = false;
995+
977996 if (strncmp(data, "select ", 7))
978997 return false;
979998 data += 7;
980999 if (sscanf(data, "pid=%u", &pid) == 1 ||
9811000 (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
9821001 struct task_struct *p;
1002+
9831003 rcu_read_lock();
9841004 if (global_pid)
9851005 p = find_task_by_pid_ns(pid, &init_pid_ns);
9861006 else
9871007 p = find_task_by_vpid(pid);
9881008 if (p)
989− domain = tomoyo_real_domain(p);
1009+ domain = tomoyo_task(p)->domain_info;
9901010 rcu_read_unlock();
9911011 } else if (!strncmp(data, "domain=", 7)) {
9921012 if (tomoyo_domain_def(data + 7))
@@ -1020,10 +1040,11 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head, · 27 unmodified lines
10201040 * Returns true if @a == @b, false otherwise.
10211041 */
10221042 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)
10241044 {
10251045 const struct tomoyo_task_acl *p1 = container_of(a, typeof(*p1), head);
10261046 const struct tomoyo_task_acl *p2 = container_of(b, typeof(*p2), head);
1047+
10271048 return p1->domainname == p2->domainname;
10281049 }
10291050
@@ -1039,11 +1060,13 @@ static bool tomoyo_same_task_acl(const struct tomoyo_acl_info *a, · 9 unmodified lines
10391060 static int tomoyo_write_task(struct tomoyo_acl_param *param)
10401061 {
10411062 int error = -EINVAL;
1063+
10421064 if (tomoyo_str_starts(&param->data, "manual_domain_transition ")) {
10431065 struct tomoyo_task_acl e = {
10441066 .head.type = TOMOYO_TYPE_MANUAL_TASK_ACL,
10451067 .domainname = tomoyo_get_domainname(param),
10461068 };
1069+
10471070 if (e.domainname)
10481071 error = tomoyo_update_domain(&e.head, sizeof(e), param,
10491072 tomoyo_same_task_acl,
@@ -1110,7 +1133,7 @@ static int tomoyo_write_domain2(struct tomoyo_policy_namespace *ns, · 60 unmodified lines
11101133 };
11111134 static const struct {
11121135 const char *keyword;
1113− int (*write) (struct tomoyo_acl_param *);
1136+ int (*write)(struct tomoyo_acl_param *param);
11141137 } tomoyo_callback[5] = {
11151138 { "file ", tomoyo_write_file },
11161139 { "network inet ", tomoyo_write_inet_network },
@@ -1151,9 +1174,11 @@ static int tomoyo_write_domain(struct tomoyo_io_buffer *head) · 34 unmodified lines
11511174 struct tomoyo_domain_info *domain = head->w.domain;
11521175 const bool is_delete = head->w.is_delete;
11531176 bool is_select = !is_delete && tomoyo_str_starts(&data, "select ");
1154− unsigned int profile;
1177+ unsigned int idx;
1178+
11551179 if (*data == '<') {
11561180 int ret = 0;
1181+
11571182 domain = NULL;
11581183 if (is_delete)
11591184 ret = tomoyo_delete_domain(data);
@@ -1167,23 +1192,27 @@ static int tomoyo_write_domain(struct tomoyo_io_buffer *head) · 7 unmodified lines
11671192 if (!domain)
11681193 return -EINVAL;
11691194 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;
11741200 return 0;
11751201 }
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) {
11781204 if (!is_delete)
1179− domain->group = (u8) profile;
1205+ set_bit(idx, domain->group);
1206+ else
1207+ clear_bit(idx, domain->group);
11801208 return 0;
11811209 }
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+
11841213 if (strncmp(data, cp, strlen(cp) - 1))
11851214 continue;
1186− domain->flags[profile] = !is_delete;
1215+ domain->flags[idx] = !is_delete;
11871216 return 0;
11881217 }
11891218 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
12251254 const struct tomoyo_envp *envp =
12261255 (typeof(envp)) (argv + cond->argc);
12271256 u16 skip;
1257+
12281258 for (skip = 0; skip < head->r.cond_index; skip++) {
12291259 const u8 left = condp->left;
12301260 const u8 right = condp->right;
1261+
12311262 condp++;
12321263 switch (left) {
12331264 case TOMOYO_ARGV_ENTRY:
@@ -1253,6 +1284,7 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 19 unmodified lines
12531284 const u8 match = condp->equals;
12541285 const u8 left = condp->left;
12551286 const u8 right = condp->right;
1287+
12561288 if (!tomoyo_flush(head))
12571289 return false;
12581290 condp++;
@@ -1262,8 +1294,7 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 3 unmodified lines
12621294 case TOMOYO_ARGV_ENTRY:
12631295 tomoyo_io_printf(head,
12641296 "exec.argv[%lu]%s=\"",
1265− argv->index, argv->
1266− is_not ? "!" : "");
1297+ argv->index, argv->is_not ? "!" : "");
12671298 tomoyo_set_string(head,
12681299 argv->value->name);
12691300 tomoyo_set_string(head, "\"");
@@ -1274,12 +1305,10 @@ static bool tomoyo_print_condition(struct tomoyo_io_buffer *head, · 4 unmodified lines
12741305 "exec.envp[\"");
12751306 tomoyo_set_string(head,
12761307 envp->name->name);
1277− tomoyo_io_printf(head, "\"]%s=", envp->
1278− is_not ? "!" : "");
1308+ tomoyo_io_printf(head, "\"]%s=", envp->is_not ? "!" : "");
12791309 if (envp->value) {
12801310 tomoyo_set_string(head, "\"");
1281− tomoyo_set_string(head, envp->
1282− value->name);
1311+ tomoyo_set_string(head, envp->value->name);
12831312 tomoyo_set_string(head, "\"");
12841313 } else {
12851314 tomoyo_set_string(head,
@@ -1375,6 +1404,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 89 unmodified lines
13751404 struct tomoyo_path_acl *ptr =
13761405 container_of(acl, typeof(*ptr), head);
13771406 const u16 perm = ptr->perm;
1407+
13781408 for (bit = 0; bit < TOMOYO_MAX_PATH_OPERATION; bit++) {
13791409 if (!(perm & (1 << bit)))
13801410 continue;
@@ -1395,6 +1425,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 14 unmodified lines
13951425 } else if (acl_type == TOMOYO_TYPE_MANUAL_TASK_ACL) {
13961426 struct tomoyo_task_acl *ptr =
13971427 container_of(acl, typeof(*ptr), head);
1428+
13981429 tomoyo_set_group(head, "task ");
13991430 tomoyo_set_string(head, "manual_domain_transition ");
14001431 tomoyo_set_string(head, ptr->domainname->name);
@@ -1404,6 +1435,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 3 unmodified lines
14041435 struct tomoyo_path2_acl *ptr =
14051436 container_of(acl, typeof(*ptr), head);
14061437 const u8 perm = ptr->perm;
1438+
14071439 for (bit = 0; bit < TOMOYO_MAX_PATH2_OPERATION; bit++) {
14081440 if (!(perm & (1 << bit)))
14091441 continue;
@@ -1424,6 +1456,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 14 unmodified lines
14241456 struct tomoyo_path_number_acl *ptr =
14251457 container_of(acl, typeof(*ptr), head);
14261458 const u8 perm = ptr->perm;
1459+
14271460 for (bit = 0; bit < TOMOYO_MAX_PATH_NUMBER_OPERATION; bit++) {
14281461 if (!(perm & (1 << bit)))
14291462 continue;
@@ -1444,6 +1477,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 14 unmodified lines
14441477 struct tomoyo_mkdev_acl *ptr =
14451478 container_of(acl, typeof(*ptr), head);
14461479 const u8 perm = ptr->perm;
1480+
14471481 for (bit = 0; bit < TOMOYO_MAX_MKDEV_OPERATION; bit++) {
14481482 if (!(perm & (1 << bit)))
14491483 continue;
@@ -1490,6 +1524,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 40 unmodified lines
14901524 ->name);
14911525 } else {
14921526 char buf[128];
1527+
14931528 tomoyo_print_ip(buf, sizeof(buf), &ptr->address);
14941529 tomoyo_io_printf(head, "%s", buf);
14951530 }
@@ -1519,6 +1554,7 @@ static bool tomoyo_print_entry(struct tomoyo_io_buffer *head, · 23 unmodified lines
15191554 } else if (acl_type == TOMOYO_TYPE_MOUNT_ACL) {
15201555 struct tomoyo_mount_acl *ptr =
15211556 container_of(acl, typeof(*ptr), head);
1557+
15221558 tomoyo_set_group(head, "file mount");
15231559 tomoyo_print_name_union(head, &ptr->dev_name);
15241560 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
15621598 list_for_each_cookie(head->r.acl, list) {
15631599 struct tomoyo_acl_info *ptr =
15641600 list_entry(head->r.acl, typeof(*ptr), list);
1601+
15651602 if (!tomoyo_print_entry(head, ptr))
15661603 return false;
15671604 }
@@ -1583,8 +1620,9 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head) · 15 unmodified lines
15831620 list_for_each_cookie(head->r.domain, &tomoyo_domain_list) {
15841621 struct tomoyo_domain_info *domain =
15851622 list_entry(head->r.domain, typeof(*domain), list);
1623+ u8 i;
1624+
15861625 switch (head->r.step) {
1587− u8 i;
15881626 case 0:
15891627 if (domain->is_deleted &&
15901628 !head->r.print_this_domain_only)
@@ -1594,22 +1632,33 @@ static void tomoyo_read_domain(struct tomoyo_io_buffer *head) · 3 unmodified lines
15941632 tomoyo_set_lf(head);
15951633 tomoyo_io_printf(head, "use_profile %u\n",
15961634 domain->profile);
1597− tomoyo_io_printf(head, "use_group %u\n",
1598− domain->group);
15991635 for (i = 0; i < TOMOYO_MAX_DOMAIN_INFO_FLAGS; i++)
16001636 if (domain->flags[i])
16011637 tomoyo_set_string(head, tomoyo_dif[i]);
1638+ head->r.index = 0;
16021639 head->r.step++;
1603− tomoyo_set_lf(head);
16041640 /* fall through */
16051641 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:
16061655 if (!tomoyo_read_domain2(head, &domain->acl_info_list))
16071656 return;
16081657 head->r.step++;
16091658 if (!tomoyo_set_lf(head))
16101659 return;
16111660 /* fall through */
1612− case 2:
1661+ case 3:
16131662 head->r.step = 0;
16141663 if (head->r.print_this_domain_only)
16151664 goto done;
@@ -1668,7 +1717,7 @@ static void tomoyo_read_pid(struct tomoyo_io_buffer *head) · 52 unmodified lines
16681717 else
16691718 p = find_task_by_vpid(pid);
16701719 if (p)
1671− domain = tomoyo_real_domain(p);
1720+ domain = tomoyo_task(p)->domain_info;
16721721 rcu_read_unlock();
16731722 if (!domain)
16741723 return;
@@ -1711,6 +1760,7 @@ static int tomoyo_write_exception(struct tomoyo_io_buffer *head) · 36 unmodified lines
17111760 .data = head->write_buf,
17121761 };
17131762 u8 i;
1763+
17141764 if (tomoyo_str_starts(&param.data, "aggregator "))
17151765 return tomoyo_write_aggregator(&param);
17161766 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
17221772 if (tomoyo_str_starts(&param.data, "acl_group ")) {
17231773 unsigned int group;
17241774 char *data;
1775+
17251776 group = simple_strtoul(param.data, &data, 10);
17261777 if (group < TOMOYO_MAX_ACL_GROUPS && *data++ == ' ')
17271778 return tomoyo_write_domain2
@@ -1746,12 +1797,15 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) · 18 unmodified lines
17461797 struct tomoyo_policy_namespace *ns =
17471798 container_of(head->r.ns, typeof(*ns), namespace_list);
17481799 struct list_head *list = &ns->group_list[idx];
1800+
17491801 list_for_each_cookie(head->r.group, list) {
17501802 struct tomoyo_group *group =
17511803 list_entry(head->r.group, typeof(*group), head.list);
1804+
17521805 list_for_each_cookie(head->r.acl, &group->member_list) {
17531806 struct tomoyo_acl_head *ptr =
17541807 list_entry(head->r.acl, typeof(*ptr), list);
1808+
17551809 if (ptr->is_deleted)
17561810 continue;
17571811 if (!tomoyo_flush(head))
@@ -1771,10 +1825,10 @@ static bool tomoyo_read_group(struct tomoyo_io_buffer *head, const int idx) · 13 unmodified lines
17711825 head)->number);
17721826 } else if (idx == TOMOYO_ADDRESS_GROUP) {
17731827 char buffer[128];
1774−
17751828 struct tomoyo_address_group *member =
17761829 container_of(ptr, typeof(*member),
17771830 head);
1831+
17781832 tomoyo_print_ip(buffer, sizeof(buffer),
17791833 &member->address);
17801834 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
18021856 struct tomoyo_policy_namespace *ns =
18031857 container_of(head->r.ns, typeof(*ns), namespace_list);
18041858 struct list_head *list = &ns->policy_list[idx];
1859+
18051860 list_for_each_cookie(head->r.acl, list) {
18061861 struct tomoyo_acl_head *acl =
18071862 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
18141869 {
18151870 struct tomoyo_transition_control *ptr =
18161871 container_of(acl, typeof(*ptr), head);
1872+
18171873 tomoyo_print_namespace(head);
18181874 tomoyo_set_string(head, tomoyo_transition_type
18191875 [ptr->type]);
@@ -1829,6 +1885,7 @@ static bool tomoyo_read_policy(struct tomoyo_io_buffer *head, const int idx) · 9 unmodified lines
18291885 {
18301886 struct tomoyo_aggregator *ptr =
18311887 container_of(acl, typeof(*ptr), head);
1888+
18321889 tomoyo_print_namespace(head);
18331890 tomoyo_set_string(head, "aggregator ");
18341891 tomoyo_set_string(head,
@@ -1858,6 +1915,7 @@ static void tomoyo_read_exception(struct tomoyo_io_buffer *head) · 23 unmodified lines
18581915 {
18591916 struct tomoyo_policy_namespace *ns =
18601917 container_of(head->r.ns, typeof(*ns), namespace_list);
1918+
18611919 if (head->r.eof)
18621920 return;
18631921 while (head->r.step < TOMOYO_MAX_POLICY &&
@@ -1921,6 +1979,7 @@ static atomic_t tomoyo_query_observers = ATOMIC_INIT(0); · 57 unmodified lines
19211979 static int tomoyo_truncate(char *str)
19221980 {
19231981 char *start = str;
1982+
19241983 while (*(unsigned char *) str > (unsigned char) ' ')
19251984 str++;
19261985 *str = '\0';
@@ -1943,6 +2002,7 @@ static void tomoyo_add_entry(struct tomoyo_domain_info *domain, char *header) · 16 unmodified lines
19432002 char *symlink = NULL;
19442003 char *cp = strchr(header, '\n');
19452004 int len;
2005+
19462006 if (!cp)
19472007 return;
19482008 cp = strchr(cp + 1, '\n');
@@ -2002,6 +2062,7 @@ int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) · 53 unmodified lines
20022062 static unsigned int tomoyo_serial;
20032063 struct tomoyo_query entry = { };
20042064 bool quota_exceeded = false;
2065+
20052066 va_start(args, fmt);
20062067 len = vsnprintf((char *) &len, 1, fmt, args) + 1;
20072068 va_end(args);
@@ -2063,8 +2124,7 @@ int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...) · 55 unmodified lines
20632124 (tomoyo_answer_wait, entry.answer ||
20642125 !atomic_read(&tomoyo_query_observers), HZ))
20652126 break;
2066− else
2067− entry.timer++;
2127+ entry.timer++;
20682128 }
20692129 spin_lock(&tomoyo_query_list_lock);
20702130 list_del(&entry.list);
@@ -2100,6 +2160,7 @@ static struct tomoyo_domain_info *tomoyo_find_domain_by_qid · 29 unmodified lines
21002160 {
21012161 struct tomoyo_query *ptr;
21022162 struct tomoyo_domain_info *domain = NULL;
2163+
21032164 spin_lock(&tomoyo_query_list_lock);
21042165 list_for_each_entry(ptr, &tomoyo_query_list, list) {
21052166 if (ptr->serial != serial)
@@ -2142,15 +2203,15 @@ static void tomoyo_read_query(struct tomoyo_io_buffer *head) · 36 unmodified lines
21422203 unsigned int pos = 0;
21432204 size_t len = 0;
21442205 char *buf;
2206+
21452207 if (head->r.w_pos)
21462208 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;
21512211 spin_lock(&tomoyo_query_list_lock);
21522212 list_for_each(tmp, &tomoyo_query_list) {
21532213 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2214+
21542215 if (pos++ != head->r.query_index)
21552216 continue;
21562217 len = ptr->query_len;
@@ -2168,6 +2229,7 @@ static void tomoyo_read_query(struct tomoyo_io_buffer *head) · 11 unmodified lines
21682229 spin_lock(&tomoyo_query_list_lock);
21692230 list_for_each(tmp, &tomoyo_query_list) {
21702231 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2232+
21712233 if (pos++ != head->r.query_index)
21722234 continue;
21732235 /*
@@ -2202,9 +2264,11 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head) · 28 unmodified lines
22022264 struct list_head *tmp;
22032265 unsigned int serial;
22042266 unsigned int answer;
2267+
22052268 spin_lock(&tomoyo_query_list_lock);
22062269 list_for_each(tmp, &tomoyo_query_list) {
22072270 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2271+
22082272 ptr->timer = 0;
22092273 }
22102274 spin_unlock(&tomoyo_query_list_lock);
@@ -2213,6 +2277,7 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head) · 2 unmodified lines
22132277 spin_lock(&tomoyo_query_list_lock);
22142278 list_for_each(tmp, &tomoyo_query_list) {
22152279 struct tomoyo_query *ptr = list_entry(tmp, typeof(*ptr), list);
2280+
22162281 if (ptr->serial != serial)
22172282 continue;
22182283 ptr->answer = answer;
@@ -2235,7 +2300,7 @@ static int tomoyo_write_answer(struct tomoyo_io_buffer *head) · 16 unmodified lines
22352300 static void tomoyo_read_version(struct tomoyo_io_buffer *head)
22362301 {
22372302 if (!head->r.eof) {
2238− tomoyo_io_printf(head, "2.5.0");
2303+ tomoyo_io_printf(head, "2.6.0");
22392304 head->r.eof = true;
22402305 }
22412306 }
@@ -2287,6 +2352,7 @@ static void tomoyo_read_stat(struct tomoyo_io_buffer *head) · 45 unmodified lines
22872352 {
22882353 u8 i;
22892354 unsigned int total = 0;
2355+
22902356 if (head->r.eof)
22912357 return;
22922358 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
22952361 tomoyo_stat_updated[i]);
22962362 if (tomoyo_stat_modified[i]) {
22972363 struct tomoyo_time stamp;
2364+
22982365 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)",
23012367 stamp.year, stamp.month, stamp.day,
23022368 stamp.hour, stamp.min, stamp.sec);
23032369 }
@@ -2305,6 +2371,7 @@ static void tomoyo_read_stat(struct tomoyo_io_buffer *head) · 1 unmodified line
23052371 }
23062372 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++) {
23072373 unsigned int used = tomoyo_memory_used[i];
2374+
23082375 total += used;
23092376 tomoyo_io_printf(head, "Memory used by %-22s %10u",
23102377 tomoyo_memory_headers[i], used);
@@ -2329,6 +2396,7 @@ static int tomoyo_write_stat(struct tomoyo_io_buffer *head) · 18 unmodified lines
23292396 {
23302397 char *data = head->write_buf;
23312398 u8 i;
2399+
23322400 if (tomoyo_str_starts(&data, "Memory used by "))
23332401 for (i = 0; i < TOMOYO_MAX_MEMORY_STAT; i++)
23342402 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
24572525 __poll_t tomoyo_poll_control(struct file *file, poll_table *wait)
24582526 {
24592527 struct tomoyo_io_buffer *head = file->private_data;
2528+
24602529 if (head->poll)
24612530 return head->poll(file, wait) | EPOLLOUT | EPOLLWRNORM;
24622531 return EPOLLIN | EPOLLRDNORM | EPOLLOUT | EPOLLWRNORM;
@@ -2472,6 +2541,7 @@ __poll_t tomoyo_poll_control(struct file *file, poll_table *wait) · 9 unmodified lines
24722541 static inline void tomoyo_set_namespace_cursor(struct tomoyo_io_buffer *head)
24732542 {
24742543 struct list_head *ns;
2544+
24752545 if (head->type != TOMOYO_EXCEPTIONPOLICY &&
24762546 head->type != TOMOYO_PROFILE)
24772547 return;
@@ -2517,7 +2587,7 @@ ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer, · 39 unmodified lines
25172587 int idx;
25182588
25192589 if (!head->read)
2520− return -ENOSYS;
2590+ return -EINVAL;
25212591 if (mutex_lock_interruptible(&head->io_sem))
25222592 return -EINTR;
25232593 head->read_user_buf = buffer;
@@ -2557,6 +2627,7 @@ static int tomoyo_parse_policy(struct tomoyo_io_buffer *head, char *line) · 33 unmodified lines
25572627 head->type == TOMOYO_PROFILE) {
25582628 if (*line == '<') {
25592629 char *cp = strchr(line, ' ');
2630+
25602631 if (cp) {
25612632 *cp++ = '\0';
25622633 head->w.ns = tomoyo_assign_namespace(line);
@@ -2589,8 +2660,9 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, · 26 unmodified lines
25892660 size_t avail_len = buffer_len;
25902661 char *cp0 = head->write_buf;
25912662 int idx;
2663+
25922664 if (!head->write)
2593− return -ENOSYS;
2665+ return -EINVAL;
25942666 if (!access_ok(buffer, buffer_len))
25952667 return -EFAULT;
25962668 if (mutex_lock_interruptible(&head->io_sem))
@@ -2600,9 +2672,11 @@ ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head, · 3 unmodified lines
26002672 /* Read a line and dispatch it to the policy handler. */
26012673 while (avail_len > 0) {
26022674 char c;
2675+
26032676 if (head->w.avail >= head->writebuf_size - 1) {
26042677 const int len = head->writebuf_size * 2;
26052678 char *cp = kzalloc(len, GFP_NOFS);
2679+
26062680 if (!cp) {
26072681 error = -ENOMEM;
26082682 break;
@@ -2701,30 +2775,32 @@ void tomoyo_check_profile(void) · 92 unmodified lines
27012775 {
27022776 struct tomoyo_domain_info *domain;
27032777 const int idx = tomoyo_read_lock();
2778+
27042779 tomoyo_policy_loaded = true;
2705− printk(KERN_INFO "TOMOYO: 2.5.0\n");
2780+ pr_info("TOMOYO: 2.6.0\n");
27062781 list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) {
27072782 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",
27122792 ns->profile_version);
27132793 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",
27162795 profile, domain->domainname->name);
27172796 else
27182797 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");
27242800 panic("STOP!");
27252801 }
27262802 tomoyo_read_unlock(idx);
2727− printk(KERN_INFO "Mandatory Access Control activated.\n");
2803+ pr_info("Mandatory Access Control activated.\n");
27282804 }
27292805
27302806 /**
@@ -2743,9 +2819,11 @@ void __init tomoyo_load_builtin_policy(void) · 12 unmodified lines
27432819 #include "builtin-policy.h"
27442820 u8 i;
27452821 const int idx = tomoyo_read_lock();
2822+
27462823 for (i = 0; i < 5; i++) {
27472824 struct tomoyo_io_buffer head = { };
27482825 char *start = "";
2826+
27492827 switch (i) {
27502828 case 0:
27512829 start = tomoyo_builtin_profile;
@@ -2775,6 +2853,7 @@ void __init tomoyo_load_builtin_policy(void) · 23 unmodified lines
27752853 }
27762854 while (1) {
27772855 char *end = strchr(start, '\n');
2856+
27782857 if (!end)
27792858 break;
27802859 *end = '\0';
security/tomoyo/common.h+27−24
@@ -10,6 +10,8 @@ · 9 unmodified lines
1010 #ifndef _SECURITY_TOMOYO_COMMON_H
1111 #define _SECURITY_TOMOYO_COMMON_H
1212
13+#define pr_fmt(fmt) fmt
14+
1315 #include <linux/ctype.h>
1416 #include <linux/string.h>
1517 #include <linux/mm.h>
@@ -29,6 +31,7 @@ · 13 unmodified lines
2931 #include <linux/in.h>
3032 #include <linux/in6.h>
3133 #include <linux/un.h>
34+#include <linux/lsm_hooks.h>
3235 #include <net/sock.h>
3336 #include <net/af_unix.h>
3437 #include <net/ip.h>
@@ -681,11 +684,12 @@ struct tomoyo_domain_info { · 646 unmodified lines
681684 const struct tomoyo_path_info *domainname;
682685 /* Namespace for this domain. Never NULL. */
683686 struct tomoyo_policy_namespace *ns;
687+ /* Group numbers to use. */
688+ unsigned long group[TOMOYO_MAX_ACL_GROUPS / BITS_PER_LONG];
684689 u8 profile; /* Profile number to use. */
685− u8 group; /* Group number to use. */
686690 bool is_deleted; /* Delete flag. */
687691 bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
688− atomic_t users; /* Number of referring credentials. */
692+ atomic_t users; /* Number of referring tasks. */
689693 };
690694
691695 /*
@@ -787,9 +791,9 @@ struct tomoyo_acl_param { · 95 unmodified lines
787791 * interfaces.
788792 */
789793 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);
793797 /* Exclusive lock for this structure. */
794798 struct mutex io_sem;
795799 char __user *read_user_buf;
@@ -906,12 +910,18 @@ struct tomoyo_policy_namespace { · 110 unmodified lines
906910 struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
907911 /* List for connecting to tomoyo_namespace_list list. */
908912 struct list_head namespace_list;
909− /* Profile version. Currently only 20110903 is defined. */
913+ /* Profile version. Currently only 20150505 is defined. */
910914 unsigned int profile_version;
911915 /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
912916 const char *name;
913917 };
914918
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+
915925 /********** Function prototypes. **********/
916926
917927 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
10201030 struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
10211031 struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
10221032 const bool transit);
1033+struct tomoyo_domain_info *tomoyo_domain(void);
10231034 struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
10241035 struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
10251036 const u8 idx);
@@ -1034,8 +1045,8 @@ void *tomoyo_commit_ok(void *data, const unsigned int size); · 8 unmodified lines
10341045 void __init tomoyo_load_builtin_policy(void);
10351046 void __init tomoyo_mm_init(void);
10361047 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 *));
10391050 void tomoyo_check_profile(void);
10401051 void tomoyo_convert_time(time64_t time, struct tomoyo_time *stamp);
10411052 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
10621073 /********** External variable definitions. **********/
10631074
10641075 extern bool tomoyo_policy_loaded;
1076+extern int tomoyo_enabled;
10651077 extern const char * const tomoyo_condition_keyword
10661078 [TOMOYO_MAX_CONDITION_KEYWORD];
10671079 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
10851097 extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
10861098 extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
10871099 extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
1100+extern struct lsm_blob_sizes tomoyo_blob_sizes;
10881101
10891102 /********** Inlined functions. **********/
10901103
@@ -1121,6 +1134,7 @@ static inline void tomoyo_read_unlock(int idx) · 30 unmodified lines
11211134 static inline pid_t tomoyo_sys_getppid(void)
11221135 {
11231136 pid_t pid;
1137+
11241138 rcu_read_lock();
11251139 pid = task_tgid_vnr(rcu_dereference(current->real_parent));
11261140 rcu_read_unlock();
@@ -1197,26 +1211,15 @@ static inline void tomoyo_put_group(struct tomoyo_group *group) · 70 unmodified lines
11971211 }
11981212
11991213 /**
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.
12111215 *
1212− * @task: Pointer to "struct task_struct".
1216+ * @task - Pointer to "struct task_struct".
12131217 *
1214− * Returns pointer to "struct tomoyo_security" for specified thread.
1218+ * Returns pointer to "struct tomoyo_task" for specified thread.
12151219 */
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)
12181221 {
1219− return task_cred_xxx(task, security);
1222+ return task->security + tomoyo_blob_sizes.lbs_task;
12201223 }
12211224
12221225 /**
security/tomoyo/condition.c+43−16
@@ -28,9 +28,11 @@ static bool tomoyo_argv(const unsigned int index, const char *arg_ptr, · 27 unmodified lines
2828 {
2929 int i;
3030 struct tomoyo_path_info arg;
31+
3132 arg.name = arg_ptr;
3233 for (i = 0; i < argc; argv++, checked++, i++) {
3334 bool result;
35+
3436 if (index != argv->index)
3537 continue;
3638 *checked = 1;
@@ -62,12 +64,14 @@ static bool tomoyo_envp(const char *env_name, const char *env_value, · 25 unmodified lines
6264 int i;
6365 struct tomoyo_path_info name;
6466 struct tomoyo_path_info value;
67+
6568 name.name = env_name;
6669 tomoyo_fill_path_info(&name);
6770 value.name = env_value;
6871 tomoyo_fill_path_info(&value);
6972 for (i = 0; i < envc; envp++, checked++, i++) {
7073 bool result;
74+
7175 if (!tomoyo_path_matches_pattern(&name, envp->name))
7276 continue;
7377 *checked = 1;
@@ -113,6 +117,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 39 unmodified lines
113117 bool result = true;
114118 u8 local_checked[32];
115119 u8 *checked;
120+
116121 if (argc + envc <= sizeof(local_checked)) {
117122 checked = local_checked;
118123 memset(local_checked, 0, sizeof(local_checked));
@@ -131,6 +136,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 12 unmodified lines
131136 /* Read. */
132137 const char *kaddr = dump->data;
133138 const unsigned char c = kaddr[offset++];
139+
134140 if (c && arg_len < TOMOYO_EXEC_TMPSIZE - 10) {
135141 if (c == '\\') {
136142 arg_ptr[arg_len++] = '\\';
@@ -160,6 +166,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 23 unmodified lines
160166 argv_count--;
161167 } else if (envp_count) {
162168 char *cp = strchr(arg_ptr, '=');
169+
163170 if (cp) {
164171 *cp = '\0';
165172 if (!tomoyo_envp(arg_ptr, cp + 1,
@@ -182,6 +189,7 @@ static bool tomoyo_scan_bprm(struct tomoyo_execve *ee, · 16 unmodified lines
182189 out:
183190 if (result) {
184191 int i;
192+
185193 /* Check not-yet-checked entries. */
186194 for (i = 0; i < argc; i++) {
187195 if (checked[i])
@@ -229,6 +237,7 @@ static bool tomoyo_scan_exec_realpath(struct file *file, · 41 unmodified lines
229237 {
230238 bool result;
231239 struct tomoyo_path_info exe;
240+
232241 if (!file)
233242 return false;
234243 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
250259 static const struct tomoyo_path_info *tomoyo_get_dqword(char *start)
251260 {
252261 char *cp = start + strlen(start) - 1;
262+
253263 if (cp == start || *start++ != '"' || *cp != '"')
254264 return NULL;
255265 *cp = '\0';
@@ -270,6 +280,7 @@ static bool tomoyo_parse_name_union_quoted(struct tomoyo_acl_param *param, · 14 unmodified lines
270280 struct tomoyo_name_union *ptr)
271281 {
272282 char *filename = param->data;
283+
273284 if (*filename == '@')
274285 return tomoyo_parse_name_union(param, ptr);
275286 ptr->filename = tomoyo_get_dqword(filename);
@@ -310,6 +321,7 @@ static bool tomoyo_parse_envp(char *left, char *right, · 34 unmodified lines
310321 const struct tomoyo_path_info *name;
311322 const struct tomoyo_path_info *value;
312323 char *cp = left + strlen(left) - 1;
324+
313325 if (*cp-- != ']' || *cp != '"')
314326 goto out;
315327 *cp = '\0';
@@ -364,6 +376,7 @@ static inline bool tomoyo_same_condition(const struct tomoyo_condition *a, · 48 unmodified lines
364376 static u8 tomoyo_condition_type(const char *word)
365377 {
366378 u8 i;
379+
367380 for (i = 0; i < TOMOYO_MAX_CONDITION_KEYWORD; i++) {
368381 if (!strcmp(word, tomoyo_condition_keyword[i]))
369382 break;
@@ -395,6 +408,7 @@ static struct tomoyo_condition *tomoyo_commit_condition · 25 unmodified lines
395408 {
396409 struct tomoyo_condition *ptr;
397410 bool found = false;
411+
398412 if (mutex_lock_interruptible(&tomoyo_policy_lock)) {
399413 dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
400414 ptr = NULL;
@@ -442,12 +456,14 @@ static char *tomoyo_get_transit_preference(struct tomoyo_acl_param *param, · 41 unmodified lines
442456 {
443457 char * const pos = param->data;
444458 bool flag;
459+
445460 if (*pos == '<') {
446461 e->transit = tomoyo_get_domainname(param);
447462 goto done;
448463 }
449464 {
450465 char *cp = strchr(pos, ' ');
466+
451467 if (cp)
452468 *cp = '\0';
453469 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
489505 tomoyo_get_transit_preference(param, &e);
490506 char * const end_of_string = start_of_string + strlen(start_of_string);
491507 char *pos;
508+
492509 rerun:
493510 pos = start_of_string;
494511 while (1) {
@@ -498,6 +515,7 @@ rerun: · 3 unmodified lines
498515 char *cp;
499516 char *right_word;
500517 bool is_not;
518+
501519 if (!*left_word)
502520 break;
503521 /*
@@ -622,8 +640,8 @@ rerun: · 118 unmodified lines
622640 }
623641 store_value:
624642 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);
627645 continue;
628646 }
629647 condp->left = left;
@@ -660,6 +678,7 @@ store_value: · 30 unmodified lines
660678 envp = (struct tomoyo_envp *) (argv + e.argc);
661679 {
662680 bool flag = false;
681+
663682 for (pos = start_of_string; pos < end_of_string; pos++) {
664683 if (*pos)
665684 continue;
@@ -698,6 +717,7 @@ void tomoyo_get_attributes(struct tomoyo_obj_info *obj) · 32 unmodified lines
698717
699718 for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
700719 struct inode *inode;
720+
701721 switch (i) {
702722 case TOMOYO_PATH1:
703723 dentry = obj->path1.dentry;
@@ -718,6 +738,7 @@ void tomoyo_get_attributes(struct tomoyo_obj_info *obj) · 14 unmodified lines
718738 inode = d_backing_inode(dentry);
719739 if (inode) {
720740 struct tomoyo_mini_stat *stat = &obj->stat[i];
741+
721742 stat->uid = inode->i_uid;
722743 stat->gid = inode->i_gid;
723744 stat->ino = inode->i_ino;
@@ -726,8 +747,7 @@ void tomoyo_get_attributes(struct tomoyo_obj_info *obj) · 2 unmodified lines
726747 stat->rdev = inode->i_rdev;
727748 obj->stat_valid[i] = true;
728749 }
729− if (i & 1) /* i == TOMOYO_PATH1_PARENT ||
730− i == TOMOYO_PATH2_PARENT */
750+ if (i & 1) /* TOMOYO_PATH1_PARENT or TOMOYO_PATH2_PARENT */
731751 dput(dentry);
732752 }
733753 }
@@ -758,6 +778,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 24 unmodified lines
758778 u16 argc;
759779 u16 envc;
760780 struct linux_binprm *bprm = NULL;
781+
761782 if (!cond)
762783 return true;
763784 condc = cond->condc;
@@ -780,6 +801,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 16 unmodified lines
780801 const u8 right = condp->right;
781802 bool is_bitop[2] = { false, false };
782803 u8 j;
804+
783805 condp++;
784806 /* Check argv[] and envp[] later. */
785807 if (left == TOMOYO_ARGV_ENTRY || left == TOMOYO_ENVP_ENTRY)
@@ -787,10 +809,11 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 1 unmodified line
787809 /* Check string expressions. */
788810 if (right == TOMOYO_NAME_UNION) {
789811 const struct tomoyo_name_union *ptr = names_p++;
812+ struct tomoyo_path_info *symlink;
813+ struct tomoyo_execve *ee;
814+ struct file *file;
815+
790816 switch (left) {
791− struct tomoyo_path_info *symlink;
792− struct tomoyo_execve *ee;
793− struct file *file;
794817 case TOMOYO_SYMLINK_TARGET:
795818 symlink = obj ? obj->symlink_target : NULL;
796819 if (!symlink ||
@@ -812,6 +835,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 15 unmodified lines
812835 for (j = 0; j < 2; j++) {
813836 const u8 index = j ? right : left;
814837 unsigned long value = 0;
838+
815839 switch (index) {
816840 case TOMOYO_TASK_UID:
817841 value = from_kuid(&init_user_ns, current_uid());
@@ -874,31 +898,31 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 56 unmodified lines
874898 value = S_ISVTX;
875899 break;
876900 case TOMOYO_MODE_OWNER_READ:
877− value = S_IRUSR;
901+ value = 0400;
878902 break;
879903 case TOMOYO_MODE_OWNER_WRITE:
880− value = S_IWUSR;
904+ value = 0200;
881905 break;
882906 case TOMOYO_MODE_OWNER_EXECUTE:
883− value = S_IXUSR;
907+ value = 0100;
884908 break;
885909 case TOMOYO_MODE_GROUP_READ:
886− value = S_IRGRP;
910+ value = 0040;
887911 break;
888912 case TOMOYO_MODE_GROUP_WRITE:
889− value = S_IWGRP;
913+ value = 0020;
890914 break;
891915 case TOMOYO_MODE_GROUP_EXECUTE:
892− value = S_IXGRP;
916+ value = 0010;
893917 break;
894918 case TOMOYO_MODE_OTHERS_READ:
895− value = S_IROTH;
919+ value = 0004;
896920 break;
897921 case TOMOYO_MODE_OTHERS_WRITE:
898− value = S_IWOTH;
922+ value = 0002;
899923 break;
900924 case TOMOYO_MODE_OTHERS_EXECUTE:
901− value = S_IXOTH;
925+ value = 0001;
902926 break;
903927 case TOMOYO_EXEC_ARGC:
904928 if (!bprm)
@@ -923,6 +947,7 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 18 unmodified lines
923947 {
924948 u8 stat_index;
925949 struct tomoyo_mini_stat *stat;
950+
926951 switch (index) {
927952 case TOMOYO_PATH1_UID:
928953 case TOMOYO_PATH1_GID:
@@ -1036,12 +1061,14 @@ bool tomoyo_condition(struct tomoyo_request_info *r, · 107 unmodified lines
10361061 if (left == TOMOYO_NUMBER_UNION) {
10371062 /* Fetch values now. */
10381063 const struct tomoyo_number_union *ptr = numbers_p++;
1064+
10391065 min_v[0] = ptr->values[0];
10401066 max_v[0] = ptr->values[1];
10411067 }
10421068 if (right == TOMOYO_NUMBER_UNION) {
10431069 /* Fetch values now. */
10441070 const struct tomoyo_number_union *ptr = numbers_p++;
1071+
10451072 if (ptr->group) {
10461073 if (tomoyo_number_matches_group(min_v[0],
10471074 max_v[0],
security/tomoyo/domain.c+51−25
@@ -30,10 +30,10 @@ struct tomoyo_domain_info tomoyo_kernel_domain; · 29 unmodified lines
3030 */
3131 int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
3232 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+ *))
3737 {
3838 int error = param->is_delete ? -ENOENT : -ENOMEM;
3939 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
9090 */
9191 int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
9292 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))
100100 {
101101 const bool is_delete = param->is_delete;
102102 int error = is_delete ? -ENOENT : -ENOMEM;
@@ -157,13 +157,13 @@ out: · 54 unmodified lines
157157 * Caller holds tomoyo_read_lock().
158158 */
159159 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 *))
162162 {
163163 const struct tomoyo_domain_info *domain = r->domain;
164164 struct tomoyo_acl_info *ptr;
165− bool retried = false;
166165 const struct list_head *list = &domain->acl_info_list;
166+ u16 i = 0;
167167
168168 retry:
169169 list_for_each_entry_rcu(ptr, list, list) {
@@ -177,9 +177,10 @@ retry: · 7 unmodified lines
177177 r->granted = true;
178178 return;
179179 }
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++];
183184 goto retry;
184185 }
185186 r->granted = false;
@@ -198,6 +199,7 @@ LIST_HEAD(tomoyo_domain_list); · 12 unmodified lines
198199 static const char *tomoyo_last_word(const char *name)
199200 {
200201 const char *cp = strrchr(name, ' ');
202+
201203 if (cp)
202204 return cp + 1;
203205 return name;
@@ -220,6 +222,7 @@ static bool tomoyo_same_transition_control(const struct tomoyo_acl_head *a, · 16 unmodified lines
220222 const struct tomoyo_transition_control *p2 = container_of(b,
221223 typeof(*p2),
222224 head);
225+
223226 return p1->type == p2->type && p1->is_last_name == p2->is_last_name
224227 && p1->domainname == p2->domainname
225228 && p1->program == p2->program;
@@ -240,6 +243,7 @@ int tomoyo_write_transition_control(struct tomoyo_acl_param *param, · 14 unmodified lines
240243 int error = param->is_delete ? -ENOENT : -ENOMEM;
241244 char *program = param->data;
242245 char *domainname = strstr(program, " from ");
246+
243247 if (domainname) {
244248 *domainname = '\0';
245249 domainname += 6;
@@ -293,6 +297,7 @@ static inline bool tomoyo_scan_transition · 47 unmodified lines
293297 const enum tomoyo_transition_type type)
294298 {
295299 const struct tomoyo_transition_control *ptr;
300+
296301 list_for_each_entry_rcu(ptr, list, head.list) {
297302 if (ptr->head.is_deleted || ptr->type != type)
298303 continue;
@@ -338,9 +343,11 @@ static enum tomoyo_transition_type tomoyo_transition_type · 39 unmodified lines
338343 {
339344 const char *last_name = tomoyo_last_word(domainname->name);
340345 enum tomoyo_transition_type type = TOMOYO_TRANSITION_CONTROL_NO_RESET;
346+
341347 while (type < TOMOYO_MAX_TRANSITION_TYPE) {
342348 const struct list_head * const list =
343349 &ns->policy_list[TOMOYO_ID_TRANSITION_CONTROL];
350+
344351 if (!tomoyo_scan_transition(list, domainname, program,
345352 last_name, type)) {
346353 type++;
@@ -375,6 +382,7 @@ static bool tomoyo_same_aggregator(const struct tomoyo_acl_head *a, · 28 unmodified lines
375382 head);
376383 const struct tomoyo_aggregator *p2 = container_of(b, typeof(*p2),
377384 head);
385+
378386 return p1->original_name == p2->original_name &&
379387 p1->aggregated_name == p2->aggregated_name;
380388 }
@@ -394,6 +402,7 @@ int tomoyo_write_aggregator(struct tomoyo_acl_param *param) · 13 unmodified lines
394402 int error = param->is_delete ? -ENOENT : -ENOMEM;
395403 const char *original_name = tomoyo_read_token(param);
396404 const char *aggregated_name = tomoyo_read_token(param);
405+
397406 if (!tomoyo_correct_word(original_name) ||
398407 !tomoyo_correct_path(aggregated_name))
399408 return -EINVAL;
@@ -426,6 +435,7 @@ static struct tomoyo_policy_namespace *tomoyo_find_namespace · 26 unmodified lines
426435 (const char *name, const unsigned int len)
427436 {
428437 struct tomoyo_policy_namespace *ns;
438+
429439 list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
430440 if (strncmp(name, ns->name, len) ||
431441 (name[len] && name[len] != ' '))
@@ -451,6 +461,7 @@ struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname) · 19 unmodified lines
451461 struct tomoyo_policy_namespace *entry;
452462 const char *cp = domainname;
453463 unsigned int len = 0;
464+
454465 while (*cp && *cp++ != ' ')
455466 len++;
456467 ptr = tomoyo_find_namespace(domainname, len);
@@ -466,6 +477,7 @@ struct tomoyo_policy_namespace *tomoyo_assign_namespace(const char *domainname) · 9 unmodified lines
466477 ptr = tomoyo_find_namespace(domainname, len);
467478 if (!ptr && tomoyo_memory_ok(entry)) {
468479 char *name = (char *) (entry + 1);
480+
469481 ptr = entry;
470482 memmove(name, domainname, len);
471483 name[len] = '\0';
@@ -490,6 +502,7 @@ static bool tomoyo_namespace_jump(const char *domainname) · 18 unmodified lines
490502 {
491503 const char *namespace = tomoyo_current_namespace()->name;
492504 const int len = strlen(namespace);
505+
493506 return strncmp(domainname, namespace, len) ||
494507 (domainname[len] && domainname[len] != ' ');
495508 }
@@ -510,6 +523,7 @@ struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, · 14 unmodified lines
510523 struct tomoyo_domain_info e = { };
511524 struct tomoyo_domain_info *entry = tomoyo_find_domain(domainname);
512525 bool created = false;
526+
513527 if (entry) {
514528 if (transit) {
515529 /*
@@ -546,8 +560,9 @@ struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname, · 30 unmodified lines
546560 */
547561 if (transit) {
548562 const struct tomoyo_domain_info *domain = tomoyo_domain();
563+
549564 e.profile = domain->profile;
550− e.group = domain->group;
565+ memcpy(e.group, domain->group, sizeof(e.group));
551566 }
552567 e.domainname = tomoyo_get_name(domainname);
553568 if (!e.domainname)
@@ -569,12 +584,17 @@ out: · 15 unmodified lines
569584 if (entry && transit) {
570585 if (created) {
571586 struct tomoyo_request_info r;
587+ int i;
588+
572589 tomoyo_init_request_info(&r, entry,
573590 TOMOYO_MAC_FILE_EXECUTE);
574591 r.granted = false;
575592 tomoyo_write_log(&r, "use_profile %u\n",
576593 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);
578598 tomoyo_update_stat(TOMOYO_STAT_POLICY_UPDATES);
579599 }
580600 }
@@ -712,6 +732,7 @@ retry: · 131 unmodified lines
712732 struct tomoyo_aggregator *ptr;
713733 struct list_head *list =
714734 &old_domain->ns->policy_list[TOMOYO_ID_AGGREGATOR];
735+
715736 /* Check 'aggregator' directive. */
716737 candidate = &exename;
717738 list_for_each_entry_rcu(ptr, list, head.list) {
@@ -747,6 +768,7 @@ retry: · 29 unmodified lines
747768 */
748769 if (ee->transition) {
749770 const char *domainname = ee->transition->name;
771+
750772 reject_on_transition_failure = true;
751773 if (!strcmp(domainname, "keep"))
752774 goto force_keep_domain;
@@ -758,6 +780,7 @@ retry: · 5 unmodified lines
758780 goto force_initialize_domain;
759781 if (!strcmp(domainname, "parent")) {
760782 char *cp;
783+
761784 strncpy(ee->tmp, old_domain->domainname->name,
762785 TOMOYO_EXEC_TMPSIZE - 1);
763786 cp = strrchr(ee->tmp, ' ');
@@ -822,8 +845,7 @@ force_jump_domain: · 58 unmodified lines
822845 if (domain)
823846 retval = 0;
824847 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);
827849 retval = -ENOMEM;
828850 } else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
829851 retval = -ENOMEM;
@@ -834,16 +856,20 @@ force_jump_domain: · 4 unmodified lines
834856 ee->r.granted = false;
835857 tomoyo_write_log(&ee->r, "%s", tomoyo_dif
836858 [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);
839860 }
840861 }
841862 out:
842863 if (!domain)
843864 domain = old_domain;
844865 /* 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+ }
847873 kfree(exename.name);
848874 if (!retval) {
849875 ee->r.domain = domain;
security/tomoyo/file.c+20−0
@@ -214,6 +214,7 @@ static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r) · 213 unmodified lines
214214 const u8 type = r->param.path_number.operation;
215215 u8 radix;
216216 char buffer[64];
217+
217218 switch (type) {
218219 case TOMOYO_TYPE_CREATE:
219220 case TOMOYO_TYPE_MKDIR:
@@ -253,6 +254,7 @@ static bool tomoyo_check_path_acl(struct tomoyo_request_info *r, · 33 unmodified lines
253254 {
254255 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
255256 head);
257+
256258 if (acl->perm & (1 << r->param.path.operation)) {
257259 r->param.path.matched_path =
258260 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
275277 {
276278 const struct tomoyo_path_number_acl *acl =
277279 container_of(ptr, typeof(*acl), head);
280+
278281 return (acl->perm & (1 << r->param.path_number.operation)) &&
279282 tomoyo_compare_number_union(r->param.path_number.number,
280283 &acl->number) &&
@@ -295,6 +298,7 @@ static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r, · 14 unmodified lines
295298 {
296299 const struct tomoyo_path2_acl *acl =
297300 container_of(ptr, typeof(*acl), head);
301+
298302 return (acl->perm & (1 << r->param.path2.operation)) &&
299303 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
300304 && 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
314318 {
315319 const struct tomoyo_mkdev_acl *acl =
316320 container_of(ptr, typeof(*acl), head);
321+
317322 return (acl->perm & (1 << r->param.mkdev.operation)) &&
318323 tomoyo_compare_number_union(r->param.mkdev.mode,
319324 &acl->mode) &&
@@ -338,6 +343,7 @@ static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a, · 18 unmodified lines
338343 {
339344 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
340345 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
346+
341347 return tomoyo_same_name_union(&p1->name, &p2->name);
342348 }
343349
@@ -358,6 +364,7 @@ static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a, · 14 unmodified lines
358364 ->perm;
359365 u16 perm = *a_perm;
360366 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
367+
361368 if (is_delete)
362369 perm &= ~b_perm;
363370 else
@@ -384,6 +391,7 @@ static int tomoyo_update_path_acl(const u16 perm, · 20 unmodified lines
384391 .perm = perm
385392 };
386393 int error;
394+
387395 if (!tomoyo_parse_name_union(param, &e.name))
388396 error = -EINVAL;
389397 else
@@ -407,6 +415,7 @@ static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a, · 17 unmodified lines
407415 {
408416 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
409417 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
418+
410419 return tomoyo_same_name_union(&p1->name, &p2->name) &&
411420 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
412421 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
431440 u8 perm = *a_perm;
432441 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
433442 ->perm;
443+
434444 if (is_delete)
435445 perm &= ~b_perm;
436446 else
@@ -457,6 +467,7 @@ static int tomoyo_update_mkdev_acl(const u8 perm, · 20 unmodified lines
457467 .perm = perm
458468 };
459469 int error;
470+
460471 if (!tomoyo_parse_name_union(param, &e.name) ||
461472 !tomoyo_parse_number_union(param, &e.mode) ||
462473 !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
486497 {
487498 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
488499 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
500+
489501 return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
490502 tomoyo_same_name_union(&p1->name2, &p2->name2);
491503 }
@@ -507,6 +519,7 @@ static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a, · 15 unmodified lines
507519 ->perm;
508520 u8 perm = *a_perm;
509521 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
522+
510523 if (is_delete)
511524 perm &= ~b_perm;
512525 else
@@ -533,6 +546,7 @@ static int tomoyo_update_path2_acl(const u8 perm, · 20 unmodified lines
533546 .perm = perm
534547 };
535548 int error;
549+
536550 if (!tomoyo_parse_name_union(param, &e.name1) ||
537551 !tomoyo_parse_name_union(param, &e.name2))
538552 error = -EINVAL;
@@ -621,6 +635,7 @@ static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a, · 82 unmodified lines
621635 head);
622636 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
623637 head);
638+
624639 return tomoyo_same_name_union(&p1->name, &p2->name) &&
625640 tomoyo_same_number_union(&p1->number, &p2->number);
626641 }
@@ -643,6 +658,7 @@ static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a, · 16 unmodified lines
643658 u8 perm = *a_perm;
644659 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
645660 ->perm;
661+
646662 if (is_delete)
647663 perm &= ~b_perm;
648664 else
@@ -667,6 +683,7 @@ static int tomoyo_update_path_number_acl(const u8 perm, · 18 unmodified lines
667683 .perm = perm
668684 };
669685 int error;
686+
670687 if (!tomoyo_parse_name_union(param, &e.name) ||
671688 !tomoyo_parse_number_union(param, &e.number))
672689 error = -EINVAL;
@@ -947,6 +964,7 @@ static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a, · 274 unmodified lines
947964 {
948965 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
949966 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
967+
950968 return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
951969 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
952970 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
966984 {
967985 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
968986 int error;
987+
969988 if (!tomoyo_parse_name_union(param, &e.dev_name) ||
970989 !tomoyo_parse_name_union(param, &e.dir_name) ||
971990 !tomoyo_parse_name_union(param, &e.fs_type) ||
@@ -995,6 +1014,7 @@ int tomoyo_write_file(struct tomoyo_acl_param *param) · 23 unmodified lines
9951014 u16 perm = 0;
9961015 u8 type;
9971016 const char *operation = tomoyo_read_token(param);
1017+
9981018 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
9991019 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
10001020 perm |= 1 << type;
security/tomoyo/gc.c+19−0
@@ -77,11 +77,13 @@ static bool tomoyo_name_used_by_io_buffer(const char *string) · 76 unmodified lines
7777 spin_lock(&tomoyo_io_buffer_list_lock);
7878 list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
7979 int i;
80+
8081 head->users++;
8182 spin_unlock(&tomoyo_io_buffer_list_lock);
8283 mutex_lock(&head->io_sem);
8384 for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) {
8485 const char *w = head->r.w[i];
86+
8587 if (w < string || w > string + size)
8688 continue;
8789 in_use = true;
@@ -108,6 +110,7 @@ static inline void tomoyo_del_transition_control(struct list_head *element) · 20 unmodified lines
108110 {
109111 struct tomoyo_transition_control *ptr =
110112 container_of(element, typeof(*ptr), head.list);
113+
111114 tomoyo_put_name(ptr->domainname);
112115 tomoyo_put_name(ptr->program);
113116 }
@@ -123,6 +126,7 @@ static inline void tomoyo_del_aggregator(struct list_head *element) · 9 unmodified lines
123126 {
124127 struct tomoyo_aggregator *ptr =
125128 container_of(element, typeof(*ptr), head.list);
129+
126130 tomoyo_put_name(ptr->original_name);
127131 tomoyo_put_name(ptr->aggregated_name);
128132 }
@@ -138,6 +142,7 @@ static inline void tomoyo_del_manager(struct list_head *element) · 9 unmodified lines
138142 {
139143 struct tomoyo_manager *ptr =
140144 container_of(element, typeof(*ptr), head.list);
145+
141146 tomoyo_put_name(ptr->manager);
142147 }
143148
@@ -152,6 +157,7 @@ static void tomoyo_del_acl(struct list_head *element) · 8 unmodified lines
152157 {
153158 struct tomoyo_acl_info *acl =
154159 container_of(element, typeof(*acl), list);
160+
155161 tomoyo_put_condition(acl->cond);
156162 switch (acl->type) {
157163 case TOMOYO_TYPE_PATH_ACL:
@@ -226,6 +232,7 @@ static void tomoyo_del_acl(struct list_head *element) · 68 unmodified lines
226232 {
227233 struct tomoyo_task_acl *entry =
228234 container_of(acl, typeof(*entry), head);
235+
229236 tomoyo_put_name(entry->domainname);
230237 }
231238 break;
@@ -247,6 +254,7 @@ static inline void tomoyo_del_domain(struct list_head *element) · 15 unmodified lines
247254 container_of(element, typeof(*domain), list);
248255 struct tomoyo_acl_info *acl;
249256 struct tomoyo_acl_info *tmp;
257+
250258 /*
251259 * Since this domain is referenced from neither
252260 * "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
286294 = (const struct tomoyo_argv *) (names_p + names_count);
287295 const struct tomoyo_envp *envp
288296 = (const struct tomoyo_envp *) (argv + argc);
297+
289298 for (i = 0; i < numbers_count; i++)
290299 tomoyo_put_number_union(numbers_p++);
291300 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
321330 {
322331 struct tomoyo_path_group *member =
323332 container_of(element, typeof(*member), head.list);
333+
324334 tomoyo_put_name(member->member_name);
325335 }
326336
@@ -335,6 +345,7 @@ static inline void tomoyo_del_group(struct list_head *element) · 8 unmodified lines
335345 {
336346 struct tomoyo_group *group =
337347 container_of(element, typeof(*group), head.list);
348+
338349 tomoyo_put_name(group->group_name);
339350 }
340351
@@ -476,6 +487,7 @@ static void tomoyo_collect_member(const enum tomoyo_policy_id id, · 135 unmodified lines
476487 {
477488 struct tomoyo_acl_head *member;
478489 struct tomoyo_acl_head *tmp;
490+
479491 list_for_each_entry_safe(member, tmp, member_list, list) {
480492 if (!member->is_deleted)
481493 continue;
@@ -495,6 +507,7 @@ static void tomoyo_collect_acl(struct list_head *list) · 13 unmodified lines
495507 {
496508 struct tomoyo_acl_info *acl;
497509 struct tomoyo_acl_info *tmp;
510+
498511 list_for_each_entry_safe(acl, tmp, list, list) {
499512 if (!acl->is_deleted)
500513 continue;
@@ -513,10 +526,12 @@ static void tomoyo_collect_entry(void) · 12 unmodified lines
513526 int i;
514527 enum tomoyo_policy_id id;
515528 struct tomoyo_policy_namespace *ns;
529+
516530 mutex_lock(&tomoyo_policy_lock);
517531 {
518532 struct tomoyo_domain_info *domain;
519533 struct tomoyo_domain_info *tmp;
534+
520535 list_for_each_entry_safe(domain, tmp, &tomoyo_domain_list,
521536 list) {
522537 tomoyo_collect_acl(&domain->acl_info_list);
@@ -534,6 +549,7 @@ static void tomoyo_collect_entry(void) · 11 unmodified lines
534549 {
535550 struct tomoyo_shared_acl_head *ptr;
536551 struct tomoyo_shared_acl_head *tmp;
552+
537553 list_for_each_entry_safe(ptr, tmp, &tomoyo_condition_list,
538554 list) {
539555 if (atomic_read(&ptr->users) > 0)
@@ -547,6 +563,7 @@ static void tomoyo_collect_entry(void) · 7 unmodified lines
547563 struct list_head *list = &ns->group_list[i];
548564 struct tomoyo_group *group;
549565 struct tomoyo_group *tmp;
566+
550567 switch (i) {
551568 case 0:
552569 id = TOMOYO_ID_PATH_GROUP;
@@ -574,6 +591,7 @@ static void tomoyo_collect_entry(void) · 21 unmodified lines
574591 struct list_head *list = &tomoyo_name_list[i];
575592 struct tomoyo_shared_acl_head *ptr;
576593 struct tomoyo_shared_acl_head *tmp;
594+
577595 list_for_each_entry_safe(ptr, tmp, list, list) {
578596 if (atomic_read(&ptr->users) > 0)
579597 continue;
@@ -595,6 +613,7 @@ static int tomoyo_gc_thread(void *unused) · 15 unmodified lines
595613 {
596614 /* Garbage collector thread is exclusive. */
597615 static DEFINE_MUTEX(tomoyo_gc_mutex);
616+
598617 if (!mutex_trylock(&tomoyo_gc_mutex))
599618 goto out;
600619 tomoyo_collect_entry();
security/tomoyo/group.c+5−0
@@ -75,11 +75,13 @@ int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type) · 74 unmodified lines
7575 {
7676 struct tomoyo_group *group = tomoyo_get_group(param, type);
7777 int error = -EINVAL;
78+
7879 if (!group)
7980 return -ENOMEM;
8081 param->list = &group->member_list;
8182 if (type == TOMOYO_PATH_GROUP) {
8283 struct tomoyo_path_group e = { };
84+
8385 e.member_name = tomoyo_get_name(tomoyo_read_token(param));
8486 if (!e.member_name) {
8587 error = -ENOMEM;
@@ -90,6 +92,7 @@ int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type) · 4 unmodified lines
9092 tomoyo_put_name(e.member_name);
9193 } else if (type == TOMOYO_NUMBER_GROUP) {
9294 struct tomoyo_number_group e = { };
95+
9396 if (param->data[0] == '@' ||
9497 !tomoyo_parse_number_union(param, &e.number))
9598 goto out;
@@ -129,6 +132,7 @@ tomoyo_path_matches_group(const struct tomoyo_path_info *pathname, · 33 unmodified lines
129132 const struct tomoyo_group *group)
130133 {
131134 struct tomoyo_path_group *member;
135+
132136 list_for_each_entry_rcu(member, &group->member_list, head.list) {
133137 if (member->head.is_deleted)
134138 continue;
@@ -156,6 +160,7 @@ bool tomoyo_number_matches_group(const unsigned long min, · 21 unmodified lines
156160 {
157161 struct tomoyo_number_group *member;
158162 bool matched = false;
163+
159164 list_for_each_entry_rcu(member, &group->member_list, head.list) {
160165 if (member->head.is_deleted)
161166 continue;
security/tomoyo/load_policy.c+4−4
@@ -37,11 +37,12 @@ __setup("TOMOYO_loader=", tomoyo_loader_setup); · 36 unmodified lines
3737 static bool tomoyo_policy_loader_exists(void)
3838 {
3939 struct path path;
40+
4041 if (!tomoyo_loader)
4142 tomoyo_loader = CONFIG_SECURITY_TOMOYO_POLICY_LOADER;
4243 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);
4546 return false;
4647 }
4748 path_put(&path);
@@ -96,8 +97,7 @@ void tomoyo_load_policy(const char *filename) · 48 unmodified lines
9697 if (!tomoyo_policy_loader_exists())
9798 return;
9899 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);
101101 argv[0] = (char *) tomoyo_loader;
102102 argv[1] = NULL;
103103 envp[0] = "HOME=/";
security/tomoyo/memory.c+7−2
@@ -19,9 +19,9 @@ void tomoyo_warn_oom(const char *function) · 18 unmodified lines
1919 /* Reduce error messages. */
2020 static pid_t tomoyo_last_pid;
2121 const pid_t pid = current->pid;
22+
2223 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);
2525 tomoyo_last_pid = pid;
2626 }
2727 if (!tomoyo_policy_loaded)
@@ -48,6 +48,7 @@ bool tomoyo_memory_ok(void *ptr) · 20 unmodified lines
4848 {
4949 if (ptr) {
5050 const size_t s = ksize(ptr);
51+
5152 tomoyo_memory_used[TOMOYO_MEMORY_POLICY] += s;
5253 if (!tomoyo_memory_quota[TOMOYO_MEMORY_POLICY] ||
5354 tomoyo_memory_used[TOMOYO_MEMORY_POLICY] <=
@@ -73,6 +74,7 @@ bool tomoyo_memory_ok(void *ptr) · 19 unmodified lines
7374 void *tomoyo_commit_ok(void *data, const unsigned int size)
7475 {
7576 void *ptr = kzalloc(size, GFP_NOFS);
77+
7678 if (tomoyo_memory_ok(ptr)) {
7779 memmove(ptr, data, size);
7880 memset(data, 0, size);
@@ -98,6 +100,7 @@ struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param, · 19 unmodified lines
98100 struct list_head *list;
99101 const char *group_name = tomoyo_read_token(param);
100102 bool found = false;
103+
101104 if (!tomoyo_correct_word(group_name) || idx >= TOMOYO_MAX_GROUP)
102105 return NULL;
103106 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
116119 }
117120 if (!found) {
118121 struct tomoyo_group *entry = tomoyo_commit_ok(&e, sizeof(e));
122+
119123 if (entry) {
120124 INIT_LIST_HEAD(&entry->member_list);
121125 atomic_set(&entry->head.users, 1);
@@ -191,6 +195,7 @@ struct tomoyo_policy_namespace tomoyo_kernel_namespace; · 69 unmodified lines
191195 void __init tomoyo_mm_init(void)
192196 {
193197 int idx;
198+
194199 for (idx = 0; idx < TOMOYO_MAX_HASH; idx++)
195200 INIT_LIST_HEAD(&tomoyo_name_list[idx]);
196201 tomoyo_kernel_namespace.name = "<kernel>";
security/tomoyo/mount.c+2−0
@@ -49,6 +49,7 @@ static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r, · 48 unmodified lines
4949 {
5050 const struct tomoyo_mount_acl *acl =
5151 container_of(ptr, typeof(*acl), head);
52+
5253 return tomoyo_compare_number_union(r->param.mount.flags,
5354 &acl->flags) &&
5455 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
8990 struct tomoyo_path_info rdir;
9091 int need_dev = 0;
9192 int error = -ENOMEM;
93+
9294 r->obj = &obj;
9395
9496 /* Get fstype. */
security/tomoyo/realpath.c+15−3
@@ -94,11 +94,13 @@ static char *tomoyo_get_absolute_path(const struct path *path, char * const buff · 93 unmodified lines
9494 const int buflen)
9595 {
9696 char *pos = ERR_PTR(-ENOMEM);
97+
9798 if (buflen >= 256) {
9899 /* go to whatever namespace root we are under */
99100 pos = d_absolute_path(path, buffer, buflen - 1);
100101 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
101102 struct inode *inode = d_backing_inode(path->dentry);
103+
102104 if (inode && S_ISDIR(inode->i_mode)) {
103105 buffer[buflen - 2] = '/';
104106 buffer[buflen - 1] = '\0';
@@ -123,10 +125,12 @@ static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer, · 18 unmodified lines
123125 const int buflen)
124126 {
125127 char *pos = ERR_PTR(-ENOMEM);
128+
126129 if (buflen >= 256) {
127130 pos = dentry_path_raw(dentry, buffer, buflen - 1);
128131 if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
129132 struct inode *inode = d_backing_inode(dentry);
133+
130134 if (inode && S_ISDIR(inode->i_mode)) {
131135 buffer[buflen - 2] = '/';
132136 buffer[buflen - 1] = '\0';
@@ -150,12 +154,14 @@ static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, · 17 unmodified lines
150154 {
151155 struct super_block *sb = dentry->d_sb;
152156 char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen);
157+
153158 if (IS_ERR(pos))
154159 return pos;
155160 /* Convert from $PID to self if $PID is current thread. */
156161 if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
157162 char *ep;
158163 const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
164+
159165 if (*ep == '/' && pid && pid ==
160166 task_tgid_nr_ns(current, sb->s_fs_info)) {
161167 pos = ep - 5;
@@ -170,6 +176,7 @@ static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, · 8 unmodified lines
170176 goto prepend_filesystem_name;
171177 {
172178 struct inode *inode = d_backing_inode(sb->s_root);
179+
173180 /*
174181 * Use filesystem name if filesystem does not support rename()
175182 * operation.
@@ -182,6 +189,7 @@ static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, · 6 unmodified lines
182189 char name[64];
183190 int name_len;
184191 const dev_t dev = sb->s_dev;
192+
185193 name[sizeof(name) - 1] = '\0';
186194 snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
187195 MINOR(dev));
@@ -197,6 +205,7 @@ prepend_filesystem_name: · 9 unmodified lines
197205 {
198206 const char *name = sb->s_type->name;
199207 const int name_len = strlen(name);
208+
200209 pos -= name_len + 1;
201210 if (pos < buffer)
202211 goto out;
@@ -223,10 +232,10 @@ static char *tomoyo_get_socket_name(const struct path *path, char * const buffer · 20 unmodified lines
223232 struct inode *inode = d_backing_inode(path->dentry);
224233 struct socket *sock = inode ? SOCKET_I(inode) : NULL;
225234 struct sock *sk = sock ? sock->sk : NULL;
235+
226236 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);
230239 } else {
231240 snprintf(buffer, buflen, "socket:[unknown]");
232241 }
@@ -255,12 +264,14 @@ char *tomoyo_realpath_from_path(const struct path *path) · 22 unmodified lines
255264 unsigned int buf_len = PAGE_SIZE / 2;
256265 struct dentry *dentry = path->dentry;
257266 struct super_block *sb;
267+
258268 if (!dentry)
259269 return NULL;
260270 sb = dentry->d_sb;
261271 while (1) {
262272 char *pos;
263273 struct inode *inode;
274+
264275 buf_len <<= 1;
265276 kfree(buf);
266277 buf = kmalloc(buf_len, GFP_NOFS);
@@ -323,6 +334,7 @@ char *tomoyo_realpath_nofollow(const char *pathname) · 56 unmodified lines
323334
324335 if (pathname && kern_path(pathname, 0, &path) == 0) {
325336 char *buf = tomoyo_realpath_from_path(&path);
337+
326338 path_put(&path);
327339 return buf;
328340 }
security/tomoyo/securityfs_if.c+17−13
@@ -21,6 +21,7 @@ static bool tomoyo_check_task_acl(struct tomoyo_request_info *r, · 20 unmodified lines
2121 {
2222 const struct tomoyo_task_acl *acl = container_of(ptr, typeof(*acl),
2323 head);
24+
2425 return !tomoyo_pathcmp(r->param.task.domainname, acl->domainname);
2526 }
2627
@@ -42,6 +43,7 @@ static ssize_t tomoyo_write_self(struct file *file, const char __user *buf, · 15 unmodified lines
4243 {
4344 char *data;
4445 int error;
46+
4547 if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10)
4648 return -ENOMEM;
4749 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
5254 const int idx = tomoyo_read_lock();
5355 struct tomoyo_path_info name;
5456 struct tomoyo_request_info r;
57+
5558 name.name = data;
5659 tomoyo_fill_path_info(&name);
5760 /* 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
6770 if (!new_domain) {
6871 error = -ENOENT;
6972 } 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;
8281 }
8382 }
8483 tomoyo_read_unlock(idx);
@@ -104,6 +103,7 @@ static ssize_t tomoyo_read_self(struct file *file, char __user *buf, · 19 unmodified lines
104103 const char *domain = tomoyo_domain()->domainname->name;
105104 loff_t len = strlen(domain);
106105 loff_t pos = *ppos;
106+
107107 if (pos >= len || !count)
108108 return 0;
109109 len -= pos;
@@ -234,10 +234,14 @@ static void __init tomoyo_create_entry(const char *name, const umode_t mode, · 124 unmodified lines
234234 */
235235 static int __init tomoyo_initerface_init(void)
236236 {
237+ struct tomoyo_domain_info *domain;
237238 struct dentry *tomoyo_dir;
238239
240+ if (!tomoyo_enabled)
241+ return 0;
242+ domain = tomoyo_domain();
239243 /* Don't create securityfs entries unless registered. */
240− if (current_cred()->security != &tomoyo_kernel_domain)
244+ if (domain != &tomoyo_kernel_domain)
241245 return 0;
242246
243247 tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
security/tomoyo/tomoyo.c+102−58
@@ -9,17 +9,19 @@ · 8 unmodified lines
99 #include "common.h"
1010
1111 /**
12− * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
12+ * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
1313 *
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.
1815 */
19−static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
16+struct tomoyo_domain_info *tomoyo_domain(void)
2017 {
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;
2325 }
2426
2527 /**
@@ -34,42 +36,38 @@ static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp) · 8 unmodified lines
3436 static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
3537 gfp_t gfp)
3638 {
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+ }
4147 return 0;
4248 }
4349
4450 /**
45− * tomoyo_cred_transfer - Target for security_transfer_creds().
51+ * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds().
4652 *
47− * @new: Pointer to "struct cred".
48− * @old: Pointer to "struct cred".
53+ * @bprm: Pointer to "struct linux_binprm".
4954 */
50−static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
55+static void tomoyo_bprm_committed_creds(struct linux_binprm *bprm)
5156 {
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);
5459
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;
6562 }
6663
64+#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
6765 /**
6866 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
6967 *
7068 * @bprm: Pointer to "struct linux_binprm".
7169 *
72− * Returns 0 on success, negative value otherwise.
70+ * Returns 0.
7371 */
7472 static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
7573 {
@@ -79,29 +77,15 @@ static int tomoyo_bprm_set_creds(struct linux_binprm *bprm) · 3 unmodified lines
7977 */
8078 if (bprm->called_set_creds)
8179 return 0;
82−#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
8380 /*
8481 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
8582 * for the first time.
8683 */
8784 if (!tomoyo_policy_loaded)
8885 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;
10386 return 0;
10487 }
88+#endif
10589
10690 /**
10791 * 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
11296 */
11397 static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
11498 {
115− struct tomoyo_domain_info *domain = bprm->cred->security;
99+ struct tomoyo_task *s = tomoyo_task(current);
116100
117101 /*
118102 * Execute permission is checked against pathname passed to do_execve()
119103 * using current domain.
120104 */
121− if (!domain) {
105+ if (!s->old_domain_info) {
122106 const int idx = tomoyo_read_lock();
123107 const int err = tomoyo_find_next_domain(bprm);
108+
124109 tomoyo_read_unlock(idx);
125110 return err;
126111 }
127112 /*
128113 * Read permission is checked against interpreters using next domain.
129114 */
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);
132117 }
133118
134119 /**
@@ -167,6 +152,7 @@ static int tomoyo_path_truncate(const struct path *path) · 32 unmodified lines
167152 static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
168153 {
169154 struct path path = { .mnt = parent->mnt, .dentry = dentry };
155+
170156 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
171157 }
172158
@@ -183,6 +169,7 @@ static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry, · 10 unmodified lines
183169 umode_t mode)
184170 {
185171 struct path path = { .mnt = parent->mnt, .dentry = dentry };
172+
186173 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
187174 mode & S_IALLUGO);
188175 }
@@ -198,6 +185,7 @@ static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry, · 9 unmodified lines
198185 static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
199186 {
200187 struct path path = { .mnt = parent->mnt, .dentry = dentry };
188+
201189 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
202190 }
203191
@@ -214,6 +202,7 @@ static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry, · 10 unmodified lines
214202 const char *old_name)
215203 {
216204 struct path path = { .mnt = parent->mnt, .dentry = dentry };
205+
217206 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
218207 }
219208
@@ -271,6 +260,7 @@ static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_di · 51 unmodified lines
271260 {
272261 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
273262 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
263+
274264 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
275265 }
276266
@@ -291,6 +281,7 @@ static int tomoyo_path_rename(const struct path *old_parent, · 14 unmodified lines
291281 {
292282 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
293283 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
284+
294285 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
295286 }
296287
@@ -322,11 +313,11 @@ static int tomoyo_file_fcntl(struct file *file, unsigned int cmd, · 25 unmodified lines
322313 */
323314 static int tomoyo_file_open(struct file *f)
324315 {
325− int flags = f->f_flags;
326316 /* Don't check read permission here if called from do_execve(). */
327317 if (current->in_execve)
328318 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);
330321 }
331322
332323 /**
@@ -370,6 +361,7 @@ static int tomoyo_path_chmod(const struct path *path, umode_t mode) · 37 unmodified lines
370361 static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
371362 {
372363 int error = 0;
364+
373365 if (uid_valid(uid))
374366 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
375367 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
419411 static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
420412 {
421413 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
414+
422415 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
423416 }
424417
@@ -493,16 +486,61 @@ static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg, · 68 unmodified lines
493486 return tomoyo_socket_sendmsg_permission(sock, msg, size);
494487 }
495488
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+
496532 /*
497533 * tomoyo_security_ops is a "struct security_operations" which is used for
498534 * registering TOMOYO.
499535 */
500536 static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
501− LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank),
502537 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
505542 LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds),
543+#endif
506544 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
507545 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
508546 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
531569 /* Lock for GC. */
532570 DEFINE_SRCU(tomoyo_ss);
533571
572+int tomoyo_enabled __lsm_ro_after_init = 1;
573+
534574 /**
535575 * tomoyo_init - Register TOMOYO Linux as a LSM module.
536576 *
@@ -538,19 +578,23 @@ DEFINE_SRCU(tomoyo_ss); · 1 unmodified line
538578 */
539579 static int __init tomoyo_init(void)
540580 {
541− struct cred *cred = (struct cred *) current_cred();
581+ struct tomoyo_task *s = tomoyo_task(current);
542582
543− if (!security_module_enable("tomoyo"))
544− return 0;
545583 /* register ourselves with the security framework */
546584 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;
549589 tomoyo_mm_init();
590+
550591 return 0;
551592 }
552593
553594 DEFINE_LSM(tomoyo) = {
554595 .name = "tomoyo",
596+ .enabled = &tomoyo_enabled,
597+ .flags = LSM_FLAG_LEGACY_MAJOR,
598+ .blobs = &tomoyo_blob_sizes,
555599 .init = tomoyo_init,
556600 };
security/tomoyo/util.c+18−5
@@ -91,6 +91,7 @@ const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = { · 90 unmodified lines
9191 void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp)
9292 {
9393 struct tm tm;
94+
9495 time64_to_tm(time64, 0, &tm);
9596 stamp->sec = tm.tm_sec;
9697 stamp->min = tm.tm_min;
@@ -113,6 +114,7 @@ void tomoyo_convert_time(time64_t time64, struct tomoyo_time *stamp) · 16 unmodified lines
113114 bool tomoyo_permstr(const char *string, const char *keyword)
114115 {
115116 const char *cp = strstr(string, keyword);
117+
116118 if (cp)
117119 return cp == string || *(cp - 1) == '/';
118120 return false;
@@ -132,6 +134,7 @@ char *tomoyo_read_token(struct tomoyo_acl_param *param) · 13 unmodified lines
132134 {
133135 char *pos = param->data;
134136 char *del = strchr(pos, ' ');
137+
135138 if (del)
136139 *del++ = '\0';
137140 else
@@ -152,6 +155,7 @@ const struct tomoyo_path_info *tomoyo_get_domainname · 14 unmodified lines
152155 {
153156 char *start = param->data;
154157 char *pos = start;
158+
155159 while (*pos) {
156160 if (*pos++ != ' ' || *pos++ == '/')
157161 continue;
@@ -181,8 +185,10 @@ u8 tomoyo_parse_ulong(unsigned long *result, char **str) · 23 unmodified lines
181185 const char *cp = *str;
182186 char *ep;
183187 int base = 10;
188+
184189 if (*cp == '0') {
185190 char c = *(cp + 1);
191+
186192 if (c == 'x' || c == 'X') {
187193 base = 16;
188194 cp += 2;
@@ -240,6 +246,7 @@ bool tomoyo_parse_name_union(struct tomoyo_acl_param *param, · 51 unmodified lines
240246 struct tomoyo_name_union *ptr)
241247 {
242248 char *filename;
249+
243250 if (param->data[0] == '@') {
244251 param->data++;
245252 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
266273 char *data;
267274 u8 type;
268275 unsigned long v;
276+
269277 memset(ptr, 0, sizeof(*ptr));
270278 if (param->data[0] == '@') {
271279 param->data++;
@@ -429,6 +437,7 @@ static bool tomoyo_correct_word2(const char *string, size_t len) · 157 unmodified lines
429437 unsigned char c;
430438 unsigned char d;
431439 unsigned char e;
440+
432441 if (!len)
433442 goto out;
434443 while (len--) {
@@ -533,6 +542,7 @@ bool tomoyo_correct_domain(const unsigned char *domainname) · 98 unmodified lines
533542 return true;
534543 while (1) {
535544 const unsigned char *cp = strchr(domainname, ' ');
545+
536546 if (!cp)
537547 break;
538548 if (*domainname != '/' ||
@@ -554,6 +564,7 @@ bool tomoyo_domain_def(const unsigned char *buffer) · 15 unmodified lines
554564 {
555565 const unsigned char *cp;
556566 int len;
567+
557568 if (*buffer != '<')
558569 return false;
559570 cp = strchr(buffer, ' ');
@@ -668,6 +679,9 @@ static bool tomoyo_file_matches_pattern2(const char *filename, · 108 unmodified lines
668679 {
669680 while (filename < filename_end && pattern < pattern_end) {
670681 char c;
682+ int i;
683+ int j;
684+
671685 if (*pattern != '\\') {
672686 if (*filename++ != *pattern++)
673687 return false;
@@ -676,8 +690,6 @@ static bool tomoyo_file_matches_pattern2(const char *filename, · 2 unmodified lines
676690 c = *filename;
677691 pattern++;
678692 switch (*pattern) {
679− int i;
680− int j;
681693 case '?':
682694 if (c == '/') {
683695 return false;
@@ -985,6 +997,7 @@ int tomoyo_init_request_info(struct tomoyo_request_info *r, · 301 unmodified lines
985997 struct tomoyo_domain_info *domain, const u8 index)
986998 {
987999 u8 profile;
1000+
9881001 memset(r, 0, sizeof(*r));
9891002 if (!domain)
9901003 domain = tomoyo_domain();
@@ -1018,6 +1031,7 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r) · 27 unmodified lines
10181031 list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
10191032 u16 perm;
10201033 u8 i;
1034+
10211035 if (ptr->is_deleted)
10221036 continue;
10231037 switch (ptr->type) {
@@ -1062,9 +1076,8 @@ bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r) · 38 unmodified lines
10621076 domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true;
10631077 /* r->granted = false; */
10641078 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);
10681081 }
10691082 return false;
10701083 }
security/yama/yama_lsm.c+7−1
@@ -479,9 +479,15 @@ static void __init yama_init_sysctl(void) · 478 unmodified lines
479479 static inline void yama_init_sysctl(void) { }
480480 #endif /* CONFIG_SYSCTL */
481481
482−void __init yama_add_hooks(void)
482+static int __init yama_init(void)
483483 {
484484 pr_info("Yama: becoming mindful.\n");
485485 security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
486486 yama_init_sysctl();
487+ return 0;
487488 }
489+
490+DEFINE_LSM(yama) = {
491+ .name = "yama",
492+ .init = yama_init,
493+};
tools/testing/selftests/safesetid/.gitignore+1−0
@@ -0,0 +1 @@
1+safesetid-test
tools/testing/selftests/safesetid/Makefile+8−0
@@ -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
tools/testing/selftests/safesetid/config+2−0
@@ -0,0 +1,2 @@
1+CONFIG_SECURITY=y
2+CONFIG_SECURITYFS=y
tools/testing/selftests/safesetid/safesetid-test.c+334−0
@@ -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+}
tools/testing/selftests/safesetid/safesetid-test.sh+26−0
@@ -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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t