From a6fcfcc15dacaeb4cc120df447a719da3b9e0c9d Mon Sep 17 00:00:00 2001 From: Jann Horn Date: Fri, 29 Nov 2019 23:10:33 +0100 Subject: [PATCH] exec: generate unique per-execution, per-process identifiers This adds a member privunit ("privilege unit") to task_struct. privunit is only shared by threads and changes on execve(). It can be used to check whether two tasks are temporally and spatially equal for privilege checking purposes. The implementation of locally unique IDs is in sched.h and exec.c for now because those are the only users so far - if anything else wants to use them in the future, they can be moved elsewhere. Signed-off-by: Jann Horn --- fs/exec.c | 17 +++++++++++++++++ include/linux/sched.h | 14 ++++++++++++++ kernel/fork.c | 1 + 3 files changed, 32 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index c27231234764..4cea8acb95e5 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1331,6 +1331,22 @@ void would_dump(struct linux_binprm *bprm, struct file *file) } EXPORT_SYMBOL(would_dump); +/* value 0 is reserved for init */ +static DEFINE_PER_CPU(u64, luid_counters) = 1; + +/* + * Allocates a new LUID and writes the allocated LUID to @out. + * This function must not be called from IRQ context. + */ +void alloc_luid(struct luid *out) +{ + preempt_disable(); + out->count = raw_cpu_read(luid_counters); + raw_cpu_add(luid_counters, 1); + out->cpu = smp_processor_id(); + preempt_enable(); +} + void setup_new_exec(struct linux_binprm * bprm) { /* @@ -1384,6 +1400,7 @@ void setup_new_exec(struct linux_binprm * bprm) /* An exec changes our domain. We are no longer part of the thread group */ current->self_exec_id++; + alloc_luid(¤t->privunit); flush_signal_handlers(current, 0); } EXPORT_SYMBOL(setup_new_exec); diff --git a/include/linux/sched.h b/include/linux/sched.h index 07e68d9f5dc4..6a3c16b3b43d 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -626,6 +626,19 @@ struct wake_q_node { struct wake_q_node *next; }; +/* locally unique ID */ +struct luid { + u64 count; + unsigned int cpu; +}; + +void alloc_luid(struct luid *out); + +static inline bool luid_eq(const struct luid *a, const struct luid *b) +{ + return a->count == b->count && a->cpu == b->cpu; +} + struct task_struct { #ifdef CONFIG_THREAD_INFO_IN_TASK /* @@ -941,6 +954,7 @@ struct task_struct { /* Thread group tracking: */ u32 parent_exec_id; u32 self_exec_id; + struct luid privunit; /* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */ spinlock_t alloc_lock; diff --git a/kernel/fork.c b/kernel/fork.c index 00b64f41c2b4..75784ff9c9f2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2152,6 +2152,7 @@ static __latent_entropy struct task_struct *copy_process( p->exit_signal = args->exit_signal; p->group_leader = p; p->tgid = p->pid; + alloc_luid(&p->privunit); } p->nr_dirtied = 0; -- 2.24.0.393.g34dc348eaf-goog