Re: [PATCH] file: Call security_file_alloc() after initializing the filp
From: Al Viro
Date: Tue Dec 09 2025 - 04:14:58 EST
On Tue, Dec 09, 2025 at 03:53:47PM +0800, Tianjia Zhang wrote:
> When developing a dedicated LSM module, we need to operate on the
> file object within the LSM function, such as retrieving the path.
> However, in `security_file_alloc()`, the passed-in `filp` is
> only a valid pointer; the content of `filp` is completely
> uninitialized and entirely random, which confuses the LSM function.
>
> Therefore, it is necessary to call `security_file_alloc()` only
> after the main fields of the `filp` object have been initialized.
> This patch only moves the call to `security_file_alloc()` to the
> end of the `init_file()` function.
Which fields would those be and why would ->file_alloc(), which is
not called anywhere else, depend on any values being stored there?
And how would init_file() know which path we are going to use
that struct file for, anyway, considering that file is allocated
and init_file() called *before* we get around to resolving the pathname?