kernel: Only expose su when daemon is running

It has been claimed that the PG implementation of 'su' has security
vulnerabilities even when disabled.  Unfortunately, the people that
find these vulnerabilities often like to keep them private so they
can profit from exploits while leaving users exposed to malicious
hackers.

In order to reduce the attack surface for vulnerabilites, it is
therefore necessary to make 'su' completely inaccessible when it
is not in use (except by the root and system users).

Change-Id: I79716c72f74d0b7af34ec3a8054896c6559a181d
This commit is contained in:
Tom Marshall 2017-01-25 18:01:03 +01:00 committed by LuK1337
parent e0d4aab3d0
commit 169c186dc8
10 changed files with 85 additions and 0 deletions

View file

@ -1689,6 +1689,11 @@ static int do_execve_common(const char *filename,
if (retval < 0) if (retval < 0)
goto out; goto out;
if (capable(CAP_SYS_ADMIN) && d_is_su(file->f_dentry)) {
current->flags |= PF_SU;
su_exec();
}
/* execve succeeded */ /* execve succeeded */
current->fs->in_exec = 0; current->fs->in_exec = 0;
current->in_execve = 0; current->in_execve = 0;

View file

@ -2009,6 +2009,14 @@ static int path_lookupat(int dfd, const char *name,
} }
} }
if (!err) {
struct super_block *sb = nd->inode->i_sb;
if (sb->s_flags & MS_RDONLY) {
if (d_is_su(nd->path.dentry) && !su_visible())
err = -ENOENT;
}
}
if (base) if (base)
fput(base); fput(base);

View file

@ -39,6 +39,7 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
if (!IS_DEADDIR(inode)) { if (!IS_DEADDIR(inode)) {
if (file->f_op->iterate) { if (file->f_op->iterate) {
ctx->pos = file->f_pos; ctx->pos = file->f_pos;
ctx->romnt = (inode->i_sb->s_flags & MS_RDONLY);
res = file->f_op->iterate(file, ctx); res = file->f_op->iterate(file, ctx);
file->f_pos = ctx->pos; file->f_pos = ctx->pos;
} else { } else {
@ -53,6 +54,14 @@ out:
} }
EXPORT_SYMBOL(iterate_dir); EXPORT_SYMBOL(iterate_dir);
static bool hide_name(const char *name, int namlen)
{
if (namlen == 2 && !memcmp(name, "su", 2))
if (!su_visible())
return true;
return false;
}
/* /*
* Traditional linux readdir() handling.. * Traditional linux readdir() handling..
* *
@ -91,6 +100,8 @@ static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset
buf->result = -EOVERFLOW; buf->result = -EOVERFLOW;
return -EOVERFLOW; return -EOVERFLOW;
} }
if (hide_name(name, namlen) && buf->ctx.romnt)
return 0;
buf->result++; buf->result++;
dirent = buf->dirent; dirent = buf->dirent;
if (!access_ok(VERIFY_WRITE, dirent, if (!access_ok(VERIFY_WRITE, dirent,
@ -168,6 +179,8 @@ static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
buf->error = -EOVERFLOW; buf->error = -EOVERFLOW;
return -EOVERFLOW; return -EOVERFLOW;
} }
if (hide_name(name, namlen) && buf->ctx.romnt)
return 0;
dirent = buf->previous; dirent = buf->previous;
if (dirent) { if (dirent) {
if (__put_user(offset, &dirent->d_off)) if (__put_user(offset, &dirent->d_off))
@ -246,6 +259,8 @@ static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
buf->error = -EINVAL; /* only used if we fail.. */ buf->error = -EINVAL; /* only used if we fail.. */
if (reclen > buf->count) if (reclen > buf->count)
return -EINVAL; return -EINVAL;
if (hide_name(name, namlen) && buf->ctx.romnt)
return 0;
dirent = buf->previous; dirent = buf->previous;
if (dirent) { if (dirent) {
if (__put_user(offset, &dirent->d_off)) if (__put_user(offset, &dirent->d_off))

View file

@ -414,6 +414,13 @@ static inline bool d_mountpoint(struct dentry *dentry)
return dentry->d_flags & DCACHE_MOUNTED; return dentry->d_flags & DCACHE_MOUNTED;
} }
static inline bool d_is_su(const struct dentry *dentry)
{
return dentry &&
dentry->d_name.len == 2 &&
!memcmp(dentry->d_name.name, "su", 2);
}
extern int sysctl_vfs_cache_pressure; extern int sysctl_vfs_cache_pressure;
#endif /* __LINUX_DCACHE_H */ #endif /* __LINUX_DCACHE_H */

View file

@ -1534,6 +1534,7 @@ typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
struct dir_context { struct dir_context {
const filldir_t actor; const filldir_t actor;
loff_t pos; loff_t pos;
bool romnt;
}; };
static inline bool dir_emit(struct dir_context *ctx, static inline bool dir_emit(struct dir_context *ctx,

View file

@ -56,6 +56,12 @@ struct sched_param {
#include <asm/processor.h> #include <asm/processor.h>
int su_instances(void);
bool su_running(void);
bool su_visible(void);
void su_exec(void);
void su_exit(void);
#define SCHED_ATTR_SIZE_VER0 48 /* sizeof first published struct */ #define SCHED_ATTR_SIZE_VER0 48 /* sizeof first published struct */
/* /*
@ -1861,6 +1867,8 @@ static inline void sched_set_io_is_busy(int val) {};
#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */ #define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
#define PF_WAKE_UP_IDLE 0x80000000 /* try to wake up on an idle CPU */ #define PF_WAKE_UP_IDLE 0x80000000 /* try to wake up on an idle CPU */
#define PF_SU 0x00000002 /* task is su */
/* /*
* Only the _current_ task can read/write to tsk->flags, but other * Only the _current_ task can read/write to tsk->flags, but other
* tasks can access tsk->flags in readonly mode for example * tasks can access tsk->flags in readonly mode for example

View file

@ -64,6 +64,9 @@ static inline gid_t __kgid_val(kgid_t gid)
#define GLOBAL_ROOT_UID KUIDT_INIT(0) #define GLOBAL_ROOT_UID KUIDT_INIT(0)
#define GLOBAL_ROOT_GID KGIDT_INIT(0) #define GLOBAL_ROOT_GID KGIDT_INIT(0)
#define GLOBAL_SYSTEM_UID KUIDT_INIT(1000)
#define GLOBAL_SYSTEM_GID KGIDT_INIT(1000)
#define INVALID_UID KUIDT_INIT(-1) #define INVALID_UID KUIDT_INIT(-1)
#define INVALID_GID KGIDT_INIT(-1) #define INVALID_GID KGIDT_INIT(-1)

View file

@ -777,6 +777,10 @@ void do_exit(long code)
sched_exit(tsk); sched_exit(tsk);
if (tsk->flags & PF_SU) {
su_exit();
}
/* /*
* tsk->flags are checked in the futex code to protect against * tsk->flags are checked in the futex code to protect against
* an exiting task cleaning up the robust pi futexes. * an exiting task cleaning up the robust pi futexes.

View file

@ -326,6 +326,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
if (err) if (err)
goto free_ti; goto free_ti;
tsk->flags &= ~PF_SU;
tsk->stack = ti; tsk->stack = ti;
#ifdef CONFIG_SECCOMP #ifdef CONFIG_SECCOMP
/* /*

View file

@ -119,6 +119,38 @@ do { \
local_irq_restore(dflags); \ local_irq_restore(dflags); \
} while (0) } while (0)
static atomic_t __su_instances;
int su_instances(void)
{
return atomic_read(&__su_instances);
}
bool su_running(void)
{
return su_instances() > 0;
}
bool su_visible(void)
{
kuid_t uid = current_uid();
if (su_running())
return true;
if (uid_eq(uid, GLOBAL_ROOT_UID) || uid_eq(uid, GLOBAL_SYSTEM_UID))
return true;
return false;
}
void su_exec(void)
{
atomic_inc(&__su_instances);
}
void su_exit(void)
{
atomic_dec(&__su_instances);
}
const char *task_event_names[] = {"PUT_PREV_TASK", "PICK_NEXT_TASK", const char *task_event_names[] = {"PUT_PREV_TASK", "PICK_NEXT_TASK",
"TASK_WAKE", "TASK_MIGRATE", "TASK_UPDATE", "TASK_WAKE", "TASK_MIGRATE", "TASK_UPDATE",
"IRQ_UPDATE"}; "IRQ_UPDATE"};