Re: [PATCH RFC] init: fix -Wmissing-variable-declarations warning

From: Kees Cook
Date: Thu Aug 31 2023 - 17:19:35 EST


On Tue, Aug 29, 2023 at 11:38:31PM +0000, Justin Stitt wrote:
> Hi all,
>
> I was looking to get some help on solving this -Wmissing-variable-declarations
> warning as there is some hope to turn it on for W=1 soon [1].
>
> When building x86/defconfig with Clang-18 I encounter the following warning:
> | init/main.c:189:13: warning: no previous extern declaration for non-static variable 'envp_init' [-Wmissing-variable-declarations]
> | 189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
> | | ^
> | init/main.c:189:7: note: declare 'static' if the variable is not intended to be used outside of this translation unit
> | 189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
> | | ^
>
> It seems like the obvious solution is to just add the `static` keyword
> and be done with it. I suspect, however, that it is not so simple for
> the following reasons:
>
> Firstly, `envp_init` is surrounded by two other variables that have been
> explicitly marked as `static` which leads me to believe that this one
> was intentionally _not_ marked as static for some reason:
> | static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
> | static const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
> | static const char *panic_later, *panic_param;

I went looking to see the history here and it goes beyond git history. :)

>
> Secondly, there exists this `extern` declaration for `envp_init`:
> | init/do_mounts_initrd.c
> | 90: extern char *envp_init[];

This is the only user of call_usermodehelper_setup() that doesn't make
its own envp. And there's a deprecation warning in that function too:

9acc17baf1fd6 (Christoph Hellwig 2020-07-08 18:18:54 +0200 93) pr_warn("using deprecated initrd support, will be removed in 2021.\n");

> Any help here would be appreciated!

I recommend making it static and giving handle_initrd() its own copy:

diff --git a/init/do_mounts_initrd.c b/init/do_mounts_initrd.c
index 425f4bcf4b77..154bd0de85a6 100644
--- a/init/do_mounts_initrd.c
+++ b/init/do_mounts_initrd.c
@@ -87,7 +87,7 @@ static void __init handle_initrd(char *root_device_name)
{
struct subprocess_info *info;
static char *argv[] = { "linuxrc", NULL, };
- extern char *envp_init[];
+ static char *envp[] = { "HOME=/", "TERM=linux", NULL, };
int error;

pr_warn("using deprecated initrd support, will be removed in 2021.\n");
@@ -100,7 +100,7 @@ static void __init handle_initrd(char *root_device_name)
init_mkdir("/old", 0700);
init_chdir("/old");

- info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
+ info = call_usermodehelper_setup("/linuxrc", argv, envp,
GFP_KERNEL, init_linuxrc, NULL, NULL);
if (!info)
return;

--
Kees Cook