Skip to content

Considering uninitialized function parameters UB #1648

@ramosian-glider

Description

@ramosian-glider

I've learned recently that Clang may mark certain function parameters as noundef, and passing uninitialized data in such parameters will be considered an UB (see https://llvm.org/docs/LangRef.html for the definition of noundef).

Moreover, Clang is quite aggressive in assigning noundef and can do that even for parameters that aren't always used: https://godbolt.org/z/s1oPve6d4

I couldn't find any relevant bit in the C standard, so I am unsure whether or not this is the expected compiler behavior.
What I know for sure is that there are places in kernel where uninitialized arguments are being passed to functions, but thrown away afterwards:

BUG: KMSAN: uninit-value in walk_component+0x643/0x730 fs/namei.c:2026
 walk_component+0x643/0x730 fs/namei.c:2026
 lookup_last fs/namei.c:2475
 path_lookupat+0x281/0x6f0 fs/namei.c:2499
 filename_lookup+0x250/0x800 fs/namei.c:2528
 kern_path+0x7d/0x3a0 fs/namei.c:2618
 init_stat+0x76/0x143 fs/init.c:132
 clean_path+0x78/0x250 init/initramfs.c:313
 do_name+0x131/0xc02 init/initramfs.c:345
 write_buffer init/initramfs.c:432
 unpack_to_rootfs+0xa79/0xe17 init/initramfs.c:485
 do_populate_rootfs+0x5b/0x40e init/initramfs.c:674
 async_run_entry_fn+0x93/0x400 kernel/async.c:127
 process_one_work+0xb2b/0x13f0 kernel/workqueue.c:2289
 worker_thread+0x1086/0x1d80 kernel/workqueue.c:2436
 kthread+0x31f/0x430 kernel/kthread.c:376
 ret_from_fork+0x1f/0x30 ??:?

Local variable inode created at: 
 walk_component+0x4a/0x730 fs/namei.c:2005
 lookup_last fs/namei.c:2475
 path_lookupat+0x281/0x6f0 fs/namei.c:2499

(this is a report from a more conservative KMSAN version that reports uninits as they are passed to functions)

Apparently such code can be miscompiled by Clang, so the question is whether or not it is legitimate to pass uninitialized data to functions in the kernel? If it is, we probably need to build the kernel without enable_noundef_analysis?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion asked by issue author.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions