aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>2017-03-24 07:46:33 -0400
committerJames Morris <james.l.morris@oracle.com>2017-03-27 20:05:14 -0400
commite4e55b47ed9ae2c05ff062601ff6dacbe9dc4775 (patch)
tree24a332cc81000bf9bbd1e4db4dc17d16f29e1da8
parent840c91dc6a13b160f5b6e5c79c430dffac11c945 (diff)
LSM: Revive security_task_alloc() hook and per "struct task_struct" security blob.
We switched from "struct task_struct"->security to "struct cred"->security in Linux 2.6.29. But not all LSM modules were happy with that change. TOMOYO LSM module is an example which want to use per "struct task_struct" security blob, for TOMOYO's security context is defined based on "struct task_struct" rather than "struct cred". AppArmor LSM module is another example which want to use it, for AppArmor is currently abusing the cred a little bit to store the change_hat and setexeccon info. Although security_task_free() hook was revived in Linux 3.4 because Yama LSM module wanted to release per "struct task_struct" security blob, security_task_alloc() hook and "struct task_struct"->security field were not revived. Nowadays, we are getting proposals of lightweight LSM modules which want to use per "struct task_struct" security blob. We are already allowing multiple concurrent LSM modules (up to one fully armored module which uses "struct cred"->security field or exclusive hooks like security_xfrm_state_pol_flow_match(), plus unlimited number of lightweight modules which do not use "struct cred"->security nor exclusive hooks) as long as they are built into the kernel. But this patch does not implement variable length "struct task_struct"->security field which will become needed when multiple LSM modules want to use "struct task_struct"-> security field. Although it won't be difficult to implement variable length "struct task_struct"->security field, let's think about it after we merged this patch. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Acked-by: John Johansen <john.johansen@canonical.com> Acked-by: Serge Hallyn <serge@hallyn.com> Acked-by: Casey Schaufler <casey@schaufler-ca.com> Tested-by: Djalal Harouni <tixxdz@gmail.com> Acked-by: José Bollo <jobol@nonadev.net> Cc: Paul Moore <paul@paul-moore.com> Cc: Stephen Smalley <sds@tycho.nsa.gov> Cc: Eric Paris <eparis@parisplace.org> Cc: Kees Cook <keescook@chromium.org> Cc: James Morris <james.l.morris@oracle.com> Cc: José Bollo <jobol@nonadev.net> Signed-off-by: James Morris <james.l.morris@oracle.com>
-rw-r--r--include/linux/init_task.h7
-rw-r--r--include/linux/lsm_hooks.h9
-rw-r--r--include/linux/sched.h4
-rw-r--r--include/linux/security.h7
-rw-r--r--kernel/fork.c7
-rw-r--r--security/security.c5
6 files changed, 37 insertions, 2 deletions
diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index 91d9049f0039..926f2f553cc5 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -210,6 +210,12 @@ extern struct cred init_cred;
210# define INIT_TASK_TI(tsk) 210# define INIT_TASK_TI(tsk)
211#endif 211#endif
212 212
213#ifdef CONFIG_SECURITY
214#define INIT_TASK_SECURITY .security = NULL,
215#else
216#define INIT_TASK_SECURITY
217#endif
218
213/* 219/*
214 * INIT_TASK is used to set up the first task table, touch at 220 * INIT_TASK is used to set up the first task table, touch at
215 * your own risk!. Base=0, limit=0x1fffff (=2MB) 221 * your own risk!. Base=0, limit=0x1fffff (=2MB)
@@ -288,6 +294,7 @@ extern struct cred init_cred;
288 INIT_VTIME(tsk) \ 294 INIT_VTIME(tsk) \
289 INIT_NUMA_BALANCING(tsk) \ 295 INIT_NUMA_BALANCING(tsk) \
290 INIT_KASAN(tsk) \ 296 INIT_KASAN(tsk) \
297 INIT_TASK_SECURITY \
291} 298}
292 299
293 300
diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 1aa63335de9e..080f34e66017 100644
--- a/include/linux/lsm_hooks.h
+++ b/include/linux/lsm_hooks.h
@@ -533,8 +533,13 @@
533 * manual page for definitions of the @clone_flags. 533 * manual page for definitions of the @clone_flags.
534 * @clone_flags contains the flags indicating what should be shared. 534 * @clone_flags contains the flags indicating what should be shared.
535 * Return 0 if permission is granted. 535 * Return 0 if permission is granted.
536 * @task_alloc:
537 * @task task being allocated.
538 * @clone_flags contains the flags indicating what should be shared.
539 * Handle allocation of task-related resources.
540 * Returns a zero on success, negative values on failure.
536 * @task_free: 541 * @task_free:
537 * @task task being freed 542 * @task task about to be freed.
538 * Handle release of task-related resources. (Note that this can be called 543 * Handle release of task-related resources. (Note that this can be called
539 * from interrupt context.) 544 * from interrupt context.)
540 * @cred_alloc_blank: 545 * @cred_alloc_blank:
@@ -1482,6 +1487,7 @@ union security_list_options {
1482 int (*file_open)(struct file *file, const struct cred *cred); 1487 int (*file_open)(struct file *file, const struct cred *cred);
1483 1488
1484 int (*task_create)(unsigned long clone_flags); 1489 int (*task_create)(unsigned long clone_flags);
1490 int (*task_alloc)(struct task_struct *task, unsigned long clone_flags);
1485 void (*task_free)(struct task_struct *task); 1491 void (*task_free)(struct task_struct *task);
1486 int (*cred_alloc_blank)(struct cred *cred, gfp_t gfp); 1492 int (*cred_alloc_blank)(struct cred *cred, gfp_t gfp);
1487 void (*cred_free)(struct cred *cred); 1493 void (*cred_free)(struct cred *cred);
@@ -1748,6 +1754,7 @@ struct security_hook_heads {
1748 struct list_head file_receive; 1754 struct list_head file_receive;
1749 struct list_head file_open; 1755 struct list_head file_open;
1750 struct list_head task_create; 1756 struct list_head task_create;
1757 struct list_head task_alloc;
1751 struct list_head task_free; 1758 struct list_head task_free;
1752 struct list_head cred_alloc_blank; 1759 struct list_head cred_alloc_blank;
1753 struct list_head cred_free; 1760 struct list_head cred_free;
diff --git a/include/linux/sched.h b/include/linux/sched.h
index d67eee84fd43..71b8df306bb0 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1038,6 +1038,10 @@ struct task_struct {
1038 /* A live task holds one reference: */ 1038 /* A live task holds one reference: */
1039 atomic_t stack_refcount; 1039 atomic_t stack_refcount;
1040#endif 1040#endif
1041#ifdef CONFIG_SECURITY
1042 /* Used by LSM modules for access restriction: */
1043 void *security;
1044#endif
1041 /* CPU-specific state of this task: */ 1045 /* CPU-specific state of this task: */
1042 struct thread_struct thread; 1046 struct thread_struct thread;
1043 1047
diff --git a/include/linux/security.h b/include/linux/security.h
index 97df7bac5b48..af675b576645 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -308,6 +308,7 @@ int security_file_send_sigiotask(struct task_struct *tsk,
308int security_file_receive(struct file *file); 308int security_file_receive(struct file *file);
309int security_file_open(struct file *file, const struct cred *cred); 309int security_file_open(struct file *file, const struct cred *cred);
310int security_task_create(unsigned long clone_flags); 310int security_task_create(unsigned long clone_flags);
311int security_task_alloc(struct task_struct *task, unsigned long clone_flags);
311void security_task_free(struct task_struct *task); 312void security_task_free(struct task_struct *task);
312int security_cred_alloc_blank(struct cred *cred, gfp_t gfp); 313int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);
313void security_cred_free(struct cred *cred); 314void security_cred_free(struct cred *cred);
@@ -861,6 +862,12 @@ static inline int security_task_create(unsigned long clone_flags)
861 return 0; 862 return 0;
862} 863}
863 864
865static inline int security_task_alloc(struct task_struct *task,
866 unsigned long clone_flags)
867{
868 return 0;
869}
870
864static inline void security_task_free(struct task_struct *task) 871static inline void security_task_free(struct task_struct *task)
865{ } 872{ }
866 873
diff --git a/kernel/fork.c b/kernel/fork.c
index 6c463c80e93d..3d32513d6c73 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1679,9 +1679,12 @@ static __latent_entropy struct task_struct *copy_process(
1679 goto bad_fork_cleanup_perf; 1679 goto bad_fork_cleanup_perf;
1680 /* copy all the process information */ 1680 /* copy all the process information */
1681 shm_init_task(p); 1681 shm_init_task(p);
1682 retval = copy_semundo(clone_flags, p); 1682 retval = security_task_alloc(p, clone_flags);
1683 if (retval) 1683 if (retval)
1684 goto bad_fork_cleanup_audit; 1684 goto bad_fork_cleanup_audit;
1685 retval = copy_semundo(clone_flags, p);
1686 if (retval)
1687 goto bad_fork_cleanup_security;
1685 retval = copy_files(clone_flags, p); 1688 retval = copy_files(clone_flags, p);
1686 if (retval) 1689 if (retval)
1687 goto bad_fork_cleanup_semundo; 1690 goto bad_fork_cleanup_semundo;
@@ -1903,6 +1906,8 @@ bad_fork_cleanup_files:
1903 exit_files(p); /* blocking */ 1906 exit_files(p); /* blocking */
1904bad_fork_cleanup_semundo: 1907bad_fork_cleanup_semundo:
1905 exit_sem(p); 1908 exit_sem(p);
1909bad_fork_cleanup_security:
1910 security_task_free(p);
1906bad_fork_cleanup_audit: 1911bad_fork_cleanup_audit:
1907 audit_free(p); 1912 audit_free(p);
1908bad_fork_cleanup_perf: 1913bad_fork_cleanup_perf:
diff --git a/security/security.c b/security/security.c
index 2f15488dc6bc..549bddcc2116 100644
--- a/security/security.c
+++ b/security/security.c
@@ -937,6 +937,11 @@ int security_task_create(unsigned long clone_flags)
937 return call_int_hook(task_create, 0, clone_flags); 937 return call_int_hook(task_create, 0, clone_flags);
938} 938}
939 939
940int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
941{
942 return call_int_hook(task_alloc, 0, task, clone_flags);
943}
944
940void security_task_free(struct task_struct *task) 945void security_task_free(struct task_struct *task)
941{ 946{
942 call_void_hook(task_free, task); 947 call_void_hook(task_free, task);