sysctl: handle table->maxlen robustly for proc_dobool
Subsystems (4)
PATCH v3 1/1 sysctl: handle table->maxlen robustly for proc_dobool
Setting ->proc_handler to proc_dobool at the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes in the future (When I first use proc_dobool() in my driver, I assign sizeof(variable) to table->maxlen. Then I found it was wrong, it should be sizeof(int) which was very counter-intuitive). For robustness, rework proc_dobool() robustly. So it is an improvement not a real bug fix.
---
v3:
- Update commit log.
v2:
- Reimplementing proc_dobool().
fs/lockd/svc.c | 2 +- kernel/sysctl.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-)
fs/lockd/svc.c +1 -1
@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, },
kernel/sysctl.c +19 -19
@@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) } } -static int do_proc_dobool_conv (bool *negp,unsigned long *lvalp, -int *valp, -int write,void *data) -{ -if (write) { - *(bool *)valp = *lvalp; - }else { -int val = *(bool *)valp; - - *lvalp = (unsigned long )val; - *negp = false; - } -return 0 ; -} -static int do_proc_dointvec_conv (bool *negp,unsigned long *lvalp,int *valp,int write,void *data)
@@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write,* @lenp: the size of the user buffer * @ppos: file position * -* Reads/writes up to table->maxlen/sizeof(unsigned int) integer -* values from/to the user buffer, treated as an ASCII string. +* Reads/writes up to table->maxlen/sizeof(bool) bool values from/to +* the user buffer, treated as an ASCII string. * * Returns 0 on success. */ int proc_dobool (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) { -return do_proc_dointvec (table, write, buffer, lenp, ppos, - do_proc_dobool_conv,NULL ); +struct ctl_table tmp = *table; +bool *data = table->data; +unsigned int val =READ_ONCE (*data); +int ret; + +/* Do not support arrays yet. */ +if (table->maxlen !=sizeof (bool )) +return -EINVAL ; + + tmp.maxlen =sizeof (val); + tmp.data = &val; + ret =do_proc_douintvec (&tmp, write, buffer, lenp, ppos,NULL ,NULL ); +if (ret) +return ret; +if (write) +WRITE_ONCE (*data, val ? true : false); +return 0 ; }/**
Signature · 1 line
2.11.0
Hi all,
Ping guys. Any comments or objections?
Quoted 102 lines · Muchun Song
Setting ->proc_handler to
proc_doboolat the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes in the future (When I first useproc_dobool()in my driver, I assign sizeof(variable) totable->maxlen. Then I found it was wrong, it should be sizeof(int) which was very counter-intuitive). For robustness, reworkproc_dobool()robustly. So it is an improvement not a real bug fix.Signed-off-by Muchun Song <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Kees Cook <[email protected]>Cc Iurii Zaikin <[email protected]>---
v3:
- Update commit log.v2:
- Reimplementingproc_dobool().fs/lockd/svc.c | 2 +- kernel/sysctl.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-)fs/lockd/svc.c +1
-1@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, },kernel/sysctl.c +19
-20@@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) } } -static int do_proc_dobool_conv (bool *negp,unsigned long *lvalp, -int *valp, -int write,void *data) -{ -if (write) { - *(bool *)valp = *lvalp; - }else { -int val = *(bool *)valp; - - *lvalp = (unsigned long )val; - *negp = false; - } -return 0 ; -} -static int do_proc_dointvec_conv (bool *negp,unsigned long *lvalp,int *valp,int write,void *data)@@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write,* @lenp: the size of the user buffer * @ppos: file position * -* Reads/writes up to table->maxlen/sizeof(unsigned int) integer -* values from/to the user buffer, treated as an ASCII string. +* Reads/writes up to table->maxlen/sizeof(bool) bool values from/to +* the user buffer, treated as an ASCII string. * * Returns 0 on success. */ int proc_dobool (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) { -return do_proc_dointvec (table, write, buffer, lenp, ppos, - do_proc_dobool_conv,NULL ); +struct ctl_table tmp = *table; +bool *data = table->data; +unsigned int val =READ_ONCE (*data); +int ret; + +/* Do not support arrays yet. */ +if (table->maxlen !=sizeof (bool )) +return -EINVAL ; + + tmp.maxlen =sizeof (val); + tmp.data = &val; + ret =do_proc_douintvec (&tmp, write, buffer, lenp, ppos,NULL ,NULL ); +if (ret) +return ret; +if (write) +WRITE_ONCE (*data, val ? true : false); +return 0 ; }/** -- 2.11.0
Please Cc the original authors of code if sending some follow up possible enhancements.
Quoted 5 lines · Muchun Song
Setting ->proc_handler to
proc_doboolat the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes in the future (When I first useproc_dobool()in my driver, I assign sizeof(variable) totable->maxlen. Then I found it was wrong, it should be sizeof(int) which was very counter-intuitive).
How did you find this out? If I change fs/lockd/svc.c's use I get compile warnings on at least x86_64.
Quoted 2 lines
For robustness,
reworkproc_dobool()robustly.
You mention robustness twice. Just say something like:
To help make things clear, make the logic used by proc_dobool() very clear with regards to its requirement with working with bools.
Quoted 23 lines
So it is an improvement not a real bug
fix.Signed-off-by Muchun Song <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Kees Cook <[email protected]>Cc Iurii Zaikin <[email protected]>---
v3:
- Update commit log.v2:
- Reimplementingproc_dobool().fs/lockd/svc.c | 2 +- kernel/sysctl.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-)fs/lockd/svc.c +0
-0
fs/lockd/svc.c static struct ctl_table nlm_sysctls[] = { · line 502
Quoted 8 lines
fs/lockd/svc.c +1
-1@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, },
Should this be a separate patch? What about the rest of the kernel? I see it is only used once so the one commit should mention that also.
Or did chaning this as you have it now alter the way the kernel treats this sysctl? All these things would be useful to clarify in the commit log.
Quoted 34 lines
kernel/sysctl.c +2
-17@@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) } } -static int do_proc_dobool_conv (bool *negp,unsigned long *lvalp, -int *valp, -int write,void *data) -{ -if (write) { - *(bool *)valp = *lvalp; - }else { -int val = *(bool *)valp; - - *lvalp = (unsigned long )val; - *negp = false; - } -return 0 ; -} -static int do_proc_dointvec_conv (bool *negp,unsigned long *lvalp,int *valp,int write,void *data)@@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write,* @lenp: the size of the user buffer * @ppos: file position * -* Reads/writes up to table->maxlen/sizeof(unsigned int) integer -* values from/to the user buffer, treated as an ASCII string. +* Reads/writes up to table->maxlen/sizeof(bool) bool values from/to +* the user buffer, treated as an ASCII string.
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 705
Quoted 10 lines
kernel/sysctl.c +2
-2* * Returns 0 on success. */ int proc_dobool (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) { -return do_proc_dointvec (table, write, buffer, lenp, ppos, - do_proc_dobool_conv,NULL ); +struct ctl_table tmp = *table; +bool *data = table->data;
Previously do_proc_douintvec() is called, and that checks if table->data is NULL previously before reading it and if so bails on __do_proc_dointvec() as follows:
if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) {
*lenp = 0;
return 0;
}Is it possible to have table->data be NULL? I think that's where the above check comes from.
And, so if it was false but not NULL, would it never do anything?
You can use lib/test_sysctl.c for this to proove / disprove correct functionality.
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 711
Quoted 6 lines
kernel/sysctl.c +6
-0+unsigned int val =READ_ONCE (*data); +int ret; + +/* Do not support arrays yet. */ +if (table->maxlen !=sizeof (bool )) +return -EINVAL ;
This is a separate change, and while I agree with it, as it simplifies our implementation and we don't want to add more array crap support, this should *alone* should be a separate commit.
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 713
Quoted 2 lines
kernel/sysctl.c +2
-0+ + tmp.maxlen =sizeof (val);
Why even set this as you do when we know it must be sizeof(bool)? Or would this break things given do_proc_douintvec() is used?
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 715
Quoted 2 lines
kernel/sysctl.c +2
-0+ tmp.data = &val; + ret =do_proc_douintvec (&tmp, write, buffer, lenp, ppos,NULL ,NULL );
Ugh, since we are avoiding arrays and we are only dealing with bools I'm inclined to just ask we simpify this a bool implementation which does something like do_proc_do_bool() but without array and is optimized just for bools.
The current hoops to read this code is simplly irritating
Luis
Quoted 11 lines
kernel/sysctl.c +5
-0+if (ret) +return ret; +if (write) +WRITE_ONCE (*data, val ? true : false); +return 0 ; }/** Signature · 1 line
2.11.0
Quoted 3 lines · Luis Chamberlain
Please Cc the original authors of code if sending some follow up possible enhancements.
Will do. +Jia He
Quoted 10 lines · Muchun Song
Quoted 5 lines
Setting ->proc_handler to
proc_doboolat the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes in the future (When I first useproc_dobool()in my driver, I assign sizeof(variable) totable->maxlen. Then I found it was wrong, it should be sizeof(int) which was very counter-intuitive).How did you find this out? If I change fs/lockd/svc.c's use I get compile warnings on at least
x86_64.
I am writing a code like:
static bool optimize_vmemmap_enabled;
static struct ctl_table hugetlb_vmemmap_sysctls[] = {
{
.procname = "hugetlb_optimize_vmemmap", .data = &optimize_vmemmap_enabled, .maxlen = sizeof(optimize_vmemmap_enabled), .mode = 0644,
.proc_handler = proc_dobool,
},
{ }
};
At least I don't see any warnings from compiler. And I found the assignment of ".data" should be "sizeof(int)", otherwise, it does not work properly. It is a little weird to me.
Quoted 8 lines
Quoted 3 lines
For robustness,
reworkproc_dobool()robustly.You mention robustness twice. Just say something like:
To help make things clear, make the logic used by
proc_dobool()very clear with regards to its requirement with working with bools.
Clearer! Thanks.
Quoted 19 lines
Quoted 19 lines
So it is an improvement not a real bug
fix.Signed-off-by Muchun Song <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Kees Cook <[email protected]>Cc Iurii Zaikin <[email protected]>---
v3:
- Update commit log.v2:
- Reimplementingproc_dobool().fs/lockd/svc.c | 2 +- kernel/sysctl.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-)
Quoted 15 lines
fs/lockd/svc.c static struct ctl_table nlm_sysctls[] = { · line 502
Quoted 13 lines
fs/lockd/svc.c +1
-1@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, },Should this be a separate patch? What about the rest of the kernel?
I afraid not. Since this change of proc_dobool will break the "nsm_use_hostnames". It should be changed to sizeof(nsm_use_hostnames) at the same time.
Quoted 1 line
I see it is only used once so the one commit should mention that also.
Well, will do.
Quoted 4 lines
Or did chaning this as you have it now alter the way the kernel treats this sysctl? All these things would be useful to clarify in the commit log.
Make sense. I'll mention those things into commit log.
Quoted 50 lines
Quoted 35 lines
kernel/sysctl.c +2
-17@@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) } } -static int do_proc_dobool_conv (bool *negp,unsigned long *lvalp, -int *valp, -int write,void *data) -{ -if (write) { - *(bool *)valp = *lvalp; - }else { -int val = *(bool *)valp; - - *lvalp = (unsigned long )val; - *negp = false; - } -return 0 ; -} -static int do_proc_dointvec_conv (bool *negp,unsigned long *lvalp,int *valp,int write,void *data)@@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write,* @lenp: the size of the user buffer * @ppos: file position * -* Reads/writes up to table->maxlen/sizeof(unsigned int) integer -* values from/to the user buffer, treated as an ASCII string. +* Reads/writes up to table->maxlen/sizeof(bool) bool values from/to +* the user buffer, treated as an ASCII string. kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 705
Quoted 10 lines
kernel/sysctl.c +2
-2* * Returns 0 on success. */ int proc_dobool (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) { -return do_proc_dointvec (table, write, buffer, lenp, ppos, - do_proc_dobool_conv,NULL ); +struct ctl_table tmp = *table; +bool *data = table->data;Previously
do_proc_douintvec()is called, and that checks iftable->datais NULL previously before reading it and if so bails on__do_proc_dointvec()as follows:
Quoted 7 lines
if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; }Is it possible to have
table->databe NULL? I think that's where the above check comes from.
At least now it cannot be NULL (no users do this now).
Quoted 2 lines
And, so if it was false but not NULL, would it never do anything?
I think we can add the check of NULL in the future if it could be happened, just like proc_dou8vec_minmax and proc_do_static_key do (they do not check ->data as well).
Quoted 3 lines
You can use
lib/test_sysctl.cfor this to proove / disprove correct functionality.
I didn't see the test for proc_bool in lib/test_sysctl.c. I think we can add a separate patch later to add a test for proc_bool.
Quoted 11 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 711
Quoted 7 lines
kernel/sysctl.c +6
-0+unsigned int val =READ_ONCE (*data); +int ret; + +/* Do not support arrays yet. */ +if (table->maxlen !=sizeof (bool )) +return -EINVAL ;This is a separate change, and while I agree with it, as it simplifies our implementation and we don't want to add more array crap support, this should *alone* should be a separate commit.
If you agree reusing do_proc_douintvec to implement proc_dobool(), I think a separate commit may be not suitable since do_proc_douintvec() only support non-array. Mentioning this in commit log makes sense to me.
Quoted 6 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 713
Quoted 3 lines
kernel/sysctl.c +2
-0+ + tmp.maxlen =sizeof (val);Why even set this as you do when we know it must be sizeof(bool)? Or would this break things given
do_proc_douintvec()is used?
Since we reuse do_proc_douintvec(), which requires a uint type, to get/set the value from/to the users. I think you can refer to the implementation of proc_dou8vec_minmax().
Quoted 8 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 715
Quoted 3 lines
kernel/sysctl.c +2
-0+ tmp.data = &val; + ret =do_proc_douintvec (&tmp, write, buffer, lenp, ppos,NULL ,NULL );Ugh, since we are avoiding arrays and we are only dealing with bools I'm inclined to just ask we simpify this a bool implementation which does something like
do_proc_do_bool()but without array and is optimized just for bools.
The current implementation of __do_proc_douintvec() is already only deal with non-array. Maybe it is better to reuse __do_proc_douintvec()? Otherwise, we need to implement a similar functionality (do_proc_do_bool) like it but just process bool type. I suspect the changes will be not small. I am wondering is it value to do this? If yes, should we also rework proc_dou8vec_minmax() as well?
Thanks.
Quoted 16 lines
The current hoops to read this code is simplly irritating
Luis
Quoted 11 lines
kernel/sysctl.c +5
-1+if (ret) +return ret; +if (write) +WRITE_ONCE (*data, val ? true : false); +return 0 ; }/** -- 2.11.0
Quoted 33 lines · Muchun Song
Quoted 3 lines · Luis Chamberlain
Please Cc the original authors of code if sending some follow up possible enhancements.
Will do. +Jia He
Quoted 10 lines · Muchun Song
Quoted 5 lines
Setting ->proc_handler to
proc_doboolat the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes in the future (When I first useproc_dobool()in my driver, I assign sizeof(variable) totable->maxlen. Then I found it was wrong, it should be sizeof(int) which was very counter-intuitive).How did you find this out? If I change fs/lockd/svc.c's use I get compile warnings on at least
x86_64.I am writing a code like:
static bool
optimize_vmemmap_enabled;static struct
ctl_tablehugetlb_vmemmap_sysctls[] = {
{
.procname = "hugetlb_optimize_vmemmap", .data =&optimize_vmemmap_enabled, .maxlen =sizeof(optimize_vmemmap_enabled), .mode = 0644,
.proc_handler =proc_dobool,
},
{ }
};
Quoted 3 lines
At least I don't see any warnings from compiler. And I found the assignment of ".data" should be "sizeof(int)", otherwise, it does not work properly. It is a little weird to me.
This is still odd to me but please clarify in your commit logs how you found an issue. You don't need to specify the exact code snippet, but just mentioning how you found it helps during patch review.
Quoted 45 lines
Quoted 7 lines
Quoted 2 lines
For robustness,
reworkproc_dobool()robustly.You mention robustness twice. Just say something like:
To help make things clear, make the logic used by
proc_dobool()very clear with regards to its requirement with working with bools.Clearer! Thanks.
Quoted 19 lines
Quoted 19 lines
So it is an improvement not a real bug
fix.Signed-off-by Muchun Song <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Kees Cook <[email protected]>Cc Iurii Zaikin <[email protected]>---
v3:
- Update commit log.v2:
- Reimplementingproc_dobool().fs/lockd/svc.c | 2 +- kernel/sysctl.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-)Quoted 15 lines
fs/lockd/svc.c static struct ctl_table nlm_sysctls[] = { · line 502
Quoted 13 lines
fs/lockd/svc.c +1
-1@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, },Should this be a separate patch? What about the rest of the kernel?
Quoted 3 lines
I afraid not. Since this change of
proc_doboolwill break the "nsm_use_hostnames". It should be changed tosizeof(nsm_use_hostnames)at the same time.
OK!
Quoted 3 lines
Quoted 1 line
I see it is only used once so the one commit should mention that also.
Well, will do.
OK
Quoted 61 lines
Quoted 3 lines
Or did chaning this as you have it now alter the way the kernel treats this sysctl? All these things would be useful to clarify in the commit log.
Make sense. I'll mention those things into commit log.
Quoted 55 lines
Quoted 35 lines
kernel/sysctl.c +2
-17@@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) } } -static int do_proc_dobool_conv (bool *negp,unsigned long *lvalp, -int *valp, -int write,void *data) -{ -if (write) { - *(bool *)valp = *lvalp; - }else { -int val = *(bool *)valp; - - *lvalp = (unsigned long )val; - *negp = false; - } -return 0 ; -} -static int do_proc_dointvec_conv (bool *negp,unsigned long *lvalp,int *valp,int write,void *data)@@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write,* @lenp: the size of the user buffer * @ppos: file position * -* Reads/writes up to table->maxlen/sizeof(unsigned int) integer -* values from/to the user buffer, treated as an ASCII string. +* Reads/writes up to table->maxlen/sizeof(bool) bool values from/to +* the user buffer, treated as an ASCII string. kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 705
Quoted 10 lines
kernel/sysctl.c +2
-2* * Returns 0 on success. */ int proc_dobool (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) { -return do_proc_dointvec (table, write, buffer, lenp, ppos, - do_proc_dobool_conv,NULL ); +struct ctl_table tmp = *table; +bool *data = table->data;Previously
do_proc_douintvec()is called, and that checks iftable->datais NULL previously before reading it and if so bails on__do_proc_dointvec()as follows:if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; }
Quoted 4 lines
Quoted 2 lines
Is it possible to have
table->databe NULL? I think that's where the above check comes from.At least now it cannot be NULL (no users do this now).
It does not mean new users where it is NULL can't be introduced.
Quoted 5 lines
Quoted 1 line
And, so if it was false but not NULL, would it never do anything?
I think we can add the check of NULL in the future if it could be happened, just like
proc_dou8vec_minmaxandproc_do_static_keydo (they do not check ->data as well).
Preventing bad uses ahead of time is definitely prefered.
Quoted 5 lines
Quoted 2 lines
You can use
lib/test_sysctl.cfor this to proove / disprove correct functionality.I didn't see the test for
proc_boolinlib/test_sysctl.c. I think we can add a separate patch later to add a test forproc_bool.
Yes please.
Quoted 5 lines
Quoted 5 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 709
Quoted 5 lines
kernel/sysctl.c +4
-0+unsigned int val =READ_ONCE (*data); +int ret; + +/* Do not support arrays yet. */
BTW I'd go furether. We don't want to add any more array support for anything new. So "we don't support arrays" is better.
Quoted 31 lines
Quoted 6 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 711
Quoted 2 lines
kernel/sysctl.c +2
-0+if (table->maxlen !=sizeof (bool )) +return -EINVAL ;This is a separate change, and while I agree with it, as it simplifies our implementation and we don't want to add more array crap support, this should *alone* should be a separate commit.
If you agree reusing
do_proc_douintvecto implementproc_dobool(), I think a separate commit may be not suitable sincedo_proc_douintvec()only support non-array. Mentioning this in commit log makes sense to me.Quoted 6 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 713
Quoted 3 lines
kernel/sysctl.c +2
-0+ + tmp.maxlen =sizeof (val);Why even set this as you do when we know it must be sizeof(bool)? Or would this break things given
do_proc_douintvec()is used?Since we reuse
do_proc_douintvec(), which requires a uint type, to get/set the value from/to the users. I think you can refer to the implementation ofproc_dou8vec_minmax().Quoted 8 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 715
Quoted 3 lines
kernel/sysctl.c +2
-0+ tmp.data = &val; + ret =do_proc_douintvec (&tmp, write, buffer, lenp, ppos,NULL ,NULL );Ugh, since we are avoiding arrays and we are only dealing with bools I'm inclined to just ask we simpify this a bool implementation which does something like
do_proc_do_bool()but without array and is optimized just for bools.
Quoted 5 lines
The current implementation of
__do_proc_douintvec()is already only deal with non-array. Maybe it is better to reuse__do_proc_douintvec()? Otherwise, we need to implement a similar functionality (do_proc_do_bool) like it but just process bool type. I suspect the changes will be not small. I am wondering is it value to do this? If yes, should we also reworkproc_dou8vec_minmax()as well?
I hate code which is obfuscates. Even if it s longer. My preference is to open code a few things here even if it is adding new code.
Luis
Quoted 35 lines · Luis Chamberlain
Quoted 33 lines · Muchun Song
Quoted 3 lines · Luis Chamberlain
Please Cc the original authors of code if sending some follow up possible enhancements.
Will do. +Jia He
Quoted 10 lines · Muchun Song
Quoted 5 lines
Setting ->proc_handler to
proc_doboolat the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes in the future (When I first useproc_dobool()in my driver, I assign sizeof(variable) totable->maxlen. Then I found it was wrong, it should be sizeof(int) which was very counter-intuitive).How did you find this out? If I change fs/lockd/svc.c's use I get compile warnings on at least
x86_64.I am writing a code like:
static bool
optimize_vmemmap_enabled;static struct
ctl_tablehugetlb_vmemmap_sysctls[] = {
{
.procname = "hugetlb_optimize_vmemmap", .data =&optimize_vmemmap_enabled, .maxlen =sizeof(optimize_vmemmap_enabled), .mode = 0644,
.proc_handler =proc_dobool,
},
{ }
};
Quoted 8 lines
Quoted 3 lines
At least I don't see any warnings from compiler. And I found the assignment of ".data" should be "sizeof(int)", otherwise, it does not work properly. It is a little weird to me.
This is still odd to me but please clarify in your commit logs how you found an issue. You don't need to specify the exact code snippet, but just mentioning how you found it helps during patch review.
Will do.
Quoted 119 lines
Quoted 46 lines
Quoted 8 lines
Quoted 3 lines
For robustness,
reworkproc_dobool()robustly.You mention robustness twice. Just say something like:
To help make things clear, make the logic used by
proc_dobool()very clear with regards to its requirement with working with bools.Clearer! Thanks.
Quoted 19 lines
Quoted 19 lines
So it is an improvement not a real bug
fix.Signed-off-by Muchun Song <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Kees Cook <[email protected]>Cc Iurii Zaikin <[email protected]>---
v3:
- Update commit log.v2:
- Reimplementingproc_dobool().fs/lockd/svc.c | 2 +- kernel/sysctl.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-)Quoted 15 lines
fs/lockd/svc.c static struct ctl_table nlm_sysctls[] = { · line 502
Quoted 13 lines
fs/lockd/svc.c +1
-1@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, },Should this be a separate patch? What about the rest of the kernel?
Quoted 3 lines
I afraid not. Since this change of
proc_doboolwill break the "nsm_use_hostnames". It should be changed tosizeof(nsm_use_hostnames)at the same time.OK!
Quoted 3 lines
Quoted 1 line
I see it is only used once so the one commit should mention that also.
Well, will do.
OK
Quoted 61 lines
Quoted 3 lines
Or did chaning this as you have it now alter the way the kernel treats this sysctl? All these things would be useful to clarify in the commit log.
Make sense. I'll mention those things into commit log.
Quoted 55 lines
Quoted 35 lines
kernel/sysctl.c +2
-17@@ -423,21 +423,6 @@ static void proc_put_char(void **buf, size_t *size, char c) } } -static int do_proc_dobool_conv (bool *negp,unsigned long *lvalp, -int *valp, -int write,void *data) -{ -if (write) { - *(bool *)valp = *lvalp; - }else { -int val = *(bool *)valp; - - *lvalp = (unsigned long )val; - *negp = false; - } -return 0 ; -} -static int do_proc_dointvec_conv (bool *negp,unsigned long *lvalp,int *valp,int write,void *data)@@ -708,16 +693,31 @@ int do_proc_douintvec(struct ctl_table *table, int write,* @lenp: the size of the user buffer * @ppos: file position * -* Reads/writes up to table->maxlen/sizeof(unsigned int) integer -* values from/to the user buffer, treated as an ASCII string. +* Reads/writes up to table->maxlen/sizeof(bool) bool values from/to +* the user buffer, treated as an ASCII string. kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 705
Quoted 10 lines
kernel/sysctl.c +2
-2* * Returns 0 on success. */ int proc_dobool (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) { -return do_proc_dointvec (table, write, buffer, lenp, ppos, - do_proc_dobool_conv,NULL ); +struct ctl_table tmp = *table; +bool *data = table->data;Previously
do_proc_douintvec()is called, and that checks iftable->datais NULL previously before reading it and if so bails on__do_proc_dointvec()as follows:if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; }
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Is it possible to have
table->databe NULL? I think that's where the above check comes from.At least now it cannot be NULL (no users do this now).
It does not mean new users where it is NULL can't be introduced.
Got it.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
And, so if it was false but not NULL, would it never do anything?
I think we can add the check of NULL in the future if it could be happened, just like
proc_dou8vec_minmaxandproc_do_static_keydo (they do not check ->data as well).Preventing bad uses ahead of time is definitely prefered.
Agree. Will do.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
You can use
lib/test_sysctl.cfor this to proove / disprove correct functionality.I didn't see the test for
proc_boolinlib/test_sysctl.c. I think we can add a separate patch later to add a test forproc_bool.Yes please.
OK.
Quoted 41 lines
Quoted 6 lines
Quoted 6 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 709
Quoted 6 lines
kernel/sysctl.c +4
-0+unsigned int val =READ_ONCE (*data); +int ret; + +/* Do not support arrays yet. */ BTW I'd go furether. We don't want to add any more array support for anything new. So "we don't support arrays" is better.
Quoted 31 lines
Quoted 6 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 711
Quoted 2 lines
kernel/sysctl.c +2
-0+if (table->maxlen !=sizeof (bool )) +return -EINVAL ;This is a separate change, and while I agree with it, as it simplifies our implementation and we don't want to add more array crap support, this should *alone* should be a separate commit.
If you agree reusing
do_proc_douintvecto implementproc_dobool(), I think a separate commit may be not suitable sincedo_proc_douintvec()only support non-array. Mentioning this in commit log makes sense to me.Quoted 6 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 713
Quoted 3 lines
kernel/sysctl.c +2
-0+ + tmp.maxlen =sizeof (val);Why even set this as you do when we know it must be sizeof(bool)? Or would this break things given
do_proc_douintvec()is used?Since we reuse
do_proc_douintvec(), which requires a uint type, to get/set the value from/to the users. I think you can refer to the implementation ofproc_dou8vec_minmax().Quoted 8 lines
kernel/sysctl.c int do_proc_douintvec(struct ctl_table *table, int write, · line 715
Quoted 3 lines
kernel/sysctl.c +2
-0+ tmp.data = &val; + ret =do_proc_douintvec (&tmp, write, buffer, lenp, ppos,NULL ,NULL );Ugh, since we are avoiding arrays and we are only dealing with bools I'm inclined to just ask we simpify this a bool implementation which does something like
do_proc_do_bool()but without array and is optimized just for bools.
Quoted 9 lines
Quoted 5 lines
The current implementation of
__do_proc_douintvec()is already only deal with non-array. Maybe it is better to reuse__do_proc_douintvec()? Otherwise, we need to implement a similar functionality (do_proc_do_bool) like it but just process bool type. I suspect the changes will be not small. I am wondering is it value to do this? If yes, should we also reworkproc_dou8vec_minmax()as well?I hate code which is obfuscates. Even if it s longer. My preference is to open code a few things here even if it is adding new code.
All right. Let's recreate a good start.
Thanks.
Quoted 1 line
Luis