Skip to content

Commit 03f1fc0

Browse files
committed
ipc/util: Helpers for making the sysvipc operations pid namespace aware
Capture the pid namespace when /proc/sysvipc/msg /proc/sysvipc/shm and /proc/sysvipc/sem are opened, and make it available through the new helper ipc_seq_pid_ns. This makes it possible to report the pids in these files in the pid namespace of the opener of the files. Implement ipc_update_pid. A simple impline helper that will only update a struct pid pointer if the new value does not equal the old value. This removes the need for wordy code sequences like: old = object->pid; object->pid = new; put_pid(old); and old = object->pid; if (old != new) { object->pid = new; put_pid(old); } Allowing the following to be written instead: ipc_update_pid(&object->pid, new); Which is easier to read and ensures that the pid reference count is not touched the old and the new values are the same. Not touching the reference count in this case is important to help avoid issues like af_unix experienced, where multiple threads of the same process managed to bounce the struct pid between cpu cache lines, but updating the pids reference count. Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent f83a396 commit 03f1fc0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

ipc/util.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,16 @@ int ipc_parse_version(int *cmd)
747747
#ifdef CONFIG_PROC_FS
748748
struct ipc_proc_iter {
749749
struct ipc_namespace *ns;
750+
struct pid_namespace *pid_ns;
750751
struct ipc_proc_iface *iface;
751752
};
752753

754+
struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s)
755+
{
756+
struct ipc_proc_iter *iter = s->private;
757+
return iter->pid_ns;
758+
}
759+
753760
/*
754761
* This routine locks the ipc structure found at least at position pos.
755762
*/
@@ -872,6 +879,7 @@ static int sysvipc_proc_open(struct inode *inode, struct file *file)
872879

873880
iter->iface = PDE_DATA(inode);
874881
iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
882+
iter->pid_ns = get_pid_ns(task_active_pid_ns(current));
875883

876884
return 0;
877885
}
@@ -881,6 +889,7 @@ static int sysvipc_proc_release(struct inode *inode, struct file *file)
881889
struct seq_file *seq = file->private_data;
882890
struct ipc_proc_iter *iter = seq->private;
883891
put_ipc_ns(iter->ns);
892+
put_pid_ns(iter->pid_ns);
884893
return seq_release_private(inode, file);
885894
}
886895

ipc/util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ int msg_init(void);
2323
void shm_init(void);
2424

2525
struct ipc_namespace;
26+
struct pid_namespace;
2627

2728
#ifdef CONFIG_POSIX_MQUEUE
2829
extern void mq_clear_sbinfo(struct ipc_namespace *ns);
@@ -86,6 +87,7 @@ int ipc_init_ids(struct ipc_ids *);
8687
#ifdef CONFIG_PROC_FS
8788
void __init ipc_init_proc_interface(const char *path, const char *header,
8889
int ids, int (*show)(struct seq_file *, void *));
90+
struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);
8991
#else
9092
#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
9193
#endif
@@ -150,6 +152,15 @@ struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
150152
struct ipc_ids *ids, int id, int cmd,
151153
struct ipc64_perm *perm, int extra_perm);
152154

155+
static inline void ipc_update_pid(struct pid **pos, struct pid *pid)
156+
{
157+
struct pid *old = *pos;
158+
if (old != pid) {
159+
*pos = get_pid(pid);
160+
put_pid(old);
161+
}
162+
}
163+
153164
#ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
154165
/* On IA-64, we always use the "64-bit version" of the IPC structures. */
155166
# define ipc_parse_version(cmd) IPC_64

0 commit comments

Comments
 (0)