ipc: IPCMNI limit check for *mni & increase that limit
Subsystems (2)
v8 cover ipc: IPCMNI limit check for *mni & increase that limit
v7->v8:
- Remove the __read_mostly tag for ipc_mni and related variables as their accesses are not really in performance critical path.
- Add a new ipcmni_compat sysctl parameter that can be set to restore old range check behavior if desired.
v6->v7:
- Drop the range clamping code and just return error instead for now until there is user request for clamping support.
- Fix compilation error when CONFIG_SYSVIPC_SYSCTL isn't defined.
v5->v6:
- Consolidate the 3 ctl_table flags into 2.
- Make similar changes to proc_doulongvec_minmax() and its associates to complete the clamping change.
- Remove the sysctl registration failure test patch for now for later consideration.
- Add extra braces to patch 1 to reduce code diff in a later patch.
v4->v5:
- Revert the flags back to 16-bit so that there will be no change to the size of ctl_table.
- Enhance the sysctl_check_flags() as requested by Luis to perform more checks to spot incorrect ctl_table entries.
- Change the sysctl selftest to use dummy sysctls instead of production ones & enhance it to do more checks.
- Add one more sysctl selftest for registration failure.
- Add 2 ipc patches to add an extended mode to increase IPCMNI from 32k to 2M.
- Miscellaneous change to incorporate feedback comments from reviewers.
v3->v4:
- Remove v3 patches 1 & 2 as they have been merged into the mm tree.
- Change flags from uint16_t to unsigned int.
- Remove CTL_FLAGS_OOR_WARNED and use pr_warn_ratelimited() instead.
- Simplify the warning message code.
- Add a new patch to fail the ctl_table registration with invalid flag.
- Add a test case for range clamping in sysctl selftest.
v2->v3:
- Fix kdoc comment errors.
- Incorporate comments and suggestions from Luis R. Rodriguez.
- Add a patch to fix a typo error in fs/proc/proc_sysctl.c.
v1->v2:
- Add kdoc comments to the do_proc_do{u}intvec_minmax_conv_param structures.
- Add a new flags field to the ctl_table structure for specifying whether range clamping should be activated instead of adding new sysctl parameter handlers.
- Clamp the semmni value embedded in the multi-values sem parameter.
v5 patch: https://lkml.org/lkml/2018/3/16/1106
v6 patch: https://lkml.org/lkml/2018/4/27/1094
v7 patch: https://lkml.org/lkml/2018/5/7/666
The sysctl parameters msgmni, shmmni and semmni have an inherent limit of IPC_MNI (32k). However, users may not be aware of that because they can write a value much higher than that without getting any error or notification. Reading the parameters back will show the newly written values which are not real.
The real IPCMNI limit is now enforced to make sure that users won't put in an unrealistic value. The first 2 patches enforce the limits.
There are also users out there requesting increase in the IPCMNI value. The last 2 patches attempt to do that by using a boot kernel parameter "ipcmni_extend" to increase the IPCMNI limit from 32k to 2M if the users really want the extended value.
Enforcing the range limit check may cause some existing applications to break if they unwittingly set a value higher than 32k. To allow system administrators to work around this issue, a new ipcmni_compat sysctl parameter can now be set to restore the old behavior. This compatibility mode can only be set if the ipcmni_extend boot parameter is not specified. Patch 5 implements this new sysctl parameter.
Waiman Long (5):
ipc: IPCMNI limit check for msgmni and shmmni
ipc: IPCMNI limit check for semmni
ipc: Allow boot time extension of IPCMNI from 32k to 2M
ipc: Conserve sequence numbers in extended IPCMNI mode
ipc: Add a new ipcmni_compat sysctl to fall back to old behavior
Documentation/admin-guide/kernel-parameters.txt | 3 + Documentation/sysctl/kernel.txt | 15 +++++ include/linux/ipc_namespace.h | 1 + ipc/ipc_sysctl.c | 78 ++++++++++++++++++++++++- ipc/util.c | 41 ++++++++----- ipc/util.h | 50 +++++++++++++--- 6 files changed, 164 insertions(+), 24 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v8 1/5 ipc: IPCMNI limit check for msgmni and shmmni
A user can write arbitrary integer values to msgmni and shmmni sysctl parameters without getting error, but the actual limit is really IPCMNI (32k). This can mislead users as they think they can get a value that is not real.
The right limits are now set for msgmni and shmmni so that the users will become aware if they set a value outside of the acceptable range.
--- ipc/ipc_sysctl.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
ipc/ipc_sysctl.c +5 -2
@@ -99,6 +99,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,static int zero;static int one =1 ;static int int_max =INT_MAX ; +static int ipc_mni =IPCMNI ;static struct ctl_table ipc_kern_table[] = { {
@@ -120,7 +121,9 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .data = &init_ipc_ns.shm_ctlmni, .maxlen = sizeof(init_ipc_ns.shm_ctlmni), .mode = 0644, - .proc_handler = proc_ipc_dointvec, + .proc_handler = proc_ipc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &ipc_mni, }, { .procname = "shm_rmid_forced",
@@ -147,7 +150,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .mode = 0644, .proc_handler = proc_ipc_dointvec_minmax, .extra1 = &zero, - .extra2 = &int_max, + .extra2 = &ipc_mni, }, { .procname = "auto_msgmni",
Signature · 1 line
1.8.3.1
Re: [PATCH v8 1/5] ipc: IPCMNI limit check for msgmni and shmmni
Quoted 9 lines · Waiman Long
A user can write arbitrary integer values to msgmni and shmmni sysctl parameters without getting error, but the actual limit is really IPCMNI (32k). This can mislead users as they think they can get a value that is not real.
The right limits are now set for msgmni and shmmni so that the users will become aware if they set a value outside of the acceptable range.
Signed-off-by Waiman Long <[email protected]>
Luis
Re: [PATCH v8 1/5] ipc: IPCMNI limit check for msgmni and shmmni
In reply to Waiman LongQuoted 9 lines · Waiman Long
A user can write arbitrary integer values to msgmni and shmmni sysctl parameters without getting error, but the actual limit is really IPCMNI (32k). This can mislead users as they think they can get a value that is not real.
The right limits are now set for msgmni and shmmni so that the users will become aware if they set a value outside of the acceptable range.
Signed-off-by Waiman Long <[email protected]>
Quoted 39 lines
---
ipc/ipc_sysctl.c| 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)ipc/ipc_sysctl.c +5
-2@@ -99,6 +99,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,static int zero;static int one =1 ;static int int_max =INT_MAX ; +static int ipc_mni =IPCMNI ;static struct ctl_table ipc_kern_table[] = { {@@ -120,7 +121,9 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .data = &init_ipc_ns.shm_ctlmni, .maxlen = sizeof(init_ipc_ns.shm_ctlmni), .mode = 0644, - .proc_handler = proc_ipc_dointvec, + .proc_handler = proc_ipc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &ipc_mni, }, { .procname = "shm_rmid_forced",@@ -147,7 +150,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .mode = 0644, .proc_handler = proc_ipc_dointvec_minmax, .extra1 = &zero, - .extra2 = &int_max, + .extra2 = &ipc_mni, }, { .procname = "auto_msgmni",Signature · 1 line
1.8.3.1
PATCH v8 2/5 ipc: IPCMNI limit check for semmni
In reply to Waiman LongFor SysV semaphores, the semmni value is the last part of the 4-element sem number array. To make semmni behave in a similar way to msgmni and shmmni, we can't directly use the _minmax handler. Instead, a special sem specific handler is added to check the last argument to make sure that it is limited to the [0, IPCMNI] range. An error will be returned if this is not the case.
--- ipc/ipc_sysctl.c | 23 ++++++++++++++++++++++- ipc/util.h | 9 +++++++++ 2 files changed, 31 insertions(+), 1 deletion(-)
ipc/ipc_sysctl.c +22 -1
@@ -88,12 +88,33 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos); } +static int proc_ipc_sem_dointvec (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp,loff_t *ppos) +{ +int ret, semmni; +struct ipc_namespace *ns = current->nsproxy->ipc_ns; + + semmni = ns->sem_ctls[3 ]; + ret =proc_ipc_dointvec (table, write, buffer, lenp, ppos); + +if (!ret) + ret =sem_check_semmni (current->nsproxy->ipc_ns); + +/* +* Reset the semmni value if an error happens. +*/ +if (ret) + ns->sem_ctls[3 ] = semmni; +return ret; +} +#else #define proc_ipc_doulongvec_minmax NULL#define proc_ipc_dointvec NULL#define proc_ipc_dointvec_minmax NULL#define proc_ipc_dointvec_minmax_orphans NULL#define proc_ipc_auto_msgmni NULL +#define proc_ipc_sem_dointvec NULL#endif static int zero;
@@ -175,7 +196,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .data = &init_ipc_ns.sem_ctls, .maxlen = 4*sizeof(int), .mode = 0644, - .proc_handler = proc_ipc_dointvec, + .proc_handler = proc_ipc_sem_dointvec, },#ifdef CONFIG_CHECKPOINT_RESTORE {
ipc/util.h +9 -0
@@ -218,6 +218,15 @@ int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,void free_ipcs (struct ipc_namespace *ns,struct ipc_ids *ids,void (*free)(struct ipc_namespace *,struct kern_ipc_perm *)); +static inline int sem_check_semmni (struct ipc_namespace *ns) { +/* +* Check semmni range [0, IPCMNI] +* semmni is the last element of sem_ctls[4] array +*/ +return ((ns->sem_ctls[3 ] <0 ) || (ns->sem_ctls[3 ] >IPCMNI )) + ? -ERANGE :0 ; +} +#ifdef CONFIG_COMPAT #include <linux/compat.h> struct compat_ipc_perm {
Signature · 1 line
1.8.3.1
Re: [PATCH v8 2/5] ipc: IPCMNI limit check for semmni
Quoted 6 lines · Waiman Long
For SysV semaphores, the semmni value is the last part of the 4-element sem number array. To make semmni behave in a similar way to msgmni and shmmni, we can't directly use the
_minmaxhandler. Instead, a special sem specific handler is added to check the last argument to make sure that it is limited to the [0, IPCMNI] range. An error will be returned if this is not the case.
Sorry my time has run out, and will be on vacation until July 10th. If this can wait until then then great, otherwise hopefully Kees can take a look. It would be wonderful to get Eric's review on this as well.
Luis
Re: [PATCH v8 2/5] ipc: IPCMNI limit check for semmni
Quoted 7 lines · Luis R. Rodriguez
Quoted 6 lines · Waiman Long
For SysV semaphores, the semmni value is the last part of the 4-element sem number array. To make semmni behave in a similar way to msgmni and shmmni, we can't directly use the
_minmaxhandler. Instead, a special sem specific handler is added to check the last argument to make sure that it is limited to the [0, IPCMNI] range. An error will be returned if this is not the case.
Quoted 5 lines
Sorry my time has run out, and will be on vacation until July 10th. If this can wait until then then great, otherwise hopefully Kees can take a look. It would be wonderful to get Eric's review on this as well.
Luis
Thanks for the review. I will try to get other reviewer to chime in.
Kees and Eric, do you have comments on this patchset?
Cheers,
Longman
Re: [PATCH v8 2/5] ipc: IPCMNI limit check for semmni
In reply to Waiman LongQuoted 8 lines · Waiman Long
For SysV semaphores, the semmni value is the last part of the 4-element sem number array. To make semmni behave in a similar way to msgmni and shmmni, we can't directly use the
_minmaxhandler. Instead, a special sem specific handler is added to check the last argument to make sure that it is limited to the [0, IPCMNI] range. An error will be returned if this is not the case.Signed-off-by Waiman Long <[email protected]>
Quoted 75 lines
---
ipc/ipc_sysctl.c| 23 ++++++++++++++++++++++-ipc/util.h| 9 +++++++++2 files changed, 31 insertions(+), 1 deletion(-)ipc/ipc_sysctl.c +22
-1@@ -88,12 +88,33 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos); } +static int proc_ipc_sem_dointvec (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp,loff_t *ppos) +{ +int ret, semmni; +struct ipc_namespace *ns = current->nsproxy->ipc_ns; + + semmni = ns->sem_ctls[3 ]; + ret =proc_ipc_dointvec (table, write, buffer, lenp, ppos); + +if (!ret) + ret =sem_check_semmni (current->nsproxy->ipc_ns); + +/* +* Reset the semmni value if an error happens. +*/ +if (ret) + ns->sem_ctls[3 ] = semmni; +return ret; +} +#else #define proc_ipc_doulongvec_minmax NULL#define proc_ipc_dointvec NULL#define proc_ipc_dointvec_minmax NULL#define proc_ipc_dointvec_minmax_orphans NULL#define proc_ipc_auto_msgmni NULL +#define proc_ipc_sem_dointvec NULL#endif static int zero;@@ -175,7 +196,7 @@ static int proc_ipc_auto_msgmni(struct ctl_table *table, int write, .data = &init_ipc_ns.sem_ctls, .maxlen = 4*sizeof(int), .mode = 0644, - .proc_handler = proc_ipc_dointvec, + .proc_handler = proc_ipc_sem_dointvec, },#ifdef CONFIG_CHECKPOINT_RESTORE {ipc/util.h +9
-0@@ -218,6 +218,15 @@ int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,void free_ipcs (struct ipc_namespace *ns,struct ipc_ids *ids,void (*free)(struct ipc_namespace *,struct kern_ipc_perm *)); +static inline int sem_check_semmni (struct ipc_namespace *ns) { +/* +* Check semmni range [0, IPCMNI] +* semmni is the last element of sem_ctls[4] array +*/ +return ((ns->sem_ctls[3 ] <0 ) || (ns->sem_ctls[3 ] >IPCMNI )) + ? -ERANGE :0 ; +} +#ifdef CONFIG_COMPAT #include <linux/compat.h> struct compat_ipc_perm {Signature · 1 line
1.8.3.1
PATCH v8 3/5 ipc: Allow boot time extension of IPCMNI from 32k to 2M
In reply to Waiman LongThe maximum number of unique System V IPC identifiers was limited to 32k. That limit should be big enough for most use cases.
However, there are some users out there requesting for more. To satisfy the need of those users, a new boot time kernel option "ipcmni_extend" is added to extend the IPCMNI value to 2M. This is a 64X increase which hopefully is big enough for them.
This new option does have the side effect of reducing the maximum number of unique sequence numbers from 64k down to 1k. So it is a trade-off.
--- Documentation/admin-guide/kernel-parameters.txt | 3 ++ ipc/ipc_sysctl.c | 12 ++++++- ipc/util.c | 12 +++---- ipc/util.h | 42 +++++++++++++++++++------ 4 files changed, 52 insertions(+), 17 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
@@ -1744,6 +1744,9 @@ ip= [IP_PNP] See Documentation/filesystems/nfs/nfsroot.txt. + ipcmni_extend [KNL] Extend the maximum number of unique System V + IPC identifiers from 32768 to 2097152. + irqaffinity= [SMP] Set the default irq affinity mask The argument is a cpu list, as described above.
ipc/ipc_sysctl.c +11 -1
@@ -120,7 +120,8 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,static int zero;static int one =1 ;static int int_max =INT_MAX ; -static int ipc_mni =IPCMNI ; +int ipc_mni =IPCMNI ; +int ipc_mni_shift =IPCMNI_SHIFT ;static struct ctl_table ipc_kern_table[] = { {
@@ -246,3 +247,12 @@ static int __init ipc_sysctl_init(void) }device_initcall (ipc_sysctl_init); + +static int __init ipc_mni_extend (char *str) +{ + ipc_mni =IPCMNI_EXTEND ; + ipc_mni_shift =IPCMNI_EXTEND_SHIFT ; +pr_info ("IPCMNI extended to %d.\n" , ipc_mni); +return 0 ; +} +early_param ("ipcmni_extend" , ipc_mni_extend);
ipc/util.c +6 -6
@@ -113,7 +113,7 @@ static int __init ipc_init(void)* @ids: ipc identifier set * * Set up the sequence range to use for the ipc identifier range (limited -* below IPCMNI) then initialise the keys hashtable and ids idr. +* below ipc_mni) then initialise the keys hashtable and ids idr. */ int ipc_init_ids (struct ipc_ids *ids) {
@@ -214,7 +214,7 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids, ids->next_id = -1; } -return SEQ_MULTIPLIER * new->seq + id; +return (new->seq <<SEQ_SHIFT ) + id; }#else
@@ -228,7 +228,7 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids,if (ids->seq >IPCID_SEQ_MAX ) ids->seq =0 ; - return SEQ_MULTIPLIER * new->seq + id; + return (new->seq << SEQ_SHIFT) + id; }#endif /* CONFIG_CHECKPOINT_RESTORE */
@@ -252,8 +252,8 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)kgid_t egid;int id, err; -if (limit >IPCMNI ) - limit =IPCMNI ; +if (limit > ipc_mni) + limit = ipc_mni;if (!ids->tables_initialized || ids->in_use >= limit)return -ENOSPC ;
@@ -777,7 +777,7 @@ static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,if (total >= ids->in_use)return NULL ; -for (; pos <IPCMNI ; pos++) { +for (; pos < ipc_mni; pos++) { ipc =idr_find (&ids->ipcs_idr, pos);if (ipc !=NULL ) { *new_pos = pos +1 ;
ipc/util.h +32 -10
74 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH v8 3/5] ipc: Allow boot time extension of IPCMNI from 32k to 2M
Cc'ing Manfred.
Quoted 7 lines · Waiman Long
The maximum number of unique System V IPC identifiers was limited to 32k. That limit should be big enough for most use cases.
However, there are some users out there requesting for more. To satisfy the need of those users, a new boot time kernel option "
ipcmni_extend" is added to extend the IPCMNI value to 2M. This is a 64X increase which hopefully is big enough for them.
Could you please provide more info on the need of these users and how you came up with this new value (which just seems quite arbitrary)?
Thanks,
Davidlohr
Quoted 191 lines
This new option does have the side effect of reducing the maximum number of unique sequence numbers from 64k down to 1k. So it is a trade-off.
Signed-off-by Waiman Long <[email protected]>---
Documentation/admin-guide/kernel-parameters.txt | 3 ++ ipc/ipc_sysctl.c | 12 ++++++- ipc/util.c | 12 +++---- ipc/util.h | 42 +++++++++++++++++++------ 4 files changed, 52 insertions(+), 17 deletions(-)Documentation/admin-guide/kernel-parameters.txt +3
-0@@ -1744,6 +1744,9 @@ ip= [IP_PNP] See Documentation/filesystems/nfs/nfsroot.txt. + ipcmni_extend [KNL] Extend the maximum number of unique System V + IPC identifiers from 32768 to 2097152. + irqaffinity= [SMP] Set the default irq affinity mask The argument is a cpu list, as described above.ipc/ipc_sysctl.c +11
-1@@ -120,7 +120,8 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,static int zero;static int one =1 ;static int int_max =INT_MAX ; -static int ipc_mni =IPCMNI ; +int ipc_mni =IPCMNI ; +int ipc_mni_shift =IPCMNI_SHIFT ;static struct ctl_table ipc_kern_table[] = { {@@ -246,3 +247,12 @@ static int __init ipc_sysctl_init(void) }device_initcall (ipc_sysctl_init); + +static int __init ipc_mni_extend (char *str) +{ + ipc_mni =IPCMNI_EXTEND ; + ipc_mni_shift =IPCMNI_EXTEND_SHIFT ; +pr_info ("IPCMNI extended to %d.\n" , ipc_mni); +return 0 ; +} +early_param ("ipcmni_extend" , ipc_mni_extend);ipc/util.c +6
-6@@ -113,7 +113,7 @@ static int __init ipc_init(void)* @ids: ipc identifier set * * Set up the sequence range to use for the ipc identifier range (limited -* below IPCMNI) then initialise the keys hashtable and ids idr. +* below ipc_mni) then initialise the keys hashtable and ids idr. */ int ipc_init_ids (struct ipc_ids *ids) {@@ -214,7 +214,7 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids, ids->next_id = -1; } -return SEQ_MULTIPLIER * new->seq + id; +return (new->seq <<SEQ_SHIFT ) + id; }#else @@ -228,7 +228,7 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids,if (ids->seq >IPCID_SEQ_MAX ) ids->seq =0 ; - return SEQ_MULTIPLIER * new->seq + id; + return (new->seq << SEQ_SHIFT) + id; }#endif /* CONFIG_CHECKPOINT_RESTORE */ @@ -252,8 +252,8 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)kgid_t egid;int id, err; -if (limit >IPCMNI ) - limit =IPCMNI ; +if (limit > ipc_mni) + limit = ipc_mni;if (!ids->tables_initialized || ids->in_use >= limit)return -ENOSPC ;@@ -777,7 +777,7 @@ static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,if (total >= ids->in_use)return NULL ; -for (; pos <IPCMNI ; pos++) { +for (; pos < ipc_mni; pos++) { ipc =idr_find (&ids->ipcs_idr, pos);if (ipc !=NULL ) { *new_pos = pos +1 ;ipc/util.h +32
-10@@ -15,8 +15,30 @@#include <linux/err.h> #include <linux/ipc_namespace.h> -#define IPCMNI 32768/* <= MAX_INT limit for ipc arrays (including sysctl changes) */ -#define SEQ_MULTIPLIER (IPCMNI) +/* +* By default, the ipc arrays can have up to 32k (15 bits) entries. +* When IPCMNI extension mode is turned on, the ipc arrays can have up +* to 2M (21 bits) entries. However, the space for sequence number will +* be shrunk from 16 bits to 10 bits. +*/ +#define IPCMNI_SHIFT 15 +#define IPCMNI_EXTEND_SHIFT 21 +#define IPCMNI (1 << IPCMNI_SHIFT) +#define IPCMNI_EXTEND (1 << IPCMNI_EXTEND_SHIFT) + +#ifdef CONFIG_SYSVIPC_SYSCTL +extern int ipc_mni; +extern int ipc_mni_shift; + +#define SEQ_SHIFT ipc_mni_shift +#define SEQ_MASK ((1 << ipc_mni_shift) - 1) + +#else /* CONFIG_SYSVIPC_SYSCTL */ + +#define ipc_mni IPCMNI +#define SEQ_SHIFT IPCMNI_SHIFT +#define SEQ_MASK ((1 << IPCMNI_SHIFT) - 1) +#endif /* CONFIG_SYSVIPC_SYSCTL */ int sem_init (void );int msg_init (void );@@ -96,9 +118,9 @@ void __init ipc_init_proc_interface(const char *path, const char *header,#define IPC_MSG_IDS 1#define IPC_SHM_IDS 2 -#define ipcid_to_idx (id) ((id) % SEQ_MULTIPLIER) -#define ipcid_to_seqx (id) ((id) / SEQ_MULTIPLIER) -#define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX) +#define ipcid_to_idx (id) ((id) & SEQ_MASK) +#define ipcid_to_seqx (id) ((id) >> SEQ_SHIFT) +#define IPCID_SEQ_MAX (INT_MAX >> SEQ_SHIFT)/* must be called with ids->rwsem acquired for writing */ int ipc_addid (struct ipc_ids *,struct kern_ipc_perm *,int );@@ -123,8 +145,8 @@ static inline int ipc_get_maxid(struct ipc_ids *ids)if (ids->in_use ==0 )return -1 ; -if (ids->in_use ==IPCMNI ) -return IPCMNI -1 ; +if (ids->in_use == ipc_mni) +return ipc_mni -1 ;return ids->max_id; }@@ -175,7 +197,7 @@ static inline void ipc_update_pid(struct pid **pos, struct pid *pid)static inline int ipc_checkid (struct kern_ipc_perm *ipcp,int uid) { -return uid /SEQ_MULTIPLIER != ipcp->seq; +return (uid >>SEQ_SHIFT ) != ipcp->seq; }static inline void ipc_lock_object (struct kern_ipc_perm *perm)@@ -220,10 +242,10 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,static inline int sem_check_semmni (struct ipc_namespace *ns) {/* -* Check semmni range [0, IPCMNI] +* Check semmni range [0, ipc_mni] * semmni is the last element of sem_ctls[4] array */ -return ((ns->sem_ctls[3 ] <0 ) || (ns->sem_ctls[3 ] >IPCMNI )) +return ((ns->sem_ctls[3 ] <0 ) || (ns->sem_ctls[3 ] > ipc_mni)) ? -ERANGE :0 ; }Signature · 1 line
1.8.3.1
Re: [PATCH v8 3/5] ipc: Allow boot time extension of IPCMNI from 32k to 2M
Quoted 12 lines · Davidlohr Bueso
Cc'ing Manfred.
Quoted 7 lines · Waiman Long
The maximum number of unique System V IPC identifiers was limited to 32k. That limit should be big enough for most use cases.
However, there are some users out there requesting for more. To satisfy the need of those users, a new boot time kernel option "
ipcmni_extend" is added to extend the IPCMNI value to 2M. This is a 64X increase which hopefully is big enough for them.
Quoted 5 lines
Could you please provide more info on the need of these users and how you came up with this new value (which just seems quite arbitrary)?
Thanks,
Davidlohr
Red Hat has a customer that is migrating from Solaris to Linux. Some of their applications just happen to use more than 32k of shared memory segments. I think Solaris allows up to 16M unique ID.
Yes, the amount of increase is a bit arbitrary. I was trying to balance how many bits should be left for sequence number. Maybe I should just take 8 more bits for ID and leave 8 bits for sequence number to match Solaris.
-Longman
Re: [PATCH v8 3/5] ipc: Allow boot time extension of IPCMNI from 32k to 2M
Hello together,
Quoted 17 lines · Waiman Long
Quoted 11 lines · Davidlohr Bueso
Cc'ing Manfred.
Quoted 7 lines · Waiman Long
The maximum number of unique System V IPC identifiers was limited to 32k. That limit should be big enough for most use cases.
However, there are some users out there requesting for more. To satisfy the need of those users, a new boot time kernel option "
ipcmni_extend" is added to extend the IPCMNI value to 2M. This is a 64X increase which hopefully is big enough for them.Quoted 5 lines
Could you please provide more info on the need of these users and how you came up with this new value (which just seems quite arbitrary)?
Thanks,
Davidlohr
Quoted 8 lines
Red Hat has a customer that is migrating from Solaris to Linux. Some of their applications just happen to use more than 32k of shared memory segments. I think Solaris allows up to 16M unique ID.
Yes, the amount of increase is a bit arbitrary. I was trying to balance how many bits should be left for sequence number. Maybe I should just take 8 more bits for ID and leave 8 bits for sequence number to match Solaris.
- I think we should use the same numbers as Solaris. Otherwise we later have to touch it again.
- What is the performance when using shmget() with already 10M segments present?
- I like the new logic for updating the sequence counter.
Is there a reason why you only enable it for extended mode?
You create a rarely used codepath, and I don't understand what speaks against switching to the 'deleted' approach for all systems.
--
Manfred
Re: [PATCH v8 3/5] ipc: Allow boot time extension of IPCMNI from 32k to 2M
Quoted 26 lines · Manfred Spraul
Hello together,
Quoted 23 lines · Waiman Long
Quoted 13 lines · Davidlohr Bueso
Cc'ing Manfred.
Quoted 9 lines · Waiman Long
The maximum number of unique System V IPC identifiers was limited to 32k. That limit should be big enough for most use cases.
However, there are some users out there requesting for more. To satisfy
the need of those users, a new boot time kernel option "ipcmni_extend" is added to extend the IPCMNI value to 2M. This is a 64X increase which
hopefully is big enough for them.Quoted 5 lines
Could you please provide more info on the need of these users and how you came up with this new value (which just seems quite arbitrary)?
Thanks,
DavidlohrRed Hat has a customer that is migrating from Solaris to Linux. Some of their applications just happen to use more than 32k of shared memory segments. I think Solaris allows up to 16M unique ID.
Quoted 7 lines
Quoted 4 lines
Yes, the amount of increase is a bit arbitrary. I was trying to balance how many bits should be left for sequence number. Maybe I should just take 8 more bits for ID and leave 8 bits for sequence number to match Solaris.
- I think we should use the same numbers as Solaris. Otherwise we later have to touch it again.
As said in my patch, it is a trade-off between # of uniq identifiers versus the chance of id reuse. I don't have an objection to increase it further, but I don't see the customers to really need such a large value.
Quoted 3 lines
- What is the performance when using
shmget()with already 10M segments present?
I am not sure the performance impact as I had not measure it myself. The shmget() function is considered in slowpath. We are generally less concern about its performance than other code paths that are in a performance critical path.
Quoted 4 lines
- I like the new logic for updating the sequence counter.
Is there a reason why you only enable it for extended mode?
I tried not to disturb the existing logic for backward compatibility concern. I don't mind switching it all over to use the new "deleted" approach if other people have no objection.
Cheers,
Longman
Quoted 8 lines
You create a rarely used codepath, and I don't understand what speaks against switching to the 'deleted' approach for all systems.
Signature · 1 line
Manfred
PATCH v8 4/5 ipc: Conserve sequence numbers in extended IPCMNI mode
In reply to Waiman LongThe mixing in of a sequence number into the IPC IDs is probably to avoid ID reuse in userspace as much as possible. With extended IPCMNI 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 one or more IDs have been removed previously to make sure that those IDs will not be reused when a new one is built. This is being done in the extended IPCMNI mode,
--- include/linux/ipc_namespace.h | 1 + ipc/ipc_sysctl.c | 2 ++ ipc/util.c | 29 ++++++++++++++++++++++------- ipc/util.h | 2 ++ 4 files changed, 27 insertions(+), 7 deletions(-)
include/linux/ipc_namespace.h +1 -0
@@ -16,6 +16,7 @@struct ipc_ids {int in_use;unsigned short seq; +unsigned short deleted;bool tables_initialized;struct rw_semaphore rwsem;struct idr ipcs_idr;
ipc/ipc_sysctl.c +2 -0
@@ -122,6 +122,7 @@ static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,static int int_max =INT_MAX ;int ipc_mni =IPCMNI ;int ipc_mni_shift =IPCMNI_SHIFT ; +bool ipc_mni_extended;static struct ctl_table ipc_kern_table[] = { {
@@ -252,6 +253,7 @@ static int __init ipc_mni_extend(char *str) { ipc_mni =IPCMNI_EXTEND ; ipc_mni_shift =IPCMNI_EXTEND_SHIFT ; + ipc_mni_extended = true;pr_info ("IPCMNI extended to %d.\n" , ipc_mni);return 0 ; }
ipc/util.c +22 -7
@@ -119,7 +119,8 @@ int ipc_init_ids(struct ipc_ids *ids) {int err; ids->in_use =0 ; - ids->seq =0 ; + ids->deleted = false; + ids->seq = ipc_mni_extended ?0 :-1 ;/* seq # is pre-incremented */ init_rwsem (&ids->rwsem); err =rhashtable_init (&ids->key_ht, &ipc_kht_params);if (err)
@@ -193,6 +194,11 @@ static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)return NULL ; } +/* +* To conserve sequence number space with extended ipc_mni when new ID +* is built, the sequence number is incremented only when one or more +* IDs have been removed previously. +*/ #ifdef CONFIG_CHECKPOINT_RESTORE /* * Specify desired id for next allocated IPC object.
@@ -206,9 +212,13 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids, struct kern_ipc_perm *new) {if (ids->next_id <0 ) {/* default, behave as !CHECKPOINT_RESTORE */ - new->seq = ids->seq++; -if (ids->seq >IPCID_SEQ_MAX ) - ids->seq =0 ; +if (!ipc_mni_extended || ids->deleted) { + ids->seq++; +if (ids->seq >IPCID_SEQ_MAX ) + ids->seq =0 ; + ids->deleted = false; + } + new->seq = ids->seq; }else { new->seq =ipcid_to_seqx (ids->next_id); ids->next_id =-1 ;
@@ -224,9 +234,13 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids,static inline int ipc_buildid (int id,struct ipc_ids *ids,struct kern_ipc_perm *new) { - new->seq = ids->seq++; -if (ids->seq >IPCID_SEQ_MAX ) - ids->seq =0 ; +if (!ipc_mni_extended || ids->deleted) { + ids->seq++; +if (ids->seq >IPCID_SEQ_MAX ) + ids->seq =0 ; + ids->deleted = false; + } + new->seq = ids->seq;return (new->seq <<SEQ_SHIFT ) + id; }
@@ -436,6 +450,7 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)idr_remove (&ids->ipcs_idr, lid);ipc_kht_remove (ids, ipcp); ids->in_use--; + ids->deleted = true; ipcp->deleted = true;if (unlikely (lid == ids->max_id)) {
ipc/util.h +2 -0
@@ -29,6 +29,7 @@#ifdef CONFIG_SYSVIPC_SYSCTL extern int ipc_mni;extern int ipc_mni_shift; +extern bool ipc_mni_extended;#define SEQ_SHIFT ipc_mni_shift#define SEQ_MASK ((1 << ipc_mni_shift) - 1)
@@ -36,6 +37,7 @@#else /* CONFIG_SYSVIPC_SYSCTL */ #define ipc_mni IPCMNI +#define ipc_mni_extended false#define SEQ_SHIFT IPCMNI_SHIFT#define SEQ_MASK ((1 << IPCMNI_SHIFT) - 1)#endif /* CONFIG_SYSVIPC_SYSCTL */
Signature · 1 line
1.8.3.1
PATCH v8 5/5 ipc: Add a new ipcmni_compat sysctl to fall back to old behavior
In reply to Waiman LongWith strict range limit enforcement of msgmni, shmmni and sem, it is possible that some existing applications that set those values to above 32k may fail. To help users to work around this potential problem, a new boolean ipcmni_compat sysctl is added to provide the old beahavior for compatibility when it is set to 1. In other word, the limit will then be enforced internally but no error will be reported.
This compatibility mode can only be enabled if the ipcmni_extend kernel boot parameter is not specified.
The sysctl documentation is also updated accordingly.
--- Documentation/sysctl/kernel.txt | 15 +++++++++++++++ ipc/ipc_sysctl.c | 42 ++++++++++++++++++++++++++++++++++++++--- ipc/util.h | 5 +++-- 3 files changed, 57 insertions(+), 5 deletions(-)
Documentation/sysctl/kernel.txt +15 -0
@@ -39,6 +39,7 @@ show up in /proc/sys/kernel: - hung_task_check_count - hung_task_timeout_secs - hung_task_warnings +- ipcmni_compat - kexec_load_disabled - kptr_restrict - l2cr [ PPC only ]
@@ -374,6 +375,20 @@ This file shows up if CONFIG_DETECT_HUNG_TASK is enabled. ============================================================== +ipcmni_compat: + +A boolean flag to control range checking behavior of msgmni, shmmni +and the mni portion of sem. + +0: Range limits will be strictly enforced and error will be returned + if limits are exceeded. +1: Range limits will only be enforced internally and no error will be + returned if the upper limit is exceeded. This compatibility behavior + can only be selected if the ipcmni_extend kernel boot parameter is + not specified. + +============================================================== + kexec_load_disabled: A toggle indicating if the kexec_load syscall has been disabled. This
ipc/ipc_sysctl.c +39 -3
89 lines not on this page · raw message
ipc/util.h +3 -2
@@ -29,6 +29,7 @@#ifdef CONFIG_SYSVIPC_SYSCTL extern int ipc_mni;extern int ipc_mni_shift; +extern int ipcmni_max;extern bool ipc_mni_extended;#define SEQ_SHIFT ipc_mni_shift
@@ -244,10 +245,10 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,static inline int sem_check_semmni (struct ipc_namespace *ns) {/* -* Check semmni range [0, ipc_mni] +* Check semmni range [0, ipcmni_max] * semmni is the last element of sem_ctls[4] array */ -return ((ns->sem_ctls[3 ] <0 ) || (ns->sem_ctls[3 ] > ipc_mni)) +return ((ns->sem_ctls[3 ] <0 ) || (ns->sem_ctls[3 ] > ipcmni_max)) ? -ERANGE :0 ; }
Signature · 1 line
1.8.3.1
kbuild test robot
build botRe: [PATCH v8 5/5] ipc: Add a new ipcmni_compat sysctl to fall back to old behavior
Thank you for the patch! Yet something to improve:
Hi Waiman,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc1 next-20180618]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Waiman-Long/ipc-IPCMNI-limit-check-for-mni-increase-that-limit/20180618-183206
config: i386-randconfig-x013-201824 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 All errors (new ones prefixed by >>):
In file included from ipc/mqueue.c:43:0:
ipc/util.h: In function 'sem_check_semmni':
>> ipc/util.h:251:54: error: 'ipcmni_max' undeclared (first use in this function); did you mean 'icmp_mib'?
return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipcmni_max))
^~~~~~~~~~
icmp_mib ipc/util.h:251:54: note: each undeclared identifier is reported only once for each function it appears in
--
In file included from ipc/msgutil.c:22:0:
ipc/util.h: In function 'sem_check_semmni':
>> ipc/util.h:251:54: error: 'ipcmni_max' undeclared (first use in this function); did you mean 'pci_iomap'?
return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipcmni_max))
^~~~~~~~~~
pci_iomap ipc/util.h:251:54: note: each undeclared identifier is reported only once for each function it appears in
vim +251 ipc/util.h
239
240 struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
241 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
242 const struct ipc_ops *ops, struct ipc_params *params);
243 void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
244 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
245
246 static inline int sem_check_semmni(struct ipc_namespace *ns) {
247 /*
248 * Check semmni range [0, ipcmni_max]
249 * semmni is the last element of sem_ctls[4] array
250 */
> 251 return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipcmni_max))
252 ? -ERANGE : 0;
253 }
254
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH v8 5/5] ipc: Add a new ipcmni_compat sysctl to fall back to old behavior
In reply to Waiman LongThank you for the patch! Yet something to improve:
Hi Waiman,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc1 next-20180618]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Waiman-Long/ipc-IPCMNI-limit-check-for-mni-increase-that-limit/20180618-183206
config: i386-randconfig-a0-06181851 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 All errors (new ones prefixed by >>):
In file included from ipc/mqueue.c:43:0:
ipc/util.h: In function 'sem_check_semmni':
>> ipc/util.h:251:54: error: 'ipcmni_max' undeclared (first use in this function)
return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipcmni_max))
^ ipc/util.h:251:54: note: each undeclared identifier is reported only once for each function it appears in
vim +/ipcmni_max +251 ipc/util.h
239
240 struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
241 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
242 const struct ipc_ops *ops, struct ipc_params *params);
243 void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
244 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
245
246 static inline int sem_check_semmni(struct ipc_namespace *ns) {
247 /*
248 * Check semmni range [0, ipcmni_max]
249 * semmni is the last element of sem_ctls[4] array
250 */
> 251 return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipcmni_max))
252 ? -ERANGE : 0;
253 }
254
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 78 lines · Waiman Long
v7->v8:
- Remove the__read_mostlytag foripc_mniand related variables as their accesses are not really in performance critical path.
- Add a newipcmni_compatsysctl parameter that can be set to restore old range check behavior if desired.
v6->v7:
- Drop the range clamping code and just return error instead for now until there is user request for clamping support.
- Fix compilation error whenCONFIG_SYSVIPC_SYSCTLisn't defined.
v5->v6:
- Consolidate the 3ctl_tableflags into 2.
- Make similar changes toproc_doulongvec_minmax()and its associates to complete the clamping change.
- Remove the sysctl registration failure test patch for now for later consideration.
- Add extra braces to patch 1 to reduce code diff in a later patch.
v4->v5:
- Revert the flags back to 16-bit so that there will be no change to the size ofctl_table.
- Enhance thesysctl_check_flags()as requested by Luis to perform more checks to spot incorrectctl_tableentries.
- Change the sysctl selftest to use dummy sysctls instead of production ones & enhance it to do more checks.
- Add one more sysctl selftest for registration failure.
- Add 2 ipc patches to add an extended mode to increase IPCMNI from 32k to 2M.
- Miscellaneous change to incorporate feedback comments from reviewers.
v3->v4:
- Remove v3 patches 1 & 2 as they have been merged into the mm tree.
- Change flags fromuint16_tto unsigned int.
- RemoveCTL_FLAGS_OOR_WARNEDand usepr_warn_ratelimited()instead.
- Simplify the warning message code.
- Add a new patch to fail thectl_tableregistration with invalid flag.
- Add a test case for range clamping in sysctl selftest.
v2->v3:
- Fix kdoc comment errors.
- Incorporate comments and suggestions from Luis R. Rodriguez.
- Add a patch to fix a typo error infs/proc/proc_sysctl.c.
v1->v2:
- Add kdoc comments to the do_proc_do{u}intvec_minmax_conv_param structures.
- Add a new flags field to thectl_tablestructure for specifying whether range clamping should be activated instead of adding new sysctl parameter handlers.
- Clamp the semmni value embedded in the multi-values sem parameter.v5 patch: https://lkml.org/lkml/2018/3/16/1106
v6 patch: https://lkml.org/lkml/2018/4/27/1094
v7 patch: https://lkml.org/lkml/2018/5/7/666The sysctl parameters msgmni, shmmni and semmni have an inherent limit of
IPC_MNI(32k). However, users may not be aware of that because they can write a value much higher than that without getting any error or notification. Reading the parameters back will show the newly written values which are not real.The real IPCMNI limit is now enforced to make sure that users won't put in an unrealistic value. The first 2 patches enforce the limits.
There are also users out there requesting increase in the IPCMNI value. The last 2 patches attempt to do that by using a boot kernel parameter "
ipcmni_extend" to increase the IPCMNI limit from 32k to 2M if the users really want the extended value.Enforcing the range limit check may cause some existing applications to break if they unwittingly set a value higher than 32k. To allow system administrators to work around this issue, a new
ipcmni_compatsysctl parameter can now be set to restore the old behavior. This compatibility mode can only be set if theipcmni_extendboot parameter is not specified. Patch 5 implements this new sysctl parameter.
Quoted 3 lines
Waiman Long (5):
ipc: IPCMNI limit check for msgmni and shmmni
ipc: IPCMNI limit check for semmni
I've reviewed the first two which look good and are actual immediate fixes to the bogus user input. I haven't gotten around yet the rest of the patches but are at least a bit more controversial than the first two. As such, could patch 1 and 2 be picked up once the merge window closes, for v4.20? Although -stable might want in, this situation is quite historic, so it's not that urgent.
Thanks,
Davidlohr
Quoted 15 lines
ipc: Allow boot time extension of IPCMNI from 32k to 2M
ipc: Conserve sequence numbers in extended IPCMNI mode
ipc: Add a newipcmni_compatsysctl to fall back to old behaviorDocumentation/admin-guide/kernel-parameters.txt | 3 + Documentation/sysctl/kernel.txt | 15 +++++ include/linux/ipc_namespace.h | 1 + ipc/ipc_sysctl.c | 78 ++++++++++++++++++++++++- ipc/util.c | 41 ++++++++----- ipc/util.h | 50 +++++++++++++--- 6 files changed, 164 insertions(+), 24 deletions(-)Signature · 1 line
1.8.3.1
Quoted 11 lines · On Fri, 17 Aug 2018 09:50:08 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 10 lines
Enforcing the range limit check may cause some existing applications to break if they unwittingly set a value higher than 32k. To allow system administrators to work around this issue, a new
ipcmni_compatsysctl parameter can now be set to restore the old behavior. This compatibility mode can only be set if theipcmni_extendboot parameter is not specified. Patch 5 implements this new sysctl parameter.Waiman Long (5):
ipc: IPCMNI limit check for msgmni and shmmni
ipc: IPCMNI limit check for semmni
Quoted 6 lines
I've reviewed the first two which look good and are actual immediate fixes to the bogus user input. I haven't gotten around yet the rest of the patches but are at least a bit more controversial than the first two. As such, could patch 1 and 2 be picked up once the merge window closes, for v4.20? Although -stable might want in, this situation is quite historic, so it's not that urgent.
Thanks. Could we please have a refresh and resend? Hopefully Luis will have time for another pass.