Skip to content

Commit

Permalink
Merge tag 'fixes-v5.11' of git://git.kernel.org/pub/scm/linux/kernel/…
Browse files Browse the repository at this point in the history
…git/brauner/linux

Pull misc fixes from Christian Brauner:
 "This contains several fixes which felt worth being combined into a
  single branch:

   - Use put_nsproxy() instead of open-coding it switch_task_namespaces()

   - Kirill's work to unify lifecycle management for all namespaces. The
     lifetime counters are used identically for all namespaces types.
     Namespaces may of course have additional unrelated counters and
     these are not altered. This work allows us to unify the type of the
     counters and reduces maintenance cost by moving the counter in one
     place and indicating that basic lifetime management is identical
     for all namespaces.

   - Peilin's fix adding three byte padding to Dmitry's
     PTRACE_GET_SYSCALL_INFO uapi struct to prevent an info leak.

   - Two smal patches to convert from the /* fall through */ comment
     annotation to the fallthrough keyword annotation which I had taken
     into my branch and into -next before df561f6 ("treewide: Use
     fallthrough pseudo-keyword") made it upstream which fixed this
     tree-wide.

     Since I didn't want to invalidate all testing for other commits I
     didn't rebase and kept them"

* tag 'fixes-v5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux:
  nsproxy: use put_nsproxy() in switch_task_namespaces()
  sys: Convert to the new fallthrough notation
  signal: Convert to the new fallthrough notation
  time: Use generic ns_common::count
  cgroup: Use generic ns_common::count
  mnt: Use generic ns_common::count
  user: Use generic ns_common::count
  pid: Use generic ns_common::count
  ipc: Use generic ns_common::count
  uts: Use generic ns_common::count
  net: Use generic ns_common::count
  ns: Add a common refcount into ns_common
  ptrace: Prevent kernel-infoleak in ptrace_get_syscall_info()
  • Loading branch information
torvalds committed Dec 15, 2020
2 parents 6d93a19 + aabe19b commit f9b4240
Show file tree
Hide file tree
Showing 27 changed files with 56 additions and 76 deletions.
3 changes: 1 addition & 2 deletions fs/mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <linux/fs_pin.h>

struct mnt_namespace {
atomic_t count;
struct ns_common ns;
struct mount * root;
/*
Expand Down Expand Up @@ -120,7 +119,7 @@ static inline void detach_mounts(struct dentry *dentry)

static inline void get_mnt_ns(struct mnt_namespace *ns)
{
atomic_inc(&ns->count);
refcount_inc(&ns->ns.count);
}

extern seqlock_t mount_lock;
Expand Down
4 changes: 2 additions & 2 deletions fs/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -3274,7 +3274,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
new_ns->ns.ops = &mntns_operations;
if (!anon)
new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
atomic_set(&new_ns->count, 1);
refcount_set(&new_ns->ns.count, 1);
INIT_LIST_HEAD(&new_ns->list);
init_waitqueue_head(&new_ns->poll);
spin_lock_init(&new_ns->ns_lock);
Expand Down Expand Up @@ -3848,7 +3848,7 @@ void __init mnt_init(void)

void put_mnt_ns(struct mnt_namespace *ns)
{
if (!atomic_dec_and_test(&ns->count))
if (!refcount_dec_and_test(&ns->ns.count))
return;
drop_collected_mounts(&ns->root->mnt);
free_mnt_ns(ns);
Expand Down
5 changes: 2 additions & 3 deletions include/linux/cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,6 @@ static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
#endif /* CONFIG_CGROUP_DATA */

struct cgroup_namespace {
refcount_t count;
struct ns_common ns;
struct user_namespace *user_ns;
struct ucounts *ucounts;
Expand Down Expand Up @@ -889,12 +888,12 @@ copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
static inline void get_cgroup_ns(struct cgroup_namespace *ns)
{
if (ns)
refcount_inc(&ns->count);
refcount_inc(&ns->ns.count);
}

static inline void put_cgroup_ns(struct cgroup_namespace *ns)
{
if (ns && refcount_dec_and_test(&ns->count))
if (ns && refcount_dec_and_test(&ns->ns.count))
free_cgroup_ns(ns);
}

Expand Down
3 changes: 1 addition & 2 deletions include/linux/ipc_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct ipc_ids {
};

struct ipc_namespace {
refcount_t count;
struct ipc_ids ids[3];

int sem_ctls[4];
Expand Down Expand Up @@ -128,7 +127,7 @@ extern struct ipc_namespace *copy_ipcs(unsigned long flags,
static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
{
if (ns)
refcount_inc(&ns->count);
refcount_inc(&ns->ns.count);
return ns;
}

Expand Down
3 changes: 3 additions & 0 deletions include/linux/ns_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
#ifndef _LINUX_NS_COMMON_H
#define _LINUX_NS_COMMON_H

#include <linux/refcount.h>

struct proc_ns_operations;

struct ns_common {
atomic_long_t stashed;
const struct proc_ns_operations *ops;
unsigned int inum;
refcount_t count;
};

#endif
4 changes: 1 addition & 3 deletions include/linux/pid_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <linux/workqueue.h>
#include <linux/threads.h>
#include <linux/nsproxy.h>
#include <linux/kref.h>
#include <linux/ns_common.h>
#include <linux/idr.h>

Expand All @@ -18,7 +17,6 @@
struct fs_pin;

struct pid_namespace {
struct kref kref;
struct idr idr;
struct rcu_head rcu;
unsigned int pid_allocated;
Expand All @@ -43,7 +41,7 @@ extern struct pid_namespace init_pid_ns;
static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
{
if (ns != &init_pid_ns)
kref_get(&ns->kref);
refcount_inc(&ns->ns.count);
return ns;
}

Expand Down
9 changes: 4 additions & 5 deletions include/linux/time_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


#include <linux/sched.h>
#include <linux/kref.h>
#include <linux/nsproxy.h>
#include <linux/ns_common.h>
#include <linux/err.h>
Expand All @@ -18,7 +17,6 @@ struct timens_offsets {
};

struct time_namespace {
struct kref kref;
struct user_namespace *user_ns;
struct ucounts *ucounts;
struct ns_common ns;
Expand All @@ -37,20 +35,21 @@ extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);

static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
{
kref_get(&ns->kref);
refcount_inc(&ns->ns.count);
return ns;
}

struct time_namespace *copy_time_ns(unsigned long flags,
struct user_namespace *user_ns,
struct time_namespace *old_ns);
void free_time_ns(struct kref *kref);
void free_time_ns(struct time_namespace *ns);
void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
struct vdso_data *arch_get_vdso_data(void *vvar_page);

static inline void put_time_ns(struct time_namespace *ns)
{
kref_put(&ns->kref, free_time_ns);
if (refcount_dec_and_test(&ns->ns.count))
free_time_ns(ns);
}

void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
Expand Down
5 changes: 2 additions & 3 deletions include/linux/user_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ struct user_namespace {
struct uid_gid_map uid_map;
struct uid_gid_map gid_map;
struct uid_gid_map projid_map;
atomic_t count;
struct user_namespace *parent;
int level;
kuid_t owner;
Expand Down Expand Up @@ -109,7 +108,7 @@ void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
{
if (ns)
atomic_inc(&ns->count);
refcount_inc(&ns->ns.count);
return ns;
}

Expand All @@ -119,7 +118,7 @@ extern void __put_user_ns(struct user_namespace *ns);

static inline void put_user_ns(struct user_namespace *ns)
{
if (ns && atomic_dec_and_test(&ns->count))
if (ns && refcount_dec_and_test(&ns->ns.count))
__put_user_ns(ns);
}

Expand Down
9 changes: 4 additions & 5 deletions include/linux/utsname.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


#include <linux/sched.h>
#include <linux/kref.h>
#include <linux/nsproxy.h>
#include <linux/ns_common.h>
#include <linux/err.h>
Expand All @@ -22,7 +21,6 @@ struct user_namespace;
extern struct user_namespace init_user_ns;

struct uts_namespace {
struct kref kref;
struct new_utsname name;
struct user_namespace *user_ns;
struct ucounts *ucounts;
Expand All @@ -33,16 +31,17 @@ extern struct uts_namespace init_uts_ns;
#ifdef CONFIG_UTS_NS
static inline void get_uts_ns(struct uts_namespace *ns)
{
kref_get(&ns->kref);
refcount_inc(&ns->ns.count);
}

extern struct uts_namespace *copy_utsname(unsigned long flags,
struct user_namespace *user_ns, struct uts_namespace *old_ns);
extern void free_uts_ns(struct kref *kref);
extern void free_uts_ns(struct uts_namespace *ns);

static inline void put_uts_ns(struct uts_namespace *ns)
{
kref_put(&ns->kref, free_uts_ns);
if (refcount_dec_and_test(&ns->ns.count))
free_uts_ns(ns);
}

void uts_ns_init(void);
Expand Down
11 changes: 4 additions & 7 deletions include/net/net_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ struct net {
refcount_t passive; /* To decide when the network
* namespace should be freed.
*/
refcount_t count; /* To decided when the network
* namespace should be shut down.
*/
spinlock_t rules_mod_lock;

unsigned int dev_unreg_count;
Expand Down Expand Up @@ -245,7 +242,7 @@ void __put_net(struct net *net);

static inline struct net *get_net(struct net *net)
{
refcount_inc(&net->count);
refcount_inc(&net->ns.count);
return net;
}

Expand All @@ -256,14 +253,14 @@ static inline struct net *maybe_get_net(struct net *net)
* exists. If the reference count is zero this
* function fails and returns NULL.
*/
if (!refcount_inc_not_zero(&net->count))
if (!refcount_inc_not_zero(&net->ns.count))
net = NULL;
return net;
}

static inline void put_net(struct net *net)
{
if (refcount_dec_and_test(&net->count))
if (refcount_dec_and_test(&net->ns.count))
__put_net(net);
}

Expand All @@ -275,7 +272,7 @@ int net_eq(const struct net *net1, const struct net *net2)

static inline int check_net(const struct net *net)
{
return refcount_read(&net->count) != 0;
return refcount_read(&net->ns.count) != 0;
}

void net_drop_ns(void *);
Expand Down
3 changes: 2 additions & 1 deletion include/uapi/linux/ptrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ struct seccomp_metadata {

struct ptrace_syscall_info {
__u8 op; /* PTRACE_SYSCALL_INFO_* */
__u32 arch __attribute__((__aligned__(sizeof(__u32))));
__u8 pad[3];
__u32 arch;
__u64 instruction_pointer;
__u64 stack_pointer;
union {
Expand Down
2 changes: 1 addition & 1 deletion init/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int version_string(LINUX_VERSION_CODE);
#endif

struct uts_namespace init_uts_ns = {
.kref = KREF_INIT(2),
.ns.count = REFCOUNT_INIT(2),
.name = {
.sysname = UTS_SYSNAME,
.nodename = UTS_NODENAME,
Expand Down
2 changes: 1 addition & 1 deletion ipc/msgutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ DEFINE_SPINLOCK(mq_lock);
* and not CONFIG_IPC_NS.
*/
struct ipc_namespace init_ipc_ns = {
.count = REFCOUNT_INIT(1),
.ns.count = REFCOUNT_INIT(1),
.user_ns = &init_user_ns,
.ns.inum = PROC_IPC_INIT_INO,
#ifdef CONFIG_IPC_NS
Expand Down
4 changes: 2 additions & 2 deletions ipc/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
goto fail_free;
ns->ns.ops = &ipcns_operations;

refcount_set(&ns->count, 1);
refcount_set(&ns->ns.count, 1);
ns->user_ns = get_user_ns(user_ns);
ns->ucounts = ucounts;

Expand Down Expand Up @@ -164,7 +164,7 @@ static DECLARE_WORK(free_ipc_work, free_ipc);
*/
void put_ipc_ns(struct ipc_namespace *ns)
{
if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) {
mq_clear_sbinfo(ns);
spin_unlock(&mq_lock);

Expand Down
2 changes: 1 addition & 1 deletion kernel/cgroup/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static u16 have_canfork_callback __read_mostly;

/* cgroup namespace for init task */
struct cgroup_namespace init_cgroup_ns = {
.count = REFCOUNT_INIT(2),
.ns.count = REFCOUNT_INIT(2),
.user_ns = &init_user_ns,
.ns.ops = &cgroupns_operations,
.ns.inum = PROC_CGROUP_INIT_INO,
Expand Down
2 changes: 1 addition & 1 deletion kernel/cgroup/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
kfree(new_ns);
return ERR_PTR(ret);
}
refcount_set(&new_ns->count, 1);
refcount_set(&new_ns->ns.count, 1);
new_ns->ns.ops = &cgroupns_operations;
return new_ns;
}
Expand Down
6 changes: 3 additions & 3 deletions kernel/nsproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
* it along with CLONE_NEWIPC.
*/
if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
(CLONE_NEWIPC | CLONE_SYSVSEM))
(CLONE_NEWIPC | CLONE_SYSVSEM))
return -EINVAL;

new_ns = create_new_namespaces(flags, tsk, user_ns, tsk->fs);
Expand Down Expand Up @@ -245,8 +245,8 @@ void switch_task_namespaces(struct task_struct *p, struct nsproxy *new)
p->nsproxy = new;
task_unlock(p);

if (ns && atomic_dec_and_test(&ns->count))
free_nsproxy(ns);
if (ns)
put_nsproxy(ns);
}

void exit_task_namespaces(struct task_struct *p)
Expand Down
2 changes: 1 addition & 1 deletion kernel/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int pid_max_max = PID_MAX_LIMIT;
* the scheme scales to up to 4 million PIDs, runtime.
*/
struct pid_namespace init_pid_ns = {
.kref = KREF_INIT(2),
.ns.count = REFCOUNT_INIT(2),
.idr = IDR_INIT(init_pid_ns.idr),
.pid_allocated = PIDNS_ADDING,
.level = 0,
Expand Down
13 changes: 3 additions & 10 deletions kernel/pid_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
goto out_free_idr;
ns->ns.ops = &pidns_operations;

kref_init(&ns->kref);
refcount_set(&ns->ns.count, 1);
ns->level = level;
ns->parent = get_pid_ns(parent_pid_ns);
ns->user_ns = get_user_ns(user_ns);
Expand Down Expand Up @@ -148,22 +148,15 @@ struct pid_namespace *copy_pid_ns(unsigned long flags,
return create_pid_namespace(user_ns, old_ns);
}

static void free_pid_ns(struct kref *kref)
{
struct pid_namespace *ns;

ns = container_of(kref, struct pid_namespace, kref);
destroy_pid_namespace(ns);
}

void put_pid_ns(struct pid_namespace *ns)
{
struct pid_namespace *parent;

while (ns != &init_pid_ns) {
parent = ns->parent;
if (!kref_put(&ns->kref, free_pid_ns))
if (!refcount_dec_and_test(&ns->ns.count))
break;
destroy_pid_namespace(ns);
ns = parent;
}
}
Expand Down
Loading

0 comments on commit f9b4240

Please sign in to comment.