Messages in this thread Patch in this message | | | Date | Tue, 14 Jun 2022 16:48:53 +0200 | Subject | [PATCH] [RFC] Initialization of unused function parameters | From | Alexander Potapenko <> |
| |
Consider the patch below, where under certain circumstances initialization of `unsigned seq` and `struct inode *inode` passed into step_into() may be skipped. In particular, if the call to lookup_fast() in walk_component() returns NULL, and lookup_slow() returns a valid dentry, then the `seq` and `inode` will remain uninitialized until the call to step_into() (see [1] for more info).
Right now step_into() correctly handles this and does not use the uninitialized values, but explicitly initializing them will guarantee their sanity in the case of future changes to step_into().
The bigger question I want to raise here is whether we want to discourage passing uninitialized variables to functions in the kernel altogether. While this is not required by the C standard (see the whole discussion at [2]), KMSAN can help with detecting cases where uninits are passed to functions (there is a more aggressive -fsanitize-memory-param-retval compiler option for that). This is somewhat similar to -Wmaybe-uninitialized, but works dynamically and has no false positives.
Would initializing function parameters be a net benefit to the kernel?
[1] https://github.com/ClangBuiltLinux/linux/issues/1648#issuecomment-1146608063 [2] https://github.com/ClangBuiltLinux/linux/issues/1648
Cc: Evgenii Stepanov <eugenis@google.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Marco Elver <elver@google.com> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vitaly Buka <vitalybuka@google.com> Cc: linux-kernel@vger.kernel.org Cc: linux-toolchains@vger.kernel.org Signed-off-by: Alexander Potapenko <glider@google.com> --- fs/namei.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c index 509657fdf4f56..dde370338f5d6 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -2001,8 +2001,8 @@ static const char *handle_dots(struct nameidata *nd, int type) static const char *walk_component(struct nameidata *nd, int flags) { struct dentry *dentry; - struct inode *inode; - unsigned seq; + struct inode *inode = NULL; + unsigned seq = 0; /* * "." and ".." are special - ".." especially so because it has * to be able to know about the current root directory and @@ -3349,8 +3349,8 @@ static const char *open_last_lookups(struct nameidata *nd, struct dentry *dir = nd->path.dentry; int open_flag = op->open_flag; bool got_write = false; - unsigned seq; - struct inode *inode; + unsigned seq = 0; + struct inode *inode = NULL; struct dentry *dentry; const char *res; -- 2.36.1.476.g0c4daa206d-goog
| |