Re: [PATCH] kernel: sysctl: use 'unsigned long' type for 'zero' variable

From: Andrew Morton
Date: Wed Dec 03 2014 - 18:25:30 EST


On Wed, 03 Dec 2014 15:41:21 +0300 Andrey Ryabinin <a.ryabinin@xxxxxxxxxxx> wrote:

>
> Use the 'unsigned long' type for 'zero' variable to fix this.
> Changing type to 'unsigned long' shouldn't affect any other users
> of this variable.
>
> Reported-by: Dmitry Vyukov <dvyukov@xxxxxxxxxx>
> Fixes: ed4d4902ebdd ("mm, hugetlb: remove hugetlb_zero and hugetlb_infinity")
> Signed-off-by: Andrey Ryabinin <a.ryabinin@xxxxxxxxxxx>
> ---
> kernel/sysctl.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/sysctl.c b/kernel/sysctl.c
> index 15f2511..45c45c9 100644
> --- a/kernel/sysctl.c
> +++ b/kernel/sysctl.c
> @@ -120,7 +120,7 @@ static int sixty = 60;
>
> static int __maybe_unused neg_one = -1;
>
> -static int zero;
> +static unsigned long zero;
> static int __maybe_unused one = 1;
> static int __maybe_unused two = 2;
> static int __maybe_unused four = 4;

Yeah, this is ghastly.

Look at

{
.procname = "numa_balancing",
.data = NULL, /* filled in by handler */
.maxlen = sizeof(unsigned int),
.mode = 0644,
.proc_handler = sysctl_numa_balancing,
.extra1 = &zero,
.extra2 = &one,
},

Now extra1 points at a long and extra2 points at an int.
sysctl_numa_balancing() calls proc_dointvec_minmax() and I think your
patch just broke big-endian 64-bit machines. "sched_autogroup_enabled"
breaks as well.

These sysctl tables drove a big truck straight through the C type
system and enabled all sorts of nasty breakage.

So how do we fix it? Maybe we could actually use the type system a bit
and do something like

union sysctl_payload {
int i;
unsigned long ul;
...
};

static union zero_int = {
.i = 0
};

static union zero_ulong = {
.ul = 0
};

and change proc_dointvec_minmax() and a million other functions to take
`union sysctl_payload *' arguments. But I haven't thought about it much.

Even for a minimal fix, someone should go through each and every
ctl_table and audit/fix the .extra1/.extra2 types/sizes. And until
that's done I'm not inclined to apply anything, because at least the
current code appears to work a bit.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/