2018-02-27 22:52:48

by Danilo Krummrich

[permalink] [raw]
Subject: [PATCH 1/2] fs/sysctl: fix potential page fault while unregistering sysctl table

proc_sys_link_fill_cache() does not take currently unregistering
sysctl tables into account, which might result into a page fault in
sysctl_follow_link() - add a check to fix it.

Signed-off-by: Danilo Krummrich <[email protected]>
---
fs/proc/proc_sysctl.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index c5cbbdff3c3d..a0b6c647835e 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -709,6 +709,9 @@ static bool proc_sys_link_fill_cache(struct file *file,
bool ret = true;
head = sysctl_head_grab(head);

+ if (IS_ERR(head))
+ return false;
+
if (S_ISLNK(table->mode)) {
/* It is not an error if we can not follow the link ignore it */
int err = sysctl_follow_link(&head, &table);
--
2.14.1



2018-02-27 22:51:12

by Danilo Krummrich

[permalink] [raw]
Subject: [PATCH 2/2] fs/sysctl: remove redundant link check in proc_sys_link_fill_cache()

proc_sys_link_fill_cache() does not need to check whether we're
called for a link - it's already done by scan().

Signed-off-by: Danilo Krummrich <[email protected]>
---
fs/proc/proc_sysctl.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index a0b6c647835e..7e7d9facb842 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -707,17 +707,16 @@ static bool proc_sys_link_fill_cache(struct file *file,
struct ctl_table *table)
{
bool ret = true;
+ int err = 0;
head = sysctl_head_grab(head);

if (IS_ERR(head))
return false;

- if (S_ISLNK(table->mode)) {
- /* It is not an error if we can not follow the link ignore it */
- int err = sysctl_follow_link(&head, &table);
- if (err)
- goto out;
- }
+ /* It is not an error if we can not follow the link ignore it */
+ sysctl_follow_link(&head, &table);
+ if (err)
+ goto out;

ret = proc_sys_fill_cache(file, ctx, head, table);
out:
--
2.14.1


2018-02-27 23:01:52

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 2/2] fs/sysctl: remove redundant link check in proc_sys_link_fill_cache()

On Tue, Feb 27, 2018 at 2:43 PM, Danilo Krummrich
<[email protected]> wrote:
> proc_sys_link_fill_cache() does not need to check whether we're
> called for a link - it's already done by scan().
>
> Signed-off-by: Danilo Krummrich <[email protected]>
> ---
> fs/proc/proc_sysctl.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> index a0b6c647835e..7e7d9facb842 100644
> --- a/fs/proc/proc_sysctl.c
> +++ b/fs/proc/proc_sysctl.c
> @@ -707,17 +707,16 @@ static bool proc_sys_link_fill_cache(struct file *file,
> struct ctl_table *table)
> {
> bool ret = true;
> + int err = 0;
> head = sysctl_head_grab(head);
>
> if (IS_ERR(head))
> return false;
>
> - if (S_ISLNK(table->mode)) {
> - /* It is not an error if we can not follow the link ignore it */
> - int err = sysctl_follow_link(&head, &table);
> - if (err)
> - goto out;
> - }
> + /* It is not an error if we can not follow the link ignore it */
> + sysctl_follow_link(&head, &table);

Shouldn't this be err = sysctl_follow_link... ? Otherwise I don't see
where err is used.

-Kees

> + if (err)
> + goto out;
>
> ret = proc_sys_fill_cache(file, ctx, head, table);
> out:
> --
> 2.14.1
>



--
Kees Cook
Pixel Security

2018-02-27 23:02:28

by Danilo Krummrich

[permalink] [raw]
Subject: Re: [PATCH 2/2] fs/sysctl: remove redundant link check in proc_sys_link_fill_cache()

On 2018-02-27 23:59, Kees Cook wrote:
> On Tue, Feb 27, 2018 at 2:43 PM, Danilo Krummrich
> <[email protected]> wrote:
>> proc_sys_link_fill_cache() does not need to check whether we're
>> called for a link - it's already done by scan().
>>
>> Signed-off-by: Danilo Krummrich <[email protected]>
>> ---
>> fs/proc/proc_sysctl.c | 11 +++++------
>> 1 file changed, 5 insertions(+), 6 deletions(-)
>>
>> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
>> index a0b6c647835e..7e7d9facb842 100644
>> --- a/fs/proc/proc_sysctl.c
>> +++ b/fs/proc/proc_sysctl.c
>> @@ -707,17 +707,16 @@ static bool proc_sys_link_fill_cache(struct file
>> *file,
>> struct ctl_table *table)
>> {
>> bool ret = true;
>> + int err = 0;
>> head = sysctl_head_grab(head);
>>
>> if (IS_ERR(head))
>> return false;
>>
>> - if (S_ISLNK(table->mode)) {
>> - /* It is not an error if we can not follow the link
>> ignore it */
>> - int err = sysctl_follow_link(&head, &table);
>> - if (err)
>> - goto out;
>> - }
>> + /* It is not an error if we can not follow the link ignore it
>> */
>> + sysctl_follow_link(&head, &table);
>
> Shouldn't this be err = sysctl_follow_link... ? Otherwise I don't see
> where err is used.
>
> -Kees
>
Of course, thanks.
>> + if (err)
>> + goto out;
>>
>> ret = proc_sys_fill_cache(file, ctx, head, table);
>> out:
>> --
>> 2.14.1
>>

2018-02-27 23:03:37

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 1/2] fs/sysctl: fix potential page fault while unregistering sysctl table

On Tue, Feb 27, 2018 at 2:43 PM, Danilo Krummrich
<[email protected]> wrote:
> proc_sys_link_fill_cache() does not take currently unregistering
> sysctl tables into account, which might result into a page fault in
> sysctl_follow_link() - add a check to fix it.
>
> Signed-off-by: Danilo Krummrich <[email protected]>
> ---
> fs/proc/proc_sysctl.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> index c5cbbdff3c3d..a0b6c647835e 100644
> --- a/fs/proc/proc_sysctl.c
> +++ b/fs/proc/proc_sysctl.c
> @@ -709,6 +709,9 @@ static bool proc_sys_link_fill_cache(struct file *file,
> bool ret = true;

Nothing appears to actually change "ret" in this function. It should
likely be dropped too.

> head = sysctl_head_grab(head);
>
> + if (IS_ERR(head))
> + return false;
> +

This looks sensible. I'd drop the blank line between sysctl_head_grab
and the IS_ERR, though.

How are you testing this change?

Thanks!

-Kees

> if (S_ISLNK(table->mode)) {
> /* It is not an error if we can not follow the link ignore it */
> int err = sysctl_follow_link(&head, &table);
> --
> 2.14.1
>



--
Kees Cook
Pixel Security

2018-02-27 23:21:51

by Danilo Krummrich

[permalink] [raw]
Subject: Re: [PATCH 1/2] fs/sysctl: fix potential page fault while unregistering sysctl table

On 2018-02-28 00:02, Kees Cook wrote:
> On Tue, Feb 27, 2018 at 2:43 PM, Danilo Krummrich
> <[email protected]> wrote:
>> proc_sys_link_fill_cache() does not take currently unregistering
>> sysctl tables into account, which might result into a page fault in
>> sysctl_follow_link() - add a check to fix it.
>>
>> Signed-off-by: Danilo Krummrich <[email protected]>
>> ---
>> fs/proc/proc_sysctl.c | 3 +++
>> 1 file changed, 3 insertions(+)
>>
>> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
>> index c5cbbdff3c3d..a0b6c647835e 100644
>> --- a/fs/proc/proc_sysctl.c
>> +++ b/fs/proc/proc_sysctl.c
>> @@ -709,6 +709,9 @@ static bool proc_sys_link_fill_cache(struct file
>> *file,
>> bool ret = true;
>
> Nothing appears to actually change "ret" in this function. It should
> likely be dropped too.
>
proc_sys_fill_cache() potentially changes "ret".
>> head = sysctl_head_grab(head);
>>
>> + if (IS_ERR(head))
>> + return false;
>> +
>
> This looks sensible. I'd drop the blank line between sysctl_head_grab
> and the IS_ERR, though.
>
I'll do that.
> How are you testing this change?
>
Honestly, not at all. Actually, I never run in such a page fault.
I spotted it by accident while reading the code.
> Thanks!
>
> -Kees
>
>> if (S_ISLNK(table->mode)) {
>> /* It is not an error if we can not follow the link
>> ignore it */
>> int err = sysctl_follow_link(&head, &table);
>> --
>> 2.14.1
>>