Skip to content

Commit

Permalink
Merge pull request #7 from anupritaisno1/squid_nougat
Browse files Browse the repository at this point in the history
More upstream
  • Loading branch information
FacuM committed Nov 2, 2017
2 parents aab5b52 + d9ac457 commit b31a731
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fs/exec.c
Expand Up @@ -1504,6 +1504,7 @@ static int do_execve_common(const char *filename,
bool clear_in_exec;
int retval;
const struct cred *cred = current_cred();
bool is_su;

/*
* We move the actual failure in case of RLIMIT_NPROC excess from
Expand Down Expand Up @@ -1580,10 +1581,18 @@ static int do_execve_common(const char *filename,
if (retval < 0)
goto out;

/* search_binary_handler can release file and it may be freed */
is_su = d_is_su(file->f_dentry);

retval = search_binary_handler(bprm);
if (retval < 0)
goto out;

if (is_su && capable(CAP_SYS_ADMIN)) {
current->flags |= PF_SU;
su_exec();
}

/* execve succeeded */
current->fs->in_exec = 0;
current->in_execve = 0;
Expand Down
10 changes: 10 additions & 0 deletions fs/namei.c
Expand Up @@ -2026,6 +2026,16 @@ 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()) {
path_put(&nd->path);
err = -ENOENT;
}
}
}

if (base)
fput(base);

Expand Down
15 changes: 15 additions & 0 deletions fs/readdir.c
Expand Up @@ -37,6 +37,7 @@ int iterate_dir(struct file *file, struct dir_context *ctx)

res = -ENOENT;
if (!IS_DEADDIR(inode)) {
ctx->romnt = (inode->i_sb->s_flags & MS_RDONLY);
if (file->f_op->iterate) {
ctx->pos = file->f_pos;
res = file->f_op->iterate(file, ctx);
Expand All @@ -53,6 +54,14 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
}
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..
*
Expand Down Expand Up @@ -91,6 +100,8 @@ static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset
buf->result = -EOVERFLOW;
return -EOVERFLOW;
}
if (hide_name(name, namlen) && buf->ctx.romnt)
return 0;
buf->result++;
dirent = buf->dirent;
if (!access_ok(VERIFY_WRITE, dirent,
Expand Down Expand Up @@ -168,6 +179,8 @@ static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
buf->error = -EOVERFLOW;
return -EOVERFLOW;
}
if (hide_name(name, namlen) && buf->ctx.romnt)
return 0;
dirent = buf->previous;
if (dirent) {
if (__put_user(offset, &dirent->d_off))
Expand Down Expand Up @@ -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.. */
if (reclen > buf->count)
return -EINVAL;
if (hide_name(name, namlen) && buf->ctx.romnt)
return 0;
dirent = buf->previous;
if (dirent) {
if (__put_user(offset, &dirent->d_off))
Expand Down
7 changes: 7 additions & 0 deletions include/linux/dcache.h
Expand Up @@ -411,6 +411,13 @@ static inline bool d_mountpoint(struct dentry *dentry)
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;

#endif /* __LINUX_DCACHE_H */
1 change: 1 addition & 0 deletions include/linux/fs.h
Expand Up @@ -1524,6 +1524,7 @@ typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
struct dir_context {
const filldir_t actor;
loff_t pos;
bool romnt;
};

static inline bool dir_emit(struct dir_context *ctx,
Expand Down
8 changes: 8 additions & 0 deletions include/linux/sched.h
Expand Up @@ -55,6 +55,12 @@ struct sched_param {

#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 */

/*
Expand Down Expand Up @@ -1814,6 +1820,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_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
* tasks can access tsk->flags in readonly mode for example
Expand Down
3 changes: 3 additions & 0 deletions include/linux/uidgid.h
Expand Up @@ -64,6 +64,9 @@ static inline gid_t __kgid_val(kgid_t gid)
#define GLOBAL_ROOT_UID KUIDT_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_GID KGIDT_INIT(-1)

Expand Down
4 changes: 4 additions & 0 deletions kernel/exit.c
Expand Up @@ -774,6 +774,10 @@ void do_exit(long code)

sched_exit(tsk);

if (tsk->flags & PF_SU) {
su_exit();
}

/*
* tsk->flags are checked in the futex code to protect against
* an exiting task cleaning up the robust pi futexes.
Expand Down
2 changes: 2 additions & 0 deletions kernel/fork.c
Expand Up @@ -328,6 +328,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
if (err)
goto free_ti;

tsk->flags &= ~PF_SU;

tsk->stack = ti;
#ifdef CONFIG_SECCOMP
/*
Expand Down
32 changes: 32 additions & 0 deletions kernel/sched/core.c
Expand Up @@ -117,6 +117,38 @@ do { \
local_irq_restore(dflags); \
} 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",
"TASK_WAKE", "TASK_MIGRATE", "TASK_UPDATE",
"IRQ_UPDATE"};
Expand Down

0 comments on commit b31a731

Please sign in to comment.