]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
userns: Convert taskstats to handle the user and pid namespaces.
authorEric W. Biederman <ebiederm@xmission.com>
Wed, 8 Feb 2012 01:56:49 +0000 (17:56 -0800)
committerEric W. Biederman <ebiederm@xmission.com>
Tue, 18 Sep 2012 08:01:32 +0000 (01:01 -0700)
- Explicitly limit exit task stat broadcast to the initial user and
  pid namespaces, as it is already limited to the initial network
  namespace.

- For broadcast task stats explicitly generate all of the idenitiers
  in terms of the initial user namespace and the initial pid
  namespace.

- For request stats report them in terms of the current user namespace
  and the current pid namespace.  Netlink messages are delivered
  syncrhonously to the kernel allowing us to get the user namespace
  and the pid namespace from the current task.

- Pass the namespaces for representing pids and uids and gids
  into bacct_add_task.

Cc: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
include/linux/tsacct_kern.h
init/Kconfig
kernel/taskstats.c
kernel/tsacct.c

index 7e50ac795b0b05b55b747fc96b07da8bcfcb6977..44893e5ec8f74c6d3bd358cb250a24c5aea25541 100644 (file)
 #include <linux/taskstats.h>
 
 #ifdef CONFIG_TASKSTATS
-extern void bacct_add_tsk(struct taskstats *stats, struct task_struct *tsk);
+extern void bacct_add_tsk(struct user_namespace *user_ns,
+                         struct pid_namespace *pid_ns,
+                         struct taskstats *stats, struct task_struct *tsk);
 #else
-static inline void bacct_add_tsk(struct taskstats *stats, struct task_struct *tsk)
+static inline void bacct_add_tsk(struct user_namespace *user_ns,
+                                struct pid_namespace *pid_ns,
+                                struct taskstats *stats, struct task_struct *tsk)
 {}
 #endif /* CONFIG_TASKSTATS */
 
index b5ecb4e7551862232ae2e90ddb16e437b2b99b4c..f0f636cf0ce7ff93b83c1b64ee6cb60c7ef9923f 100644 (file)
@@ -927,7 +927,6 @@ config UIDGID_CONVERTED
        # Features
        depends on IMA = n
        depends on EVM = n
-       depends on TASKSTATS = n
        depends on TRACING = n
        depends on FS_POSIX_ACL = n
        depends on QUOTA = n
index d0a32796550fcdf81e40d18cda31a3b353d332d5..3880df2acf053047efec942d6fefd2f8998a92c7 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/cgroup.h>
 #include <linux/fs.h>
 #include <linux/file.h>
+#include <linux/pid_namespace.h>
 #include <net/genetlink.h>
 #include <linux/atomic.h>
 
@@ -174,7 +175,9 @@ static void send_cpu_listeners(struct sk_buff *skb,
        up_write(&listeners->sem);
 }
 
-static void fill_stats(struct task_struct *tsk, struct taskstats *stats)
+static void fill_stats(struct user_namespace *user_ns,
+                      struct pid_namespace *pid_ns,
+                      struct task_struct *tsk, struct taskstats *stats)
 {
        memset(stats, 0, sizeof(*stats));
        /*
@@ -190,7 +193,7 @@ static void fill_stats(struct task_struct *tsk, struct taskstats *stats)
        stats->version = TASKSTATS_VERSION;
        stats->nvcsw = tsk->nvcsw;
        stats->nivcsw = tsk->nivcsw;
-       bacct_add_tsk(stats, tsk);
+       bacct_add_tsk(user_ns, pid_ns, stats, tsk);
 
        /* fill in extended acct fields */
        xacct_add_tsk(stats, tsk);
@@ -207,7 +210,7 @@ static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
        rcu_read_unlock();
        if (!tsk)
                return -ESRCH;
-       fill_stats(tsk, stats);
+       fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
        put_task_struct(tsk);
        return 0;
 }
@@ -291,6 +294,12 @@ static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
        if (!cpumask_subset(mask, cpu_possible_mask))
                return -EINVAL;
 
+       if (current_user_ns() != &init_user_ns)
+               return -EINVAL;
+
+       if (task_active_pid_ns(current) != &init_pid_ns)
+               return -EINVAL;
+
        if (isadd == REGISTER) {
                for_each_cpu(cpu, mask) {
                        s = kmalloc_node(sizeof(struct listener),
@@ -631,11 +640,12 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
        if (rc < 0)
                return;
 
-       stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID, tsk->pid);
+       stats = mk_reply(rep_skb, TASKSTATS_TYPE_PID,
+                        task_pid_nr_ns(tsk, &init_pid_ns));
        if (!stats)
                goto err;
 
-       fill_stats(tsk, stats);
+       fill_stats(&init_user_ns, &init_pid_ns, tsk, stats);
 
        /*
         * Doesn't matter if tsk is the leader or the last group member leaving
@@ -643,7 +653,8 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
        if (!is_thread_group || !group_dead)
                goto send;
 
-       stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID, tsk->tgid);
+       stats = mk_reply(rep_skb, TASKSTATS_TYPE_TGID,
+                        task_tgid_nr_ns(tsk, &init_pid_ns));
        if (!stats)
                goto err;
 
index 23b4d784ebdd3e448ade6cb425e6ab3ba4e865fb..625df0b44690a67a1ca6d5f6e7f264fc1ac28fb5 100644 (file)
@@ -26,7 +26,9 @@
 /*
  * fill in basic accounting fields
  */
-void bacct_add_tsk(struct taskstats *stats, struct task_struct *tsk)
+void bacct_add_tsk(struct user_namespace *user_ns,
+                  struct pid_namespace *pid_ns,
+                  struct taskstats *stats, struct task_struct *tsk)
 {
        const struct cred *tcred;
        struct timespec uptime, ts;
@@ -55,13 +57,13 @@ void bacct_add_tsk(struct taskstats *stats, struct task_struct *tsk)
                stats->ac_flag |= AXSIG;
        stats->ac_nice   = task_nice(tsk);
        stats->ac_sched  = tsk->policy;
-       stats->ac_pid    = tsk->pid;
+       stats->ac_pid    = task_pid_nr_ns(tsk, pid_ns);
        rcu_read_lock();
        tcred = __task_cred(tsk);
-       stats->ac_uid    = tcred->uid;
-       stats->ac_gid    = tcred->gid;
+       stats->ac_uid    = from_kuid_munged(user_ns, tcred->uid);
+       stats->ac_gid    = from_kgid_munged(user_ns, tcred->gid);
        stats->ac_ppid   = pid_alive(tsk) ?
-                               rcu_dereference(tsk->real_parent)->tgid : 0;
+               task_tgid_nr_ns(rcu_dereference(tsk->real_parent), pid_ns) : 0;
        rcu_read_unlock();
        stats->ac_utime = cputime_to_usecs(tsk->utime);
        stats->ac_stime = cputime_to_usecs(tsk->stime);