path: root/ipc/util.h
diff options
authorManfred Spraul <>2019-05-14 15:46:33 -0700
committerLinus Torvalds <>2019-05-14 19:52:52 -0700
commit3278a2c20cb302d27e6f6ee45a3f57361176e426 (patch)
tree94ce43abadc5a660dda63662a28c7000e92154d4 /ipc/util.h
parent5ac893b8cb10fe2a47a77780d37f9bf5b142854b (diff)
ipc: conserve sequence numbers in ipcmni_extend mode
Rewrite, based on the patch from Waiman Long: The mixing in of a sequence number into the IPC IDs is probably to avoid ID reuse in userspace as much as possible. With ipcmni_extend mode, the number of usable sequence numbers is greatly reduced leading to higher chance of ID reuse. To address this issue, we need to conserve the sequence number space as much as possible. Right now, the sequence number is incremented for every new ID created. In reality, we only need to increment the sequence number when new allocated ID is not greater than the last one allocated. It is in such case that the new ID may collide with an existing one. This is being done irrespective of the ipcmni mode. In order to avoid any races, the index is first allocated and then the pointer is replaced. Changes compared to the initial patch: - Handle failures from idr_alloc(). - Avoid that concurrent operations can see the wrong sequence number. (This is achieved by using idr_replace()). - IPCMNI_SEQ_SHIFT is not a constant, thus renamed to ipcmni_seq_shift(). - IPCMNI_SEQ_MAX is not a constant, thus renamed to ipcmni_seq_max(). Link: Signed-off-by: Manfred Spraul <> Signed-off-by: Waiman Long <> Suggested-by: Matthew Wilcox <> Acked-by: Waiman Long <> Cc: Al Viro <> Cc: Davidlohr Bueso <> Cc: "Eric W . Biederman" <> Cc: Jonathan Corbet <> Cc: Kees Cook <> Cc: "Luis R. Rodriguez" <> Cc: Takashi Iwai <> Signed-off-by: Andrew Morton <> Signed-off-by: Linus Torvalds <>
Diffstat (limited to 'ipc/util.h')
1 files changed, 4 insertions, 4 deletions
diff --git a/ipc/util.h b/ipc/util.h
index 9746886757de..8c834ed39012 100644
--- a/ipc/util.h
+++ b/ipc/util.h
@@ -34,13 +34,13 @@
extern int ipc_mni;
extern int ipc_mni_shift;
-#define IPCMNI_SEQ_SHIFT ipc_mni_shift
+#define ipcmni_seq_shift() ipc_mni_shift
#define IPCMNI_IDX_MASK ((1 << ipc_mni_shift) - 1)
#define ipc_mni IPCMNI
+#define ipcmni_seq_shift() IPCMNI_SHIFT
#define IPCMNI_IDX_MASK ((1 << IPCMNI_SHIFT) - 1)
@@ -123,8 +123,8 @@ struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);
#define IPC_SHM_IDS 2
#define ipcid_to_idx(id) ((id) & IPCMNI_IDX_MASK)
-#define ipcid_to_seqx(id) ((id) >> IPCMNI_SEQ_SHIFT)
+#define ipcid_to_seqx(id) ((id) >> ipcmni_seq_shift())
+#define ipcid_seq_max() (INT_MAX >> ipcmni_seq_shift())
/* must be called with ids->rwsem acquired for writing */
int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);