Landlock: Signal Scoping Support
Subsystems (10)
v2 cover Landlock: Signal Scoping Support
This patch series adds scoping mechanism for signals.
Problem
=======
A sandboxed process is currently not restricted from sending signals (e.g. SIGKILL) to processes outside the sandbox since Landlock has no restriction on signals(see more details in [1]).
A simple way to apply this restriction would be to scope signals the same way abstract unix sockets are restricted.
[1]https://lore.kernel.org/all/[email protected]/
Solution
========
To solve this issue, we extend the "scoped" field in the Landlock ruleset attribute structure by introducing "LANDLOCK_SCOPED_SIGNAL" field to specify that a ruleset will deny sending any signals from within the sandbox domain to its parent(i.e. any parent sandbox or non-sandbox processes).
Example
=======
Create a sansboxed shell and pass the character "s" to LL_SCOPED:
LL_FD_RO=/ LL_FS_RW=. LL_SCOPED="s" ./sandboxer /bin/bash Try to send a signal(like SIGTRAP) to a process ID <PID> through:
kill -SIGTRAP <PID>
The sandboxed process should not be able to send the signal.
Tahera Fahimi (4):
Landlock: Add signal control selftest/Landlock: Signal restriction tests sample/Landlock: Support signal scoping restriction
Landlock: Document LANDLOCK_SCOPED_SIGNAL
Documentation/userspace-api/landlock.rst | 27 +- include/uapi/linux/landlock.h | 3 + samples/landlock/sandboxer.c | 13 +- security/landlock/limits.h | 2 +- security/landlock/task.c | 43 +++ tools/testing/selftests/landlock/base_test.c | 2 +- .../selftests/landlock/scoped_signal_test.c | 303 ++++++++++++++++++ 7 files changed, 376 insertions(+), 17 deletions(-) create mode 100644 tools/testing/selftests/landlock/scoped_signal_test.c
Signature · 1 line
2.34.1
PATCH v2 1/4 Landlock: Add signal control
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).
---
Chenges in versions:
V2:
* Remove signal_is_scoped function
* Applying reviews of V1
V1:
* Introducing LANDLOCK_SCOPE_SIGNAL
* Adding two hooks, hook_task_kill and hook_file_send_sigiotask for signal scoping.
--- include/uapi/linux/landlock.h | 3 +++ security/landlock/limits.h | 2 +- security/landlock/task.c | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-)
include/uapi/linux/landlock.h +3 -0
@@ -292,8 +292,11 @@ struct landlock_net_port_attr {* from connecting to an abstract unix socket created by a process * outside the related Landlock domain (e.g. a parent domain or a * non-sandboxed process). +* - %LANDLOCK_SCOPED_SIGNAL: Restrict a sandboxed process from sending a signal +* to another process outside sandbox domain. */ /* clang-format off */ #define LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET (1ULL << 0) +#define LANDLOCK_SCOPED_SIGNAL (1ULL << 1)/* clang-format on*/ #endif /* _UAPI_LINUX_LANDLOCK_H */
security/landlock/limits.h +1 -1
@@ -26,7 +26,7 @@#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1)#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET) -#define LANDLOCK_LAST_SCOPE LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET +#define LANDLOCK_LAST_SCOPE LANDLOCK_SCOPED_SIGNAL#define LANDLOCK_MASK_SCOPE ((LANDLOCK_LAST_SCOPE << 1) - 1)#define LANDLOCK_NUM_SCOPE __const_hweight64(LANDLOCK_MASK_SCOPE)/* clang-format on */
security/landlock/task.c +43 -0
54 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 2/4 selftest/Landlock: Signal restriction tests
In reply to Tahera FahimiThis patch expands Landlock ABI version 6 by providing tests for signal scoping mechanism. Base on kill(2), if the signal is 0, no signal will be sent, but the permission of a process to send a signal will be checked. Likewise, this test consider one signal for each signal category.
---
Chnages in versions:
V2:
* Moving tests from ptrace_test.c to scoped_signal_test.c
* Remove debugging statements.
* Covering all basic restriction scenarios by sending 0 as signal V1:
* Expanding Landlock ABI version 6 by providing basic tests for four signals to test signal scoping mechanism.
--- tools/testing/selftests/landlock/base_test.c | 2 +- .../selftests/landlock/scoped_signal_test.c | 303 ++++++++++++++++++ 2 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/landlock/scoped_signal_test.c
tools/testing/selftests/landlock/base_test.c +1 -1
@@ -75,7 +75,7 @@ TEST(abi_version)const struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs =LANDLOCK_ACCESS_FS_READ_FILE , }; -ASSERT_EQ (5 ,landlock_create_ruleset (NULL ,0 , +ASSERT_EQ (6 ,landlock_create_ruleset (NULL ,0 ,LANDLOCK_CREATE_RULESET_VERSION )); ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,
tools/testing/selftests/landlock/scoped_signal_test.c new file +303 -0
303 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 3/4 sample/Landlock: Support signal scoping restriction
In reply to Tahera FahimiA sandboxer can receive the character "s" as input from the environment variable LL_SCOPE to restrict itself from sending a signal to a process outside its scoped domain.
Example
=======
Create a sandboxed shell and pass the character "s" to LL_SCOPED:
LL_FS_RO=/ LL_FS_RW=. LL_SCOPED="s" ./sandboxer /bin/bash Try to send a SIGTRAP to a process with process ID <PID> through:
kill -SIGTRAP <PID>
The sandboxed process should not be able to send the signal.
--- samples/landlock/sandboxer.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
samples/landlock/sandboxer.c +9 -4
@@ -193,7 +193,8 @@ static bool check_ruleset_scope(const char *const env_var,bool ret = true;char *env_type_scope, *env_type_scope_next, *ipc_scoping_name; - ruleset_attr->scoped &= ~LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET ; + ruleset_attr->scoped &= ~(LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET | +LANDLOCK_SCOPED_SIGNAL ); env_type_scope =getenv (env_var);/* scoping is not supported by the user */ if (!env_type_scope)
@@ -207,6 +208,8 @@ static bool check_ruleset_scope(const char *const env_var,if (strcmp ("a" , ipc_scoping_name) ==0 ) { ruleset_attr->scoped |=LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET ; + }else if (strcmp ("s" , ipc_scoping_name) ==0 ) { + ruleset_attr->scoped |=LANDLOCK_SCOPED_SIGNAL ; }else {fprintf (stderr,"Unsupported scoping \"%s\"\n" , ipc_scoping_name);
@@ -258,7 +261,8 @@ int main(const int argc, char *const argv[], char *const *const envp) .handled_access_fs = access_fs_rw, .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP, - .scoped = LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET, + .scoped =LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET | + LANDLOCK_SCOPED_SIGNAL, };if (argc <2 ) {
@@ -295,7 +299,7 @@ int main(const int argc, char *const argv[], char *const *const envp)"%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" " "%s=\"9418\" " "%s=\"80:443\" " -"%s=\"a\" " +"%s=\"a:s\" " "%s bash -i\n\n" ,ENV_FS_RO_NAME ,ENV_FS_RW_NAME ,ENV_TCP_BIND_NAME , ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
@@ -369,7 +373,8 @@ int main(const int argc, char *const argv[], char *const *const envp) __attribute__((fallthrough));case 5 :/* Removes LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET for ABI < 6 */ - ruleset_attr.scoped &= ~LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET ; + ruleset_attr.scoped &= ~(LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET | +LANDLOCK_SCOPED_SIGNAL );fprintf (stderr,"Hint: You should update the running kernel " "to leverage Landlock features "
Signature · 1 line
2.34.1
PATCH v2 4/4 Landlock: Document LANDLOCK_SCOPED_SIGNAL
In reply to Tahera FahimiImproving Landlock ABI version 6 to support signal scoping with LANDLOCK_SCOPED_SIGNAL.
--- Documentation/userspace-api/landlock.rst | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-)
Documentation/userspace-api/landlock.rst +16 -11
@@ -8,7 +8,7 @@ Landlock: unprivileged access control ===================================== :Author: Mickaël Salaün -:Date: July 2024 +:Date: August 2024 The goal of Landlock is to enable to restrict ambient rights (e.g. global filesystem or network access) for a set of processes. Because Landlock
@@ -82,7 +82,8 @@ to be explicit about the denied-by-default access rights. LANDLOCK_ACCESS_NET_BIND_TCP | LANDLOCK_ACCESS_NET_CONNECT_TCP, .scoped = - LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET, + LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET | + LANDLOCK_SCOPED_SIGNAL, }; Because we may not know on which kernel version an application will be
@@ -123,7 +124,8 @@ version, and only use the available subset of access rights: ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV; case 5: /* Removes LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET for ABI < 6 */ - ruleset_attr.scoped &= ~LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET; + ruleset_attr.scoped &= ~(LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET | + LANDLOCK_SCOPED_SIGNAL); } This enables to create an inclusive ruleset that will contain our rules.
@@ -319,11 +321,14 @@ interactions between sandboxes. Each Landlock domain can be explicitly scoped for a set of actions by specifying it on a ruleset. For example, if a sandboxed process should not be able to :manpage:`connect(2)` to a non-sandboxed process through abstract :manpage:`unix(7)` sockets, we can specify such restriction -with ``LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET``. +with ``LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET``. Moreover, if a sandboxed process +should not be able to send a signal to a non-sandboxed process, we can specify +this restriction with ``LANDLOCK_SCOPED_SIGNAL``. -A sandboxed process can connect to a non-sandboxed process when its domain is -not scoped. If a process's domain is scoped, it can only connect to processes in -the same scoped domain. +A sandboxed process can access to a non-sandboxed process when its domain is +not scoped. If a process's domain is scoped, it can only access to processes in +the same scoped domain. For example, If a process is scoped to send signal to +other processes, it can only send signals to processes in the same scoped domain. IPC scoping does not support Landlock rules, so if a domain is scoped, no rules can be added to allow accessing to a resource outside of the scoped domain.
@@ -563,12 +568,12 @@ earlier ABI. Starting with the Landlock ABI version 5, it is possible to restrict the use of :manpage:`ioctl(2)` using the new ``LANDLOCK_ACCESS_FS_IOCTL_DEV`` right. -Abstract Unix sockets Restriction (ABI < 6) --------------------------------------------- +Abstract Unix sockets and Signal Restriction (ABI < 6) +------------------------------------------------------- With ABI version 6, it is possible to restrict connection to an abstract Unix socket -through ``LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET``, thanks to the ``scoped`` ruleset -attribute. +through ``LANDLOCK_SCOPED_ABSTRACT_UNIX_SOCKET`` and sending signal through +``LANDLOCK_SCOPED_SIGNAL``, thanks to the ``scoped`` ruleset attribute. .. _kernel_support:
Signature · 1 line
2.34.1
Re: [PATCH v2 1/4] Landlock: Add signal control
In reply to Tahera FahimiQuoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).
[...]
Quoted 16 lines
security/landlock/task.c +8
-0@@ -261,11 +261,54 @@ static int hook_unix_may_send(struct socket *const sock,return -EPERM ; } +static int hook_task_kill (struct task_struct *const p, +struct kernel_siginfo *const info,const int sig, +const struct cred *const cred) +{ +bool is_scoped; +const struct landlock_ruleset *target_dom; + +/* rcu is already locked */
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 281
Quoted 10 lines
security/landlock/task.c +10
-0+ target_dom =landlock_get_task_domain (p); +if (cred) +/* dealing with USB IO */ + is_scoped =domain_IPC_scope (landlock_cred (cred)->domain, + target_dom, +LANDLOCK_SCOPED_SIGNAL ); +else + is_scoped =domain_IPC_scope (landlock_get_current_domain (), + target_dom, +LANDLOCK_SCOPED_SIGNAL );
This might be a bit more concise if you turn it into something like:
/* only USB IO supplies creds */
cred = cred ?: current_cred();is_scoped = domain_IPC_scope(landlock_cred(cred)->domain,
target_dom, LANDLOCK_SCOPED_SIGNAL);
but that's just a question of style, feel free to keep it as-is depending on what you prefer.
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 12 lines
security/landlock/task.c +12
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum) +{ +bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);
I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following landlock_get_task_domain() would probably crash? But I'm not entirely sure about how this works.
I think the intended way to use this hook would be to instead use the "file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change the fown_struct to record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.
Quoted 9 lines
security/landlock/task.c +9
-0+/* rcu is already locked! */ + dom =landlock_get_task_domain (result); + target_dom =landlock_get_task_domain (tsk); + is_scoped =domain_IPC_scope (dom, target_dom,LANDLOCK_SCOPED_SIGNAL ); +put_task_struct (result); +if (is_scoped) +return 0 ; +return -EPERM ; +}
Re: [PATCH v2 1/4] Landlock: Add signal control
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).
[...]
Quoted 18 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 12 lines
security/landlock/task.c +12
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum) +{ +bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.
Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.
(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Re: [PATCH v2 1/4] Landlock: Add signal control
In reply to Jann HornQuoted 39 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 16 lines
security/landlock/task.c +8
-0@@ -261,11 +261,54 @@ static int hook_unix_may_send(struct socket *const sock,return -EPERM ; } +static int hook_task_kill (struct task_struct *const p, +struct kernel_siginfo *const info,const int sig, +const struct cred *const cred) +{ +bool is_scoped; +const struct landlock_ruleset *target_dom; + +/* rcu is already locked */ security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 281
Quoted 10 lines
security/landlock/task.c +10
-0+ target_dom =landlock_get_task_domain (p); +if (cred) +/* dealing with USB IO */ + is_scoped =domain_IPC_scope (landlock_cred (cred)->domain, + target_dom, +LANDLOCK_SCOPED_SIGNAL ); +else + is_scoped =domain_IPC_scope (landlock_get_current_domain (), + target_dom, +LANDLOCK_SCOPED_SIGNAL );This might be a bit more concise if you turn it into something like:
Quoted 7 lines
/* only USB IO supplies creds */
cred = cred ?:current_cred();is_scoped= domain_IPC_scope(landlock_cred(cred)->domain,target_dom, LANDLOCK_SCOPED_SIGNAL);but that's just a question of style, feel free to keep it as-is depending on what you prefer.
Hi Jann,
Thanks for the feedback:)
Quoted 9 lines
Quoted 9 lines
security/landlock/task.c +9
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum) +{
Quoted 8 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 3 lines
security/landlock/task.c +3
-0+bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.
I considered since the file structure can always be obtained, then the file owner PID always exist. I can check if we can use the credentials stored in struct file instead.
Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.
Because Landlock does not use any security blob for this purpose, I am not sure how to record the owner's doamin.
The alternative way looks nice.
Quoted 9 lines
Quoted 9 lines
security/landlock/task.c +9
-0+/* rcu is already locked! */ + dom =landlock_get_task_domain (result); + target_dom =landlock_get_task_domain (tsk); + is_scoped =domain_IPC_scope (dom, target_dom,LANDLOCK_SCOPED_SIGNAL ); +put_task_struct (result); +if (is_scoped) +return 0 ; +return -EPERM ; +}
Re: [PATCH v2 1/4] Landlock: Add signal control
Hi!
Quoted 19 lines · Tahera Fahimi
Quoted 10 lines · Jann Horn
Quoted 9 lines · Tahera Fahimi
security/landlock/task.c +9
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum) +{Quoted 8 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 3 lines
security/landlock/task.c +3
-0+bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.
Quoted 2 lines
I considered since the file structure can always be obtained, then the file owner PID always exist.
I think the file owner "struct pid" always exists here; but I think the PID does not necessarily point to a task_struct.
I think you can have a scenario where userspace does something like this:
int fd = <open some file descriptor somehow>;fcntl(fd, F_SETOWN, <PID of some other process>);pid_t child_pid = fork();
if (child_pid == 0) {
/* this executes in the child process */
sleep(5);
<do something on the fd that triggers send_sigio() or send_sigurg()> sleep(5);
exit(0);
}
/* this continues executing in the parent process */ exit(0);
At the fcntl(fd, F_SETOWN, ...) call, a reference-counted reference to the "struct pid" of the parent process will be stored in the file (this happens in "f_modown", which increments the reference count of the "struct pid" using "get_pid(...)"). But when the parent process exits, the pointer from the parent's "struct pid" to the parent's "task_struct" is removed, and the "task_struct" will eventually be deleted from memory.
So when, at a later time, something triggers send_sigio() or send_sigurg() and enters this LSM hook, I think "get_pid_task(fown->pid, fown->pid_type)" can return NULL.
Quoted 2 lines
I can check if we can use the credentials
stored in struct file instead.
(sort of relevant: The manpage
https://man7.org/linux/man-pages/man2/fcntl.2.html says "Note: The F_SETOWN operation records the caller's credentials at the time of the fcntl() call, and it is these saved credentials that are used for the permission checks.")
You mean the credentials in the "f_cred" member of "struct file", right? If so:
I don't think Landlock can use the existing credentials stored in file->f_cred for this. Consider the following scenario:
1. A process (with no landlock restrictions so far) opens a file. At this point, the caller's credentials (which indicate no landlock restrictions) are recorded in file->f_cred.
2. The process installs a landlock filter that restricts the ability to send signals. (This changes the credentials pointer of the process to a new set of credentials that points to the new landlock domain.)
3. Malicious code starts executing in the sandboxed process.
4. The malicious code uses F_SETOWN on the already-open file.
5. Something causes sending of a signal via send_sigio() or send_sigurg() on this file.
In this scenario, if the LSM hook used the landlock restrictions attached to file->f_cred, it would think the operation is not filtered by any landlock domain.
Quoted 9 lines
Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.Because Landlock does not use any security blob for this purpose, I am not sure how to record the owner's doamin.
I think landlock already has a landlock_file_security blob attached to "struct file" that you could use to store an extra landlock domain pointer for this, kinda like the "fown_sid" in SELinux? But doing this for landlock would be a little more annoying because you'd have to store a reference-counted pointer to the landlock domain in the landlock_file_security blob, so you'd have to make sure to also decrement the reference count when the file is freed (with the file_free_security hook), and you'd have to use locking (or atomic operations) to make sure that two concurrent file_set_fowner calls don't race in a dangerous way...
So I think it's fairly straightforward, but it would be kinda ugly.
Quoted 1 line
The alternative way looks nice.
Yeah, I agree that it looks nicer. (If you decide to implement it by refactoring the fown_struct, it would be a good idea to make that a separate patch in your patch series - partly because that way, the maintainers of that fs/fcntl.c code can review just the refactoring, and partly just because splitting logical changes into separate patches makes it easier to review the individual patches.)
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
security/landlock/task.c +9
-0+/* rcu is already locked! */ + dom =landlock_get_task_domain (result); + target_dom =landlock_get_task_domain (tsk); + is_scoped =domain_IPC_scope (dom, target_dom,LANDLOCK_SCOPED_SIGNAL ); +put_task_struct (result); +if (is_scoped) +return 0 ; +return -EPERM ; +}
Mickaël Salaün
maintainerRe: [PATCH v2 1/4] Landlock: Add signal control
In reply to Jann HornQuoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 289
Quoted 8 lines
security/landlock/task.c +8
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum)
I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 18 lines
Quoted 10 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 4 lines
security/landlock/task.c +4
-0+{ +bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.
Quoted 10 lines
(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
I agree, let's extend landlock_file_security with a new "fown" pointer to a Landlock domain. We'll need to call landlock_get_ruleset() in hook_file_send_sigiotask(), and landlock_put_ruleset() in a new hook_file_free_security().
I would be nice to to replace the redundant informations in fown_struct but that can wait.
Re: [PATCH v2 1/4] Landlock: Add signal control
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 289
Quoted 8 lines
security/landlock/task.c +8
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 18 lines
Quoted 10 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 4 lines
security/landlock/task.c +4
-0+{ +bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.Quoted 10 lines
(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().
I think we should add a new hook (hook_file_set_owner()) to initialize the "fown" pointer and call landlock_get_ruleset() in that? If we do not have hook_file_set_owner to store domain in "fown", can you please give me a hint on where to do that?
Thanks
Quoted 2 lines
I would be nice to to replace the redundant informations in
fown_structbut that can wait.
Re: [PATCH v2 1/4] Landlock: Add signal control
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 289
Quoted 8 lines
security/landlock/task.c +8
-0+if (is_scoped) +return 0 ; + +return -EPERM ; +} + +static int hook_file_send_sigiotask (struct task_struct *tsk, +struct fown_struct *fown,int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
security/landlock/task.c static int hook_unix_may_send(struct socket *const sock, · line 293
Quoted 4 lines
security/landlock/task.c +4
-0+{ +bool is_scoped; +const struct landlock_ruleset *dom, *target_dom; +struct task_struct *result =get_pid_task (fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?
Yeah. Initialize the pointer in the file_set_fowner hook, and read the pointer in the file_send_sigiotask hook.
Note that in the file_set_fowner hook, you'll probably need to use both landlock_get_ruleset() (to take a reference on the ruleset you're storing in the fown pointer) and landlock_put_ruleset() (to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in the file_set_fowner hook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)
Quoted 6 lines
If we do not
havehook_file_set_ownerto store domain in "fown", can you please give me a hint on where to do that?
ThanksQuoted 2 lines
I would be nice to to replace the redundant informations in
fown_structbut that can wait.
Mickaël Salaün
maintainerRe: [PATCH v2 1/4] Landlock: Add signal control
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.
Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)
Yes, I think this should work for file_set_fowner:
struct landlock_ruleset *prev_dom, *new_dom;
new_dom = landlock_get_current_domain();landlock_get_ruleset(new_dom);
/* Cf. f_modown() */ write_lock_irq(&filp->f_owner.lock);prev_dom = rcu_replace_pointer(&landlock_file(file)->fown_domain,
new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);
With landlock_put_ruleset_rcu() define with this:
security/landlock/ruleset.c +14 -0
@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } } +static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +} + +void landlock_put_ruleset_rcu (struct landlock_ruleset *const ruleset) +{ +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) +call_rcu (&ruleset->rcu, free_ruleset_rcu); +} +/** * landlock_merge_ruleset - Merge a ruleset with a domain *
security/landlock/ruleset.h +24 -7
@@ -190,19 +190,35 @@ struct landlock_ruleset {* @work_free: Enables to free a ruleset within a lockless * section. This is only used by * landlock_put_ruleset_deferred() when @usage reaches zero. -* The fields @lock, @usage, @num_rules, @num_layers and +* The fields @rcu, @lock, @usage, @num_rules, @num_layers and * @access_masks are then unused. */ struct work_struct work_free;struct { -/** -* @lock: Protects against concurrent modifications of -* @root, if @usage is greater than zero. -*/ -struct mutex lock; +union { +/** +* @rcu: Protects RCU read-side critical +* sections. This is only used by +* landlock_put_ruleset_rcu() when @usage +* reaches zero. +* +* Only used for domains. +*/ +struct rcu_head rcu; +/** +* @lock: Protects against concurrent +* modifications of @root_inode and +* @root_net_port, if @usage is greater than +* zero. +* +* Only used for rulesets. +*/ +struct mutex lock; + };/** * @usage: Number of processes (i.e. domains) or file -* descriptors referencing this ruleset. +* descriptors referencing this ruleset. It can be +* zero in RCU read-side critical sections. */ refcount_t usage;/**
@@ -241,6 +257,7 @@ landlock_create_ruleset(const access_mask_t access_mask_fs,void landlock_put_ruleset (struct landlock_ruleset *const ruleset);void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset); +void landlock_put_ruleset_rcu (struct landlock_ruleset *const ruleset); int landlock_insert_rule(struct landlock_ruleset *const ruleset, const struct landlock_id id,
Re: [PATCH v2 1/4] Landlock: Add signal control
Quoted 104 lines · Mickaël Salaün
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)Yes, I think this should work for
file_set_fowner:struct
landlock_ruleset*prev_dom,*new_dom;
new_dom=landlock_get_current_domain();landlock_get_ruleset(new_dom);/* Cf.
f_modown()*/write_lock_irq(&filp->f_owner.lock);prev_dom= rcu_replace_pointer(&landlock_file(file)->fown_domain,new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);With
landlock_put_ruleset_rcu()define with this:security/landlock/ruleset.c +0
-0
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 533
Quoted 10 lines
security/landlock/ruleset.c +7
-0@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } } +static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +}
free_ruleset() can block but RCU callbacks aren't allowed to block, that's why landlock_put_ruleset_deferred() exists.
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 539
Quoted 6 lines
security/landlock/ruleset.c +6
-0+ +void landlock_put_ruleset_rcu (struct landlock_ruleset *const ruleset) +{ +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) +call_rcu (&ruleset->rcu, free_ruleset_rcu); +}
No, this pattern of combining refcounting and RCU doesn't work.
One legal pattern is:
*The entire object* is subject to RCU; any refcount decrement that drops the refcount to 0 does call_rcu().
(This is the usual RCU refcounting pattern in the kernel.)
Another legal pattern is:
One particular *reference* is subject to RCU; when dropping this reference, *the refcount decrement is delayed with call_rcu()*. (This is basically the RCU pattern used for stuff like the reference from "struct pid" to "struct task_struct".)
But you can't use call_rcu() depending on whether the last dropped reference happened to be a reference that required RCU; what if the refcount is 2, then you first call landlock_put_ruleset_rcu() which decrements the refcount to 1, and immediately afterwards you call landlock_put_ruleset() which drops the refcount to 0 and frees the object without waiting for an RCU grace period? Like so:
thread A thread B
======== ========rcu_read_lock()
ruleset = rcu_dereference(...->fown_domain)
ruleset = rcu_replace_pointer(...->fown_domain, new_dom, ...)
landlock_put_ruleset_rcu(ruleset)
landlock_put_ruleset(ruleset)
free_ruleset(ruleset)
kfree(ruleset)access ruleset [UAF]rcu_read_unlock()
So if you want to use RCU lifetime for this, I think you'll have to turn landlock_put_ruleset() and landlock_put_ruleset_deferred() into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules a work_struct which then does free_ruleset().
I think that would be a little ugly, and it would look nicer to just use normal locking in the file_send_sigiotask hook?
Mickaël Salaün
maintainerRe: [PATCH v2 1/4] Landlock: Add signal control
Quoted 108 lines · Jann Horn
Quoted 107 lines · Mickaël Salaün
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)Yes, I think this should work for
file_set_fowner:struct
landlock_ruleset*prev_dom,*new_dom;
new_dom=landlock_get_current_domain();landlock_get_ruleset(new_dom);/* Cf.
f_modown()*/write_lock_irq(&filp->f_owner.lock);prev_dom= rcu_replace_pointer(&landlock_file(file)->fown_domain,new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);With
landlock_put_ruleset_rcu()define with this:security/landlock/ruleset.c +0
-0@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } }
Quoted 10 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 533
Quoted 7 lines
security/landlock/ruleset.c +7
-0+static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +}
free_ruleset()can block but RCU callbacks aren't allowed to block, that's whylandlock_put_ruleset_deferred()exists.
Yes, but landlock_put_ruleset_deferred() doesn't wait for RCU read-side critical sections.
Quoted 32 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 539
Quoted 7 lines
security/landlock/ruleset.c +6
-0+ +void landlock_put_ruleset_rcu (struct landlock_ruleset *const ruleset) +{ +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) +call_rcu (&ruleset->rcu, free_ruleset_rcu); +}No, this pattern of combining refcounting and RCU doesn't work.
One legal pattern is:
*The entire object* is subject to RCU; any refcount decrement that drops the refcount to 0 doescall_rcu().
(This is the usual RCU refcounting pattern in the kernel.)Another legal pattern is:
One particular *reference* is subject to RCU; when dropping this reference, *the refcount decrement is delayed with call_rcu()*. (This is basically the RCU pattern used for stuff like the reference from "struct pid" to "structtask_struct".)But you can't use
call_rcu()depending on whether the last dropped reference happened to be a reference that required RCU; what if the refcount is 2, then you first calllandlock_put_ruleset_rcu()which decrements the refcount to 1, and immediately afterwards you calllandlock_put_ruleset()which drops the refcount to 0 and frees the object without waiting for an RCU grace period? Like so:thread A thread B
======== ========rcu_read_lock()
ruleset =rcu_dereference(...->fown_domain)
Quoted 7 lines
ruleset = rcu_replace_pointer(...->fown_domain, new_dom, ...) landlock_put_ruleset_rcu(ruleset) landlock_put_ruleset(ruleset) free_ruleset(ruleset) kfree(ruleset)access ruleset [UAF]
rcu_read_unlock()
Indeed
Quoted 9 lines
So if you want to use RCU lifetime for this, I think you'll have to turn
landlock_put_ruleset()andlandlock_put_ruleset_deferred()into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules awork_structwhich then doesfree_ruleset().I think that would be a little ugly, and it would look nicer to just use normal locking in the
file_send_sigiotaskhook?
I don't see how we can do that without delaying the free_ruleset() call to after the RCU read-side critical section in f_setown().
What about calling refcount_dec_and_test() in free_ruleset_rcu()? That would almost always queue this call but it looks safe.
An alternative might be to call synchronize_rcu() in free_ruleset(), but it's a big ugly too.
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock. And it looks weird that security_file_set_fowner() isn't called by f_modown() with the same locking to avoid races.
Mickaël Salaün
maintainerRe: [PATCH v2 1/4] Landlock: Add signal control
Quoted 172 lines · Mickaël Salaün
Quoted 108 lines · Jann Horn
Quoted 107 lines · Mickaël Salaün
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)Yes, I think this should work for
file_set_fowner:struct
landlock_ruleset*prev_dom,*new_dom;
new_dom=landlock_get_current_domain();landlock_get_ruleset(new_dom);/* Cf.
f_modown()*/write_lock_irq(&filp->f_owner.lock);prev_dom= rcu_replace_pointer(&landlock_file(file)->fown_domain,new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);With
landlock_put_ruleset_rcu()define with this:security/landlock/ruleset.c +0
-0@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } }Quoted 10 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 533
Quoted 7 lines
security/landlock/ruleset.c +7
-0+static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +}
free_ruleset()can block but RCU callbacks aren't allowed to block, that's whylandlock_put_ruleset_deferred()exists.Yes, but
landlock_put_ruleset_deferred()doesn't wait for RCU read-side critical sections.Quoted 32 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 539
Quoted 7 lines
security/landlock/ruleset.c +6
-0+ +void landlock_put_ruleset_rcu (struct landlock_ruleset *const ruleset) +{ +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) +call_rcu (&ruleset->rcu, free_ruleset_rcu); +}No, this pattern of combining refcounting and RCU doesn't work.
One legal pattern is:
*The entire object* is subject to RCU; any refcount decrement that drops the refcount to 0 doescall_rcu().
(This is the usual RCU refcounting pattern in the kernel.)Another legal pattern is:
One particular *reference* is subject to RCU; when dropping this reference, *the refcount decrement is delayed with call_rcu()*. (This is basically the RCU pattern used for stuff like the reference from "struct pid" to "structtask_struct".)But you can't use
call_rcu()depending on whether the last dropped reference happened to be a reference that required RCU; what if the refcount is 2, then you first calllandlock_put_ruleset_rcu()which decrements the refcount to 1, and immediately afterwards you calllandlock_put_ruleset()which drops the refcount to 0 and frees the object without waiting for an RCU grace period? Like so:thread A thread B
======== ========rcu_read_lock()
ruleset =rcu_dereference(...->fown_domain)Quoted 7 lines
ruleset = rcu_replace_pointer(...->fown_domain, new_dom, ...) landlock_put_ruleset_rcu(ruleset) landlock_put_ruleset(ruleset) free_ruleset(ruleset) kfree(ruleset)access ruleset [UAF]
rcu_read_unlock()Indeed
Quoted 7 lines
So if you want to use RCU lifetime for this, I think you'll have to turn
landlock_put_ruleset()andlandlock_put_ruleset_deferred()into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules awork_structwhich then doesfree_ruleset().
Quoted 8 lines
Quoted 2 lines
I think that would be a little ugly, and it would look nicer to just use normal locking in the
file_send_sigiotaskhook?I don't see how we can do that without delaying the
free_ruleset()call to after the RCU read-side critical section inf_setown().What about calling
refcount_dec_and_test()infree_ruleset_rcu()? That would almost always queue this call but it looks safe.
That's the second pattern you pointed out just above. Because we should only use the related struct rcu_head for this call site (which is protected with f_owner.lock), we should make that explicit with a "_fown" suffix for the function (landlock_put_ruleset_rcu_fown) and the ruleset's field (rcu_fown).
Quoted 8 lines
An alternative might be to call
synchronize_rcu()infree_ruleset(), but it's a big ugly too.BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock. And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.
Re: [PATCH v2 1/4] Landlock: Add signal control
In reply to Mickaël SalaünQuoted 120 lines · Mickaël Salaün
Quoted 108 lines · Jann Horn
Quoted 107 lines · Mickaël Salaün
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)Yes, I think this should work for
file_set_fowner:struct
landlock_ruleset*prev_dom,*new_dom;
new_dom=landlock_get_current_domain();landlock_get_ruleset(new_dom);/* Cf.
f_modown()*/write_lock_irq(&filp->f_owner.lock);prev_dom= rcu_replace_pointer(&landlock_file(file)->fown_domain,new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);With
landlock_put_ruleset_rcu()define with this:security/landlock/ruleset.c +0
-0@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } }Quoted 10 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 533
Quoted 7 lines
security/landlock/ruleset.c +7
-0+static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +}
free_ruleset()can block but RCU callbacks aren't allowed to block, that's whylandlock_put_ruleset_deferred()exists.
Quoted 2 lines
Yes, but
landlock_put_ruleset_deferred()doesn't wait for RCU read-side critical sections.
Ah, I phrased that badly - I didn't mean to suggest that you should use landlock_put_ruleset_deferred() as a replacement for call_rcu().
[...]
Quoted 11 lines
Quoted 8 lines
So if you want to use RCU lifetime for this, I think you'll have to turn
landlock_put_ruleset()andlandlock_put_ruleset_deferred()into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules awork_structwhich then doesfree_ruleset().I think that would be a little ugly, and it would look nicer to just use normal locking in the
file_send_sigiotaskhook?I don't see how we can do that without delaying the
free_ruleset()call to after the RCU read-side critical section inf_setown().
It should work if you used landlock_put_ruleset_deferred() instead of landlock_put_ruleset().
Quoted 2 lines
What about calling
refcount_dec_and_test()infree_ruleset_rcu()? That would almost always queue this call but it looks safe.
Every queued RCU invocation needs to have its own rcu_head - I think the approach you're suggesting could end up queuing the same rcu_head multiple times?
Quoted 5 lines
An alternative might be to call
synchronize_rcu()infree_ruleset(), but it's a big ugly too.BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Mickaël Salaün
maintainerRe: [PATCH v2 1/4] Landlock: Add signal control
In reply to Mickaël SalaünQuoted 179 lines · Mickaël Salaün
Quoted 178 lines · Mickaël Salaün
Quoted 108 lines · Jann Horn
Quoted 107 lines · Mickaël Salaün
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)Yes, I think this should work for
file_set_fowner:struct
landlock_ruleset*prev_dom,*new_dom;
new_dom=landlock_get_current_domain();landlock_get_ruleset(new_dom);/* Cf.
f_modown()*/write_lock_irq(&filp->f_owner.lock);prev_dom= rcu_replace_pointer(&landlock_file(file)->fown_domain,new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);With
landlock_put_ruleset_rcu()define with this:security/landlock/ruleset.c +0
-0@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } }Quoted 10 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 533
Quoted 7 lines
security/landlock/ruleset.c +7
-0+static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +}
free_ruleset()can block but RCU callbacks aren't allowed to block, that's whylandlock_put_ruleset_deferred()exists.Yes, but
landlock_put_ruleset_deferred()doesn't wait for RCU read-side critical sections.Quoted 32 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 539
Quoted 7 lines
security/landlock/ruleset.c +6
-0+ +void landlock_put_ruleset_rcu (struct landlock_ruleset *const ruleset) +{ +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) +call_rcu (&ruleset->rcu, free_ruleset_rcu); +}No, this pattern of combining refcounting and RCU doesn't work.
One legal pattern is:
*The entire object* is subject to RCU; any refcount decrement that drops the refcount to 0 doescall_rcu().
(This is the usual RCU refcounting pattern in the kernel.)Another legal pattern is:
One particular *reference* is subject to RCU; when dropping this reference, *the refcount decrement is delayed with call_rcu()*. (This is basically the RCU pattern used for stuff like the reference from "struct pid" to "structtask_struct".)But you can't use
call_rcu()depending on whether the last dropped reference happened to be a reference that required RCU; what if the refcount is 2, then you first calllandlock_put_ruleset_rcu()which decrements the refcount to 1, and immediately afterwards you calllandlock_put_ruleset()which drops the refcount to 0 and frees the object without waiting for an RCU grace period? Like so:thread A thread B
======== ========rcu_read_lock()
ruleset =rcu_dereference(...->fown_domain)Quoted 7 lines
ruleset = rcu_replace_pointer(...->fown_domain, new_dom, ...) landlock_put_ruleset_rcu(ruleset) landlock_put_ruleset(ruleset) free_ruleset(ruleset) kfree(ruleset)access ruleset [UAF]
rcu_read_unlock()Indeed
Quoted 9 lines
So if you want to use RCU lifetime for this, I think you'll have to turn
landlock_put_ruleset()andlandlock_put_ruleset_deferred()into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules awork_structwhich then doesfree_ruleset().I think that would be a little ugly, and it would look nicer to just use normal locking in the
file_send_sigiotaskhook?I don't see how we can do that without delaying the
free_ruleset()call to after the RCU read-side critical section inf_setown().
Quoted 8 lines
Quoted 2 lines
What about calling
refcount_dec_and_test()infree_ruleset_rcu()? That would almost always queue this call but it looks safe.That's the second pattern you pointed out just above. Because we should only use the related struct
rcu_headfor this call site (which is protected with f_owner.lock), we should make that explicit with a "_fown" suffix for the function (landlock_put_ruleset_rcu_fown) and the ruleset's field (rcu_fown).
Hmm, this is not enough because this function site could still be called with the same ruleset several time, which could lead to memory leak and potential rcu_head pointers inconsistencies...
Quoted 9 lines
Quoted 9 lines
An alternative might be to call
synchronize_rcu()infree_ruleset(), but it's a big ugly too.BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock. And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.
Mickaël Salaün
maintainerf_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
In reply to Jann HornTalking about f_modown() and security_file_set_fowner(), it looks like there are some issues:
Quoted 1 line · Jann Horn
On Fri, Aug 9, 2024 at 12:59 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 2 lines
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.
Quoted 6 lines
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Would it be OK (for VFS, SELinux, and Smack maintainers) to move the security_file_set_fowner() call into f_modown(), especially where UID/EUID are populated. That would only call security_file_set_fowner() when the fown is actually set, which I think could also fix a bug for SELinux and Smack.
Could we replace the uid and euid fields with a pointer to the current credentials? This would enables LSMs to not copy the same kind of credential informations and save some memory, simplify credential management, and improve consistency.
Mickaël Salaün
maintainerRe: [PATCH v2 1/4] Landlock: Add signal control
In reply to Jann HornQuoted 134 lines · Jann Horn
Quoted 120 lines · Mickaël Salaün
Quoted 108 lines · Jann Horn
Quoted 107 lines · Mickaël Salaün
Quoted 66 lines · Jann Horn
Quoted 55 lines · Tahera Fahimi
Quoted 54 lines · Mickaël Salaün
Quoted 11 lines · Jann Horn
Quoted 9 lines · Jann Horn
Quoted 8 lines · Tahera Fahimi
Currently, a sandbox process is not restricted to send a signal (e.g. SIGKILL) to a process outside of the sandbox environment. Ability to sending a signal for a sandboxed process should be scoped the same way abstract unix sockets are scoped. Therefore, we extend "scoped" field in a ruleset with "
LANDLOCK_SCOPED_SIGNAL" to specify that a ruleset will deny sending any signal from within a sandbox process to its parent(i.e. any parent sandbox or non-sandboxed procsses).[...]
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (is_scoped) + return 0; + + return -EPERM; +} + +static int hook_file_send_sigiotask(struct task_struct *tsk, + struct fown_struct *fown, int signum)I was wondering if we should handle this case, but I guess it makes sense to have a consistent policy for all kind of user-triggerable signals.
Quoted 28 lines
Quoted 10 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + bool is_scoped; + const struct landlock_ruleset *dom, *target_dom; + struct task_struct *result = get_pid_task(fown->pid, fown->pid_type);I'm not an expert on how the fowner stuff works, but I think this will probably give you "result = NULL" if the file owner PID has already exited, and then the following
landlock_get_task_domain()would probably crash? But I'm not entirely sure about how this works.Quoted 7 lines
I think the intended way to use this hook would be to instead use the "
file_set_fowner" hook to record the owning domain (though the setup for that is going to be kind of a pain...), see the Smack and SELinux definitions of that hook. Or alternatively maybe it would be even nicer to change thefown_structto record a cred* instead of a uid and euid and then use the domain from those credentials for this hook... I'm not sure which of those would be easier.(For what it's worth, I think the first option would probably be easier to implement and ship for now, since you can basically copy what Smack and SELinux are already doing in their implementations of these hooks. I think the second option would theoretically result in nicer code, but it might require a bit more work, and you'd have to include the maintainers of the file locking code in the review of such refactoring and have them approve those changes. So if you want to get this patchset into the kernel quickly, the first option might be better for now?)
Quoted 6 lines
Quoted 4 lines
I agree, let's extend
landlock_file_securitywith a new "fown" pointer to a Landlock domain. We'll need to calllandlock_get_ruleset()inhook_file_send_sigiotask(), andlandlock_put_ruleset()in a newhook_file_free_security().I think we should add a new hook (
hook_file_set_owner()) to initialize the "fown" pointer and calllandlock_get_ruleset()in that?Yeah. Initialize the pointer in the
file_set_fownerhook, and read the pointer in thefile_send_sigiotaskhook.Quoted 13 lines
Note that in the
file_set_fownerhook, you'll probably need to use bothlandlock_get_ruleset()(to take a reference on the ruleset you're storing in the fown pointer) andlandlock_put_ruleset()(to drop the reference to the ruleset that the fown pointer was pointing to before). And you'll need to use some kind of lock to protect the fown pointer - either by adding an appropriate lock next to your fown pointer or by using some appropriate existing lock in "struct file". Probably it's cleanest to have your own lock for this? (This lock will have to be something like a spinlock, not a mutex, since you need to be able to acquire it in thefile_set_fownerhook, which runs inside an RCU read-side critical section, where sleeping is forbidden - acquiring a mutex can sleep and therefore is forbidden in this context, acquiring a spinlock can't sleep.)Yes, I think this should work for
file_set_fowner:struct
landlock_ruleset*prev_dom,*new_dom;
new_dom=landlock_get_current_domain();landlock_get_ruleset(new_dom);/* Cf.
f_modown()*/write_lock_irq(&filp->f_owner.lock);prev_dom= rcu_replace_pointer(&landlock_file(file)->fown_domain,new_dom, lockdep_is_held(&filp->f_owner.lock));
write_unlock_irq(&filp->f_owner.lock);
landlock_put_ruleset_rcu(prev_dom);With
landlock_put_ruleset_rcu()define with this:security/landlock/ruleset.c +0
-0@@ -524,6 +524,20 @@ void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) } }Quoted 10 lines
security/landlock/ruleset.c void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset) · line 533
Quoted 7 lines
security/landlock/ruleset.c +7
-0+static void free_ruleset_rcu (struct rcu_head *const head) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(head, struct landlock_ruleset, rcu); +free_ruleset (ruleset); +}
free_ruleset()can block but RCU callbacks aren't allowed to block, that's whylandlock_put_ruleset_deferred()exists.Quoted 2 lines
Yes, but
landlock_put_ruleset_deferred()doesn't wait for RCU read-side critical sections.Ah, I phrased that badly - I didn't mean to suggest that you should use
landlock_put_ruleset_deferred()as a replacement forcall_rcu().[...]
Quoted 6 lines
Quoted 6 lines
So if you want to use RCU lifetime for this, I think you'll have to turn
landlock_put_ruleset()andlandlock_put_ruleset_deferred()into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules awork_structwhich then doesfree_ruleset().
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I think that would be a little ugly, and it would look nicer to just use normal locking in the
file_send_sigiotaskhook?I don't see how we can do that without delaying the
free_ruleset()call to after the RCU read-side critical section inf_setown().It should work if you used
landlock_put_ruleset_deferred()instead oflandlock_put_ruleset().
Calling landlock_put_ruleset_deferred() in hook_file_set_fowner() or replacing all landlock_put_ruleset() calls?
The deferred work queue is not guarantee to run after all concurrent RCU read-side critical sections right? Calling synchronize_rcu() in free_ruleset_work() should give this guarantee, but it's not nice. We could add a boolean in landlock_ruleset to only call synchronize_rcu() when required (i.e. called from file_set_fowner).
Quoted 7 lines
Quoted 3 lines
What about calling
refcount_dec_and_test()infree_ruleset_rcu()? That would almost always queue this call but it looks safe.Every queued RCU invocation needs to have its own
rcu_head- I think the approach you're suggesting could end up queuing the samercu_headmultiple times?
Right
Quoted 3 lines
Quoted 3 lines
An alternative might be to call
synchronize_rcu()infree_ruleset(), but it's a big ugly too.
Re: [PATCH v2 1/4] Landlock: Add signal control
Quoted 3 lines · Mickaël Salaün
Quoted 2 lines · Jann Horn
Quoted 1 line · Mickaël Salaün
On Thu, Aug 08, 2024 at 04:42:23PM +0200, Jann Horn wrote:
[...]
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
So if you want to use RCU lifetime for this, I think you'll have to turn
landlock_put_ruleset()andlandlock_put_ruleset_deferred()into one common function that always, when reaching refcount 0, schedules an RCU callback which then schedules awork_structwhich then doesfree_ruleset().I think that would be a little ugly, and it would look nicer to just use normal locking in the
file_send_sigiotaskhook?
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I don't see how we can do that without delaying the
free_ruleset()call to after the RCU read-side critical section inf_setown().It should work if you used
landlock_put_ruleset_deferred()instead oflandlock_put_ruleset().Calling
landlock_put_ruleset_deferred()inhook_file_set_fowner()or replacing alllandlock_put_ruleset()calls?
Calling landlock_put_ruleset_deferred() in hook_file_set_fowner().
Quoted 2 lines
The deferred work queue is not guarantee to run after all concurrent RCU read-side critical sections right?
Yes, I was talking about my "it would look nicer to just use normal locking in the file_send_sigiotask hook" suggestion - don't use any RCU stuff, just use the same lock in file_set_fowner and file_send_sigiotask.
Re: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
In reply to Mickaël SalaünQuoted 32 lines · Mickaël Salaün
Talking about
f_modown()andsecurity_file_set_fowner(), it looks like there are some issues:Quoted 1 line · Jann Horn
On Fri, Aug 9, 2024 at 12:59 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 2 lines
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.Quoted 6 lines
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Would it be OK (for VFS, SELinux, and Smack maintainers) to move the
security_file_set_fowner()call intof_modown(), especially where UID/EUID are populated. That would only callsecurity_file_set_fowner()when the fown is actually set, which I think could also fix a bug for SELinux and Smack.
Quoted 4 lines
Could we replace the uid and euid fields with a pointer to the current credentials? This would enables LSMs to not copy the same kind of credential informations and save some memory, simplify credential management, and improve consistency.
To clarify: These two paragraphs are supposed to be two alternative options, right? One option is to call security_file_set_fowner() with the lock held, the other option is to completely rip out the security_file_set_fowner() hook and instead let the VFS provide LSMs with the creds they need for the file_send_sigiotask hook?
Christian Brauner
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
Quoted 38 lines · Jann Horn
Quoted 32 lines · Mickaël Salaün
Talking about
f_modown()andsecurity_file_set_fowner(), it looks like there are some issues:Quoted 1 line · Jann Horn
On Fri, Aug 9, 2024 at 12:59 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 2 lines
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.Quoted 6 lines
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Would it be OK (for VFS, SELinux, and Smack maintainers) to move the
security_file_set_fowner()call intof_modown(), especially where UID/EUID are populated. That would only callsecurity_file_set_fowner()when the fown is actually set, which I think could also fix a bug for SELinux and Smack.Quoted 4 lines
Could we replace the uid and euid fields with a pointer to the current credentials? This would enables LSMs to not copy the same kind of credential informations and save some memory, simplify credential management, and improve consistency.
Quoted 5 lines
To clarify: These two paragraphs are supposed to be two alternative options, right? One option is to call
security_file_set_fowner()with the lock held, the other option is to completely rip out thesecurity_file_set_fowner()hook and instead let the VFS provide LSMs with the creds they need for thefile_send_sigiotaskhook?
I think it would be fine to stash the credentials in struct fown_struct same as we do for struct file itself. Calling security_file_set_fowner() with the irq lock held seems suboptimal to me. Plus, this also means one less LSM hook and that seems like a win too.
Paul Moore
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
In reply to Jann HornQuoted 38 lines · Jann Horn
Quoted 32 lines · Mickaël Salaün
Talking about
f_modown()andsecurity_file_set_fowner(), it looks like there are some issues:Quoted 1 line · Jann Horn
On Fri, Aug 9, 2024 at 12:59 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 2 lines
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.Quoted 6 lines
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Would it be OK (for VFS, SELinux, and Smack maintainers) to move the
security_file_set_fowner()call intof_modown(), especially where UID/EUID are populated. That would only callsecurity_file_set_fowner()when the fown is actually set, which I think could also fix a bug for SELinux and Smack.Quoted 4 lines
Could we replace the uid and euid fields with a pointer to the current credentials? This would enables LSMs to not copy the same kind of credential informations and save some memory, simplify credential management, and improve consistency.
Quoted 5 lines
To clarify: These two paragraphs are supposed to be two alternative options, right? One option is to call
security_file_set_fowner()with the lock held, the other option is to completely rip out thesecurity_file_set_fowner()hook and instead let the VFS provide LSMs with the creds they need for thefile_send_sigiotaskhook?
I'm not entirely clear on what is being proposed either. Some quick pseudo code might do wonders here to help clarify things.
From a LSM perspective I suspect we are always going to need some sort of hook in the F_SETOWN code path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time. While I think it is okay if we want to consider relocating the security_file_set_fowner() within the F_SETOWN call path, I don't think we can remove it, even if we add additional LSM security blobs.
Signature · 1 line
paul-moore.com
Re: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
Quoted 46 lines · Paul Moore
Quoted 38 lines · Jann Horn
Quoted 32 lines · Mickaël Salaün
Talking about
f_modown()andsecurity_file_set_fowner(), it looks like there are some issues:Quoted 1 line · Jann Horn
On Fri, Aug 9, 2024 at 12:59 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 2 lines
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.Quoted 6 lines
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Would it be OK (for VFS, SELinux, and Smack maintainers) to move the
security_file_set_fowner()call intof_modown(), especially where UID/EUID are populated. That would only callsecurity_file_set_fowner()when the fown is actually set, which I think could also fix a bug for SELinux and Smack.Quoted 4 lines
Could we replace the uid and euid fields with a pointer to the current credentials? This would enables LSMs to not copy the same kind of credential informations and save some memory, simplify credential management, and improve consistency.
Quoted 5 lines
To clarify: These two paragraphs are supposed to be two alternative options, right? One option is to call
security_file_set_fowner()with the lock held, the other option is to completely rip out thesecurity_file_set_fowner()hook and instead let the VFS provide LSMs with the creds they need for thefile_send_sigiotaskhook?
Quoted 7 lines
I'm not entirely clear on what is being proposed either. Some quick pseudo code might do wonders here to help clarify things.
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.
The only thing LSMs currently do there is capture state from current->cred. So if the VFS takes care of capturing current->cred there, we should be able to rip out all the file_set_fowner stuff. Something like this (totally untested):
fs/fcntl.c +0 -0
@@ -98,8 +98,9 @@ static void f_modown(struct file *filp, struct pid
*pid, enum pid_type type,
if (pid) {
const struct cred *cred = current_cred();
- filp->f_owner.uid = cred->uid;
- filp->f_owner.euid = cred->euid;
+ if (filp->f_owner.owner_cred)
+ put_cred(filp->f_owner.owner_cred);
+ filp->f_owner.owner_cred = get_current_cred();
}
}
write_unlock_irq(&filp->f_owner.lock);@@ -108,7 +109,6 @@ static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
int force)
{
- security_file_set_fowner(filp);
f_modown(filp, pid, type, force); }
EXPORT_SYMBOL(__f_setown);
fs/file_table.c +2 -0
@@ -426,6 +426,8 @@ static void __fput(struct file *file) }fops_put (file->f_op);put_pid (file->f_owner.pid); +if (file->f_owner.owner_cred) +put_cred (file->f_owner.owner_cred);put_file_access (file);dput (dentry);if (unlikely (mode &FMODE_NEED_UNMOUNT ))
include/linux/fs.h +0 -0
@@ -950,7 +950,7 @@ struct fown_struct {rwlock_t lock;/* protects pid, uid, euid fields */ struct pid *pid;/* pid or -pgrp where SIGIO should be sent */ enum pid_type pid_type;/* Kind of process group SIGIO
should be sent to */
- kuid_t uid, euid; /* uid/euid of process setting the owner */
+ const struct cred __rcu *owner_cred;
int signum; /* posix.1b rt signal to be
delivered on IO */
};
include/linux/lsm_hook_defs.h +0 -1
@@ -197,7 +197,6 @@ LSM_HOOK(int, 0, file_mprotect, struct vm_area_struct *vma, LSM_HOOK(int, 0, file_lock, struct file *file, unsigned int cmd) LSM_HOOK(int, 0, file_fcntl, struct file *file, unsigned int cmd, unsigned long arg) -LSM_HOOK(void, LSM_RET_VOID, file_set_fowner, struct file *file) LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk, struct fown_struct *fown, int sig) LSM_HOOK(int, 0, file_receive, struct file *file)
include/linux/security.h +0 -0
@@ -1079,11 +1079,6 @@ static inline int security_file_fcntl(struct
file *file, unsigned int cmd,
return 0;
}
-static inline void security_file_set_fowner(struct file *file) -{
- return;
-}
-
static inline int security_file_send_sigiotask(struct task_struct *tsk,
struct fown_struct *fown, int sig)
security/security.c +0 -0
@@ -2924,20 +2924,6 @@ int security_file_fcntl(struct file *file,
unsigned int cmd, unsigned long arg)
return call_int_hook(file_fcntl, file, cmd, arg);
}
-/**
- * security_file_set_fowner() - Set the file owner info in the LSM blob
- * @file: the file
- *
- * Save owner security information (typically from current->security) in
- * file->f_security for later use by the send_sigiotask hook.
- *
- * Return: Returns 0 on success.
- */
-void security_file_set_fowner(struct file *file)
-{
- call_void_hook(file_set_fowner, file);
-}
-
/**
* security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
* @tsk: target task
security/selinux/hooks.c +0 -1
@@ -3649,7 +3649,6 @@ static int selinux_file_alloc_security(struct file *file)u32 sid =current_sid (); fsec->sid = sid; - fsec->fown_sid = sid;return 0 ; }
@@ -3923,24 +3922,16 @@ static int selinux_file_fcntl(struct file
*file, unsigned int cmd,
return err;
}
-static void selinux_file_set_fowner(struct file *file)
-{
- struct file_security_struct *fsec;
-
- fsec = selinux_file(file);
- fsec->fown_sid = current_sid();
-}
-
static int selinux_file_send_sigiotask(struct task_struct *tsk,
struct fown_struct *fown, int signum)
{
- struct file *file;
+ /* struct fown_struct is never outside the context of a struct file */
+ struct file *file = container_of(fown, struct file, f_owner);
u32 sid = task_sid_obj(tsk);
u32 perm;
struct file_security_struct *fsec;
-
- /* struct fown_struct is never outside the context of a struct file */
- file = container_of(fown, struct file, f_owner);
+ struct cred_struct *fown_cred = rcu_dereference(fown->owner_cred);
+ u32 fown_sid = cred_sid(fown_cred ?: file->f_cred);fsec = selinux_file(file);
@@ -3949,7 +3940,7 @@ static int selinux_file_send_sigiotask(struct task_struct *tsk,
else
perm = signal_to_av(signum);- return avc_has_perm(fsec->fown_sid, sid,
+ return avc_has_perm(fown_sid, sid,
SECCLASS_PROCESS, perm, NULL);
}
(quoted diff) +0 -0
b/security/selinux/include/objsec.h
index dea1d6f3ed2d..d55b7f8d3a3d 100644
security/selinux/include/objsec.h +0 -1
@@ -56,7 +56,6 @@ struct inode_security_struct {struct file_security_struct {u32 sid;/* SID of open file description */ -u32 fown_sid;/* SID of file owner (for SIGIO) */ u32 isid;/* SID of inode at the time of file open */ u32 pseqno;/* Policy seqno at the time of file open */ };
security/smack/smack.h +0 -0
@@ -328,12 +328,6 @@ static inline struct task_smack *smack_cred(const
struct cred *cred)
return cred->security + smack_blob_sizes.lbs_cred;
}
-static inline struct smack_known **smack_file(const struct file *file) -{
- return (struct smack_known **)(file->f_security +
- smack_blob_sizes.lbs_file);
-}
-
static inline struct inode_smack *smack_inode(const struct inode *inode)
{
return inode->i_security + smack_blob_sizes.lbs_inode;security/smack/smack_lsm.c +0 -0
@@ -1675,26 +1675,6 @@ static void smack_inode_getsecid(struct inode
*inode, u32 *secid)
* label changing that SELinux does.
*/
-/**
- * smack_file_alloc_security - assign a file security blob
- * @file: the object
- *
- * The security blob for a file is a pointer to the master
- * label list, so no allocation is done.
- *
- * f_security is the owner security information. It
- * isn't used on file access checks, it's for send_sigio.
- *
- * Returns 0
- */
-static int smack_file_alloc_security(struct file *file) -{
- struct smack_known **blob = smack_file(file);
-
- *blob = smk_of_current();
- return 0;
-}
-
/**
* smack_file_ioctl - Smack check on ioctls
* @file: the object
@@ -1913,18 +1893,6 @@ static int smack_mmap_file(struct file *file,
return rc;
}
-/**
- * smack_file_set_fowner - set the file security blob value
- * @file: object in question
- *
- */
-static void smack_file_set_fowner(struct file *file) -{
- struct smack_known **blob = smack_file(file);
-
- *blob = smk_of_current();
-}
-
/**
* smack_file_send_sigiotask - Smack on sigio
* @tsk: The target task
@@ -1946,6 +1914,7 @@ static int smack_file_send_sigiotask(struct task_struct *tsk,
struct file *file;
int rc;
struct smk_audit_info ad;+ struct cred_struct *fown_cred = rcu_dereference(fown->owner_cred);
/*
* struct fown_struct is never outside the context of a struct file
@@ -1953,8 +1922,7 @@ static int smack_file_send_sigiotask(struct task_struct *tsk,
file = container_of(fown, struct file, f_owner);
/* we don't log here as rc can be overriden */
- blob = smack_file(file);
- skp = *blob;
+ skp = smk_of_task(fown_cred ?: file->f_cred);
rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);@@ -5045,7 +5013,6 @@ static int smack_uring_cmd(struct io_uring_cmd *ioucmd)
struct lsm_blob_sizes smack_blob_sizes __ro_after_init = {
.lbs_cred = sizeof(struct task_smack),
- .lbs_file = sizeof(struct smack_known *),
.lbs_inode = sizeof(struct inode_smack),
.lbs_ipc = sizeof(struct smack_known *), .lbs_msg_msg = sizeof(struct smack_known *),
@@ -5104,7 +5071,6 @@ static struct security_hook_list smack_hooks[] __ro_after_init = {
LSM_HOOK_INIT(file_fcntl, smack_file_fcntl), LSM_HOOK_INIT(mmap_file, smack_mmap_file), LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
- LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask), LSM_HOOK_INIT(file_receive, smack_file_receive),
Quoted 4 lines
While I think it is okay if we want to
consider relocating thesecurity_file_set_fowner()within theF_SETOWNcall path, I don't think we can remove it, even if we add additional LSM security blobs.
Mickaël Salaün
maintainerPATCH 1/1 fs,security: Fix file_set_fowner LSM hook inconsistencies
In reply to Paul MooreThe fcntl's F_SETOWN command sets the process that handle SIGIO/SIGURG for the related file descriptor. Before this change, the file_set_fowner LSM hook was used to store this information. However, there are three issues with this approach:
- Because security_file_set_fowner() only get one argument, all hook implementations ignore the VFS logic which may not actually change the process that handles SIGIO (e.g. TUN, TTY, dnotify).
- Because security_file_set_fowner() is called before f_modown() without lock (e.g. f_owner.lock), concurrent F_SETOWN commands could result to a race condition and inconsistent LSM states (e.g. SELinux's fown_sid) compared to struct fown_struct's UID/EUID.
- Because the current hook implementations does not use explicit atomic operations, they may create inconsistencies. It would help to completely remove this constraint, as well as the requirements of the RCU read-side critical section for the hook.
Fix these issues by replacing f_owner.uid and f_owner.euid with a new f_owner.cred [1]. This also saves memory by removing dedicated LSM blobs, and simplifies code by removing the file_set_fowner LSM hook.
This changes enables to remove the smack_file_alloc_security implementation, Smack's file blob, and SELinux's file_security_struct->fown_sid field.
As for the UID/EUID, f_owner.cred is not always updated. Because the file_set_fowner hook is removed, the fowner credentials now have the same semantic as what is used by the VFS.
Before this change, f_owner's UID/EUID were initialized to zero (i.e. GLOBAL_ROOT_UID), but to simplify code, f_owner's cred is now initialized with the file descriptor creator's credentials (i.e. file->f_cred), which is more consistent and simplifies LSMs logic. The sigio_perm()'s semantic does not need any change because SIGIO/SIGURG are only sent when a process is explicitly set with __f_setown().
Rename f_modown() to __f_setown() to simplify code.
--- fs/fcntl.c | 44 +++++++++++++++---------------- fs/file_table.c | 3 +++ include/linux/fs.h | 2 +- include/linux/lsm_hook_defs.h | 1 - include/linux/security.h | 1 - security/security.c | 14 ---------- security/selinux/hooks.c | 22 +++------------- security/selinux/include/objsec.h | 1 - security/smack/smack.h | 6 ----- security/smack/smack_lsm.c | 39 +-------------------------- 10 files changed, 29 insertions(+), 104 deletions(-)
fs/fcntl.c +21 -23
@@ -87,8 +87,8 @@ static int setfl(int fd, struct file * filp, unsigned int arg)return error; } -static void f_modown (struct file *filp,struct pid *pid,enum pid_type type, -int force) +void __f_setown (struct file *filp,struct pid *pid,enum pid_type type, +int force) {write_lock_irq (&filp->f_owner.lock);if (force || !filp->f_owner.pid) {
@@ -96,21 +96,14 @@ static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, filp->f_owner.pid =get_pid (pid); filp->f_owner.pid_type = type; -if (pid) { -const struct cred *cred =current_cred (); - filp->f_owner.uid = cred->uid; - filp->f_owner.euid = cred->euid; - } +if (pid) +put_cred (rcu_replace_pointer ( + filp->f_owner.cred, +get_cred_rcu (current_cred ()), +lockdep_is_held (&filp->f_owner.lock))); }write_unlock_irq (&filp->f_owner.lock); } - -void __f_setown (struct file *filp,struct pid *pid,enum pid_type type, -int force) -{ -security_file_set_fowner (filp); -f_modown (filp, pid, type, force); -}EXPORT_SYMBOL (__f_setown);int f_setown (struct file *filp,int who,int force)
@@ -146,7 +139,7 @@ EXPORT_SYMBOL(f_setown);void f_delown (struct file *filp) { -f_modown (filp,NULL ,PIDTYPE_TGID ,1 ); +__f_setown (filp,NULL ,PIDTYPE_TGID ,1 ); }pid_t f_getown (struct file *filp)
@@ -249,13 +242,15 @@ static int f_getowner_uids(struct file *filp, unsigned long arg) {struct user_namespace *user_ns =current_user_ns ();uid_t __user *dst = (void __user *)arg; +const struct cred *fown_cred;uid_t src[2 ];int err; -read_lock_irq (&filp->f_owner.lock); - src[0 ] =from_kuid (user_ns, filp->f_owner.uid); - src[1 ] =from_kuid (user_ns, filp->f_owner.euid); -read_unlock_irq (&filp->f_owner.lock); +rcu_read_lock (); + fown_cred =rcu_dereference (filp->f_owner->cred); + src[0 ] =from_kuid (user_ns, fown_cred->uid); + src[1 ] =from_kuid (user_ns, fown_cred->euid); +rcu_read_unlock (); err =put_user (src[0 ], &dst[0 ]); err |=put_user (src[1 ], &dst[1 ]);
@@ -737,14 +732,17 @@ static const __poll_t band_table[NSIGPOLL] = {static inline int sigio_perm (struct task_struct *p,struct fown_struct *fown,int sig) { -const struct cred *cred; +const struct cred *cred, *fown_cred;int ret;rcu_read_lock (); + fown_cred =rcu_dereference (fown->cred); cred =__task_cred (p); - ret = ((uid_eq (fown->euid,GLOBAL_ROOT_UID ) || -uid_eq (fown->euid, cred->suid) ||uid_eq (fown->euid, cred->uid) || -uid_eq (fown->uid, cred->suid) ||uid_eq (fown->uid, cred->uid)) && + ret = ((uid_eq (fown_cred->euid,GLOBAL_ROOT_UID ) || +uid_eq (fown_cred->euid, cred->suid) || +uid_eq (fown_cred->euid, cred->uid) || +uid_eq (fown_cred->uid, cred->suid) || +uid_eq (fown_cred->uid, cred->uid)) && !security_file_send_sigiotask (p, fown, sig));rcu_read_unlock ();return ret;
fs/file_table.c +3 -0
@@ -66,6 +66,7 @@ static inline void file_free(struct file *f)if (likely (!(f->f_mode &FMODE_NOACCOUNT )))percpu_counter_dec (&nr_files);put_cred (f->f_cred); +put_cred (f->f_owner.cred);if (unlikely (f->f_mode &FMODE_BACKING )) {path_put (backing_file_user_path (f));kfree (backing_file (f));
@@ -149,9 +150,11 @@ static int init_file(struct file *f, int flags, const struct cred *cred)int error; f->f_cred =get_cred (cred); + f->f_owner.cred =get_cred (cred); error =security_file_alloc (f);if (unlikely (error)) {put_cred (f->f_cred); +put_cred (f->f_owner.cred);return error; }
include/linux/fs.h +1 -1
@@ -942,7 +942,7 @@ struct fown_struct {rwlock_t lock;/* protects pid, uid, euid fields */ struct pid *pid;/* pid or -pgrp where SIGIO should be sent */ enum pid_type pid_type;/* Kind of process group SIGIO should be sent to */ -kuid_t uid, euid;/* uid/euid of process setting the owner */ +const struct cred __rcu *cred;/* cred of process setting the owner */ int signum;/* posix.1b rt signal to be delivered on IO */ };
include/linux/lsm_hook_defs.h +0 -1
@@ -196,7 +196,6 @@ LSM_HOOK(int, 0, file_mprotect, struct vm_area_struct *vma, LSM_HOOK(int, 0, file_lock, struct file *file, unsigned int cmd) LSM_HOOK(int, 0, file_fcntl, struct file *file, unsigned int cmd, unsigned long arg) -LSM_HOOK(void, LSM_RET_VOID, file_set_fowner, struct file *file) LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk, struct fown_struct *fown, int sig) LSM_HOOK(int, 0, file_receive, struct file *file)
include/linux/security.h +0 -1
@@ -415,7 +415,6 @@ int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot);int security_file_lock (struct file *file,unsigned int cmd);int security_file_fcntl (struct file *file,unsigned int cmd,unsigned long arg); -void security_file_set_fowner (struct file *file);int security_file_send_sigiotask (struct task_struct *tsk,struct fown_struct *fown,int sig);int security_file_receive (struct file *file);
security/security.c +0 -14
@@ -2898,20 +2898,6 @@ int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)return call_int_hook (file_fcntl, file, cmd, arg); } -/** -* security_file_set_fowner() - Set the file owner info in the LSM blob -* @file: the file -* -* Save owner security information (typically from current->security) in -* file->f_security for later use by the send_sigiotask hook. -* -* Return: Returns 0 on success. -*/ -void security_file_set_fowner (struct file *file) -{ -call_void_hook (file_set_fowner, file); -} -/** * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed * @tsk: target task
security/selinux/hooks.c +3 -19
@@ -3644,8 +3644,6 @@ static int selinux_file_alloc_security(struct file *file)u32 sid =current_sid (); fsec->sid = sid; - fsec->fown_sid = sid; -return 0 ; }
@@ -3918,33 +3916,20 @@ static int selinux_file_fcntl(struct file *file, unsigned int cmd,return err; } -static void selinux_file_set_fowner (struct file *file) -{ -struct file_security_struct *fsec; - - fsec =selinux_file (file); - fsec->fown_sid =current_sid (); -} -static int selinux_file_send_sigiotask (struct task_struct *tsk,struct fown_struct *fown,int signum) { -struct file *file;u32 sid =task_sid_obj (tsk);u32 perm; -struct file_security_struct *fsec; - -/* struct fown_struct is never outside the context of a struct file */ - file = container_of(fown, struct file, f_owner); - - fsec =selinux_file (file); +const struct task_security_struct *tsec = +selinux_cred (rcu_dereference (fown->cred));if (!signum) perm =signal_to_av (SIGIO );/* as per send_sigio_to_task */ else perm =signal_to_av (signum); -return avc_has_perm (fsec->fown_sid, sid, +return avc_has_perm (tsec->sid, sid,SECCLASS_PROCESS , perm,NULL ); }
@@ -7202,7 +7187,6 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (file_mprotect, selinux_file_mprotect),LSM_HOOK_INIT (file_lock, selinux_file_lock),LSM_HOOK_INIT (file_fcntl, selinux_file_fcntl), -LSM_HOOK_INIT (file_set_fowner, selinux_file_set_fowner),LSM_HOOK_INIT (file_send_sigiotask, selinux_file_send_sigiotask), LSM_HOOK_INIT(file_receive, selinux_file_receive),
security/selinux/include/objsec.h +0 -1
@@ -56,7 +56,6 @@ struct inode_security_struct {struct file_security_struct {u32 sid;/* SID of open file description */ -u32 fown_sid;/* SID of file owner (for SIGIO) */ u32 isid;/* SID of inode at the time of file open */ u32 pseqno;/* Policy seqno at the time of file open */ };
security/smack/smack.h +0 -6
@@ -328,12 +328,6 @@ static inline struct task_smack *smack_cred(const struct cred *cred)return cred->security + smack_blob_sizes.lbs_cred; } -static inline struct smack_known **smack_file (const struct file *file) -{ -return (struct smack_known **)(file->f_security + - smack_blob_sizes.lbs_file); -} -static inline struct inode_smack *smack_inode (const struct inode *inode) {return inode->i_security + smack_blob_sizes.lbs_inode;
security/smack/smack_lsm.c +1 -38
@@ -1650,26 +1650,6 @@ static void smack_inode_getsecid(struct inode *inode, u32 *secid)* label changing that SELinux does. */ -/** -* smack_file_alloc_security - assign a file security blob -* @file: the object -* -* The security blob for a file is a pointer to the master -* label list, so no allocation is done. -* -* f_security is the owner security information. It -* isn't used on file access checks, it's for send_sigio. -* -* Returns 0 -*/ -static int smack_file_alloc_security (struct file *file) -{ -struct smack_known **blob =smack_file (file); - - *blob =smk_of_current (); -return 0 ; -} -/** * smack_file_ioctl - Smack check on ioctls * @file: the object
@@ -1888,18 +1868,6 @@ static int smack_mmap_file(struct file *file,return rc; } -/** -* smack_file_set_fowner - set the file security blob value -* @file: object in question -* -*/ -static void smack_file_set_fowner (struct file *file) -{ -struct smack_known **blob =smack_file (file); - - *blob =smk_of_current (); -} -/** * smack_file_send_sigiotask - Smack on sigio * @tsk: The target task
@@ -1914,7 +1882,6 @@ static void smack_file_set_fowner(struct file *file)static int smack_file_send_sigiotask (struct task_struct *tsk,struct fown_struct *fown,int signum) { -struct smack_known **blob;struct smack_known *skp;struct smack_known *tkp =smk_of_task (smack_cred (tsk->cred));const struct cred *tcred;
@@ -1928,8 +1895,7 @@ static int smack_file_send_sigiotask(struct task_struct *tsk, file = container_of(fown, struct file, f_owner);/* we don't log here as rc can be overriden */ - blob =smack_file (file); - skp = *blob; + skp =smk_of_task (smack_cred (rcu_dereference (fown->cred))); rc =smk_access (skp, tkp,MAY_DELIVER ,NULL ); rc =smk_bu_note ("sigiotask" , skp, tkp,MAY_DELIVER , rc);
@@ -5014,7 +4980,6 @@ static int smack_uring_cmd(struct io_uring_cmd *ioucmd)struct lsm_blob_sizes smack_blob_sizes__ro_after_init = { .lbs_cred =sizeof (struct task_smack ), - .lbs_file =sizeof (struct smack_known *), .lbs_inode =sizeof (struct inode_smack ), .lbs_ipc =sizeof (struct smack_known *), .lbs_msg_msg =sizeof (struct smack_known *),
@@ -5065,14 +5030,12 @@ static struct security_hook_list smack_hooks[] __ro_after_init = {LSM_HOOK_INIT (inode_listsecurity, smack_inode_listsecurity),LSM_HOOK_INIT (inode_getsecid, smack_inode_getsecid), -LSM_HOOK_INIT (file_alloc_security, smack_file_alloc_security),LSM_HOOK_INIT (file_ioctl, smack_file_ioctl),LSM_HOOK_INIT (file_ioctl_compat, smack_file_ioctl),LSM_HOOK_INIT (file_lock, smack_file_lock),LSM_HOOK_INIT (file_fcntl, smack_file_fcntl),LSM_HOOK_INIT (mmap_file, smack_mmap_file),LSM_HOOK_INIT (mmap_addr, cap_mmap_addr), -LSM_HOOK_INIT (file_set_fowner, smack_file_set_fowner),LSM_HOOK_INIT (file_send_sigiotask, smack_file_send_sigiotask), LSM_HOOK_INIT(file_receive, smack_file_receive),
Signature · 1 line
2.45.2
Mickaël Salaün
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
In reply to Jann HornQuoted 55 lines · Jann Horn
Quoted 46 lines · Paul Moore
Quoted 38 lines · Jann Horn
Quoted 32 lines · Mickaël Salaün
Talking about
f_modown()andsecurity_file_set_fowner(), it looks like there are some issues:Quoted 1 line · Jann Horn
On Fri, Aug 9, 2024 at 12:59 PM Mickaël Salaün <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 2 lines
BTW, I don't understand why neither SELinux nor Smack use (explicit) atomic operations nor lock.
Yeah, I think they're sloppy and kinda wrong - but it sorta works in practice mostly because they don't have to do any refcounting around this?
Quoted 3 lines
And it looks weird that
security_file_set_fowner()isn't called byf_modown()with the same locking to avoid races.Quoted 6 lines
True. I imagine maybe the thought behind this design could have been that LSMs should have their own locking, and that calling an LSM hook with IRQs off is a little weird? But the way the LSMs actually use the hook now, it might make sense to call the LSM with the lock held and IRQs off...
Would it be OK (for VFS, SELinux, and Smack maintainers) to move the
security_file_set_fowner()call intof_modown(), especially where UID/EUID are populated. That would only callsecurity_file_set_fowner()when the fown is actually set, which I think could also fix a bug for SELinux and Smack.Quoted 4 lines
Could we replace the uid and euid fields with a pointer to the current credentials? This would enables LSMs to not copy the same kind of credential informations and save some memory, simplify credential management, and improve consistency.
Quoted 5 lines
To clarify: These two paragraphs are supposed to be two alternative options, right? One option is to call
security_file_set_fowner()with the lock held, the other option is to completely rip out thesecurity_file_set_fowner()hook and instead let the VFS provide LSMs with the creds they need for thefile_send_sigiotaskhook?Quoted 7 lines
I'm not entirely clear on what is being proposed either. Some quick pseudo code might do wonders here to help clarify things.
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.
Quoted 4 lines
The only thing LSMs currently do there is capture state from
current->cred. So if the VFS takes care of capturingcurrent->credthere, we should be able to rip out all thefile_set_fownerstuff. Something like this (totally untested):
I just sent a quite similar patch just before syncing my emails... The main difference seems to be related to the initialization of the f_owner's credentials.
Quoted 288 lines
fs/fcntl.c +3
-2@@ -98,8 +98,9 @@ static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, if (pid) {const struct cred *cred =current_cred (); - filp->f_owner.uid = cred->uid; - filp->f_owner.euid = cred->euid; +if (filp->f_owner.owner_cred) +put_cred (filp->f_owner.owner_cred); + filp->f_owner.owner_cred =get_current_cred (); } }write_unlock_irq(&filp->f_owner.lock);fs/fcntl.c +0
-1@@ -108,7 +109,6 @@ static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, void __f_setown(struct file *filp, struct pid *pid, enum pid_type type, int force) { -security_file_set_fowner (filp );f_modown (filp ,pid ,type ,force ); }
EXPORT_SYMBOL(__f_setown);fs/file_table.c +2
-0@@ -426,6 +426,8 @@ static void __fput(struct file *file) }fops_put (file->f_op);put_pid (file->f_owner.pid); +if (file->f_owner.owner_cred) +put_cred (file->f_owner.owner_cred);put_file_access (file);dput (dentry);if (unlikely (mode &FMODE_NEED_UNMOUNT ))include/linux/fs.h +0
-0@@ -950,7 +950,7 @@ struct fown_struct {rwlock_t lock;/* protects pid, uid, euid fields */ struct pid *pid;/* pid or -pgrp where SIGIO should be sent */ enum pid_type pid_type;/* Kind of process group SIGIO should be sent to */
include/linux/fs.h +1
-1-kuid_t uid, euid;/* uid/euid of process setting the owner */ +const struct cred __rcu *owner_cred;int signum;/* posix.1b rt signal to be delivered on IO */
};include/linux/lsm_hook_defs.h +0
-1@@ -197,7 +197,6 @@ LSM_HOOK(int, 0, file_mprotect, struct vm_area_struct *vma, LSM_HOOK(int, 0, file_lock, struct file *file, unsigned int cmd) LSM_HOOK(int, 0, file_fcntl, struct file *file, unsigned int cmd, unsigned long arg) -LSM_HOOK(void, LSM_RET_VOID, file_set_fowner, struct file *file) LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk, struct fown_struct *fown, int sig) LSM_HOOK(int, 0, file_receive, struct file *file)include/linux/security.h +0
-5@@ -1079,11 +1079,6 @@ static inline int security_file_fcntl(struct file *file, unsigned int cmd, return 0; } -static inline void security_file_set_fowner (struct file *file) -{ -return ; -} - static inline int security_file_send_sigiotask(struct task_struct *tsk, struct fown_struct *fown,int sig)
security/security.c +0
-14@@ -2924,20 +2924,6 @@ int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg) return call_int_hook(file_fcntl, file, cmd, arg); } -/** -* security_file_set_fowner() - Set the file owner info in the LSM blob -* @file: the file -* -* Save owner security information (typically from current->security) in -* file->f_security for later use by the send_sigiotask hook. -* -* Return: Returns 0 on success. -*/ -void security_file_set_fowner (struct file *file) -{ -call_void_hook (file_set_fowner, file); -} -/** * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed * @tsk: target task
security/selinux/hooks.c +5
-14@@ -3649,7 +3649,6 @@ static int selinux_file_alloc_security(struct file *file)u32 sid =current_sid (); fsec->sid = sid; - fsec->fown_sid = sid;return 0 ; }@@ -3923,24 +3922,16 @@ static int selinux_file_fcntl(struct file *file, unsigned int cmd, return err; } -static void selinux_file_set_fowner (struct file *file) -{ -struct file_security_struct *fsec; - - fsec =selinux_file (file); - fsec->fown_sid =current_sid (); -} -static int selinux_file_send_sigiotask (struct task_struct *tsk,struct fown_struct *fown,int signum) { -struct file *file; +/* struct fown_struct is never outside the context of a struct file */ + struct file *file = container_of(fown, struct file, f_owner);u32 sid =task_sid_obj (tsk);u32 perm;struct file_security_struct *fsec; - -/* struct fown_struct is never outside the context of a struct file */ - file = container_of(fown, struct file, f_owner); +struct cred_struct *fown_cred =rcu_dereference (fown->owner_cred); +u32 fown_sid =cred_sid (fown_cred ?: file->f_cred); fsec =selinux_file (file);@@ -3949,7 +3940,7 @@ static int selinux_file_send_sigiotask(structtask_struct *tsk, else perm =signal_to_av (signum); -return avc_has_perm (fsec->fown_sid, sid, +return avc_has_perm (fown_sid, sid,SECCLASS_PROCESS , perm,NULL ); }security/selinux/include/objsec.h +0
-1@@ -56,7 +56,6 @@ struct inode_security_struct {struct file_security_struct {u32 sid;/* SID of open file description */ -u32 fown_sid;/* SID of file owner (for SIGIO) */ u32 isid;/* SID of inode at the time of file open */ u32 pseqno;/* Policy seqno at the time of file open */ };security/smack/smack.h +0
-6@@ -328,12 +328,6 @@ static inline struct task_smack *smack_cred(const struct cred *cred)return cred->security + smack_blob_sizes.lbs_cred; } -static inline struct smack_known **smack_file (const struct file *file) -{ -return (struct smack_known **)(file->f_security + - smack_blob_sizes.lbs_file); -} -static inline struct inode_smack *smack_inode (const struct inode *inode) {return inode->i_security + smack_blob_sizes.lbs_inode;security/smack/smack_lsm.c +0
-20@@ -1675,26 +1675,6 @@ static void smack_inode_getsecid(struct inode*inode, u32 *secid) * label changing that SELinux does. */ -/** -* smack_file_alloc_security - assign a file security blob -* @file: the object -* -* The security blob for a file is a pointer to the master -* label list, so no allocation is done. -* -* f_security is the owner security information. It -* isn't used on file access checks, it's for send_sigio. -* -* Returns 0 -*/ -static int smack_file_alloc_security (struct file *file) -{ -struct smack_known **blob =smack_file (file); - - *blob =smk_of_current (); -return 0 ; -} -/** * smack_file_ioctl - Smack check on ioctls * @file: the object
security/smack/smack_lsm.c +1
-12@@ -1913,18 +1893,6 @@ static int smack_mmap_file(struct file *file,return rc; } -/** -* smack_file_set_fowner - set the file security blob value -* @file: object in question -* -*/ -static void smack_file_set_fowner (struct file *file) -{ -struct smack_known **blob =smack_file (file); - - *blob =smk_of_current (); -} -/** * smack_file_send_sigiotask - Smack on sigio * @tsk: The target task @@ -1946,6 +1914,7 @@ static int smack_file_send_sigiotask(structtask_struct *tsk, struct file *file;int rc;struct smk_audit_info ad; +struct cred_struct *fown_cred =rcu_dereference (fown->owner_cred);/* * struct
fown_structis never outside the context of a struct filesecurity/smack/smack_lsm.c +1
-4@@ -1953,8 +1922,7 @@ static int smack_file_send_sigiotask(structtask_struct *tsk, file = container_of(fown, struct file, f_owner);/* we don't log here as rc can be overriden */ - blob =smack_file (file); - skp = *blob; + skp =smk_of_task (fown_cred ?: file->f_cred); rc =smk_access (skp, tkp,MAY_DELIVER ,NULL ); rc =smk_bu_note ("sigiotask" , skp, tkp,MAY_DELIVER , rc);@@ -5045,7 +5013,6 @@ static int smack_uring_cmd(struct io_uring_cmd *ioucmd)struct lsm_blob_sizes smack_blob_sizes__ro_after_init = { .lbs_cred =sizeof (struct task_smack ), - .lbs_file =sizeof (struct smack_known *), .lbs_inode =sizeof (struct inode_smack ), .lbs_ipc =sizeof (struct smack_known *), .lbs_msg_msg =sizeof (struct smack_known *),@@ -5104,7 +5071,6 @@ static struct security_hook_list smack_hooks[] __ro_after_init = {LSM_HOOK_INIT (file_fcntl, smack_file_fcntl),LSM_HOOK_INIT (mmap_file, smack_mmap_file),LSM_HOOK_INIT (mmap_addr, cap_mmap_addr), -LSM_HOOK_INIT (file_set_fowner, smack_file_set_fowner),LSM_HOOK_INIT (file_send_sigiotask, smack_file_send_sigiotask), LSM_HOOK_INIT(file_receive, smack_file_receive),Quoted 6 lines
While I think it is okay if we want to
consider relocating thesecurity_file_set_fowner()within theF_SETOWNcall path, I don't think we can remove it, even if we add additional LSM security blobs.
Paul Moore
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
In reply to Jann HornQuoted 1 line · Jann Horn
On Mon, Aug 12, 2024 at 12:04 AM Paul Moore <[email protected]> wrote:
...
Quoted 9 lines
Quoted 4 lines
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.The only thing LSMs currently do there is capture state from
current->cred. So if the VFS takes care of capturingcurrent->credthere, we should be able to rip out all thefile_set_fownerstuff. Something like this (totally untested):
I've very hesitant to drop the LSM hook from the F_SETOWN path both because it is reasonable that other LSMs may want to do other things here, and adding a LSM hook to the kernel, even if it is re-adding a hook that was previously removed, is a difficult and painful process with an uncertain outcome.
Signature · 1 line
paul-moore.com
Paul Moore
maintainerRe: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies
In reply to Mickaël Salaüninclude/linux/lsm_hook_defs.h LSM_HOOK(int, 0, file_mprotect, struct vm_area_struct *vma, · line 199
Quoted 9 lines · Mickaël Salaün
include/linux/lsm_hook_defs.h +0
-1@@ -196,7 +196,6 @@ LSM_HOOK(int, 0, file_mprotect, struct vm_area_struct *vma, LSM_HOOK(int, 0, file_lock, struct file *file, unsigned int cmd) LSM_HOOK(int, 0, file_fcntl, struct file *file, unsigned int cmd, unsigned long arg) -LSM_HOOK(void, LSM_RET_VOID, file_set_fowner, struct file *file)
As I mentioned in the other thread, I don't want to see the file_set_owner hook removed at this point in time. I'm open to the idea of moving it around, but as of right now I think it is important to keep it around.
Quoted 3 lines
include/linux/lsm_hook_defs.h +0
-0LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk, struct fown_struct *fown, int sig) LSM_HOOK(int, 0, file_receive, struct file *file)
Signature · 1 line
paul-moore.com
Re: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
In reply to Paul MooreQuoted 10 lines · Paul Moore
Quoted 1 line · Jann Horn
On Mon, Aug 12, 2024 at 12:04 AM Paul Moore <[email protected]> wrote:
...
Quoted 5 lines
Quoted 4 lines
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.
Quoted 8 lines
Quoted 4 lines
The only thing LSMs currently do there is capture state from
current->cred. So if the VFS takes care of capturingcurrent->credthere, we should be able to rip out all thefile_set_fownerstuff. Something like this (totally untested):I've very hesitant to drop the LSM hook from the
F_SETOWNpath both because it is reasonable that other LSMs may want to do other things here,
What is an example for other things an LSM might want to do there? As far as I understand, the whole point of this hook is to record the identity of the sender of signals - are you talking about an LSM that might not be storing credentials in struct cred, or something like that?
Quoted 3 lines
and adding a LSM hook to the kernel, even if it is re-adding a hook that was previously removed, is a difficult and painful process with an uncertain outcome.
Do you mean that even if the LSM hook ends up with zero users remaining, you'd still want to keep it around in case it's needed again later?
Paul Moore
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
Quoted 20 lines · Jann Horn
Quoted 10 lines · Paul Moore
Quoted 1 line · Jann Horn
On Mon, Aug 12, 2024 at 12:04 AM Paul Moore <[email protected]> wrote:
...
Quoted 5 lines
Quoted 4 lines
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.Quoted 8 lines
Quoted 4 lines
The only thing LSMs currently do there is capture state from
current->cred. So if the VFS takes care of capturingcurrent->credthere, we should be able to rip out all thefile_set_fownerstuff. Something like this (totally untested):I've very hesitant to drop the LSM hook from the
F_SETOWNpath both because it is reasonable that other LSMs may want to do other things here,
Quoted 5 lines
What is an example for other things an LSM might want to do there? As far as I understand, the whole point of this hook is to record the identity of the sender of signals - are you talking about an LSM that might not be storing credentials in struct cred, or something like that?
Sure. The LSM framework is intentionally very vague and limited on what restrictions it places on individual LSMs; we want to be able to support a wide range of security models and concepts. I view the F_SETOWN hook are important because it is a control point that is used to set/copy/transfer/whatever security attributes from the current process to a file/fd for the purpose of managing signals on the fd.
Quoted 7 lines
Quoted 3 lines
and adding a LSM hook to the kernel, even if it is re-adding a hook that was previously removed, is a difficult and painful process with an uncertain outcome.
Do you mean that even if the LSM hook ends up with zero users remaining, you'd still want to keep it around in case it's needed again later?
I want the security_file_set_fowner() hook to remain a viable hook for capturing the current task's security attributes, regardless of what security attributes the LSM is interested in capturing and where those attributes are stored.
Signature · 1 line
paul-moore.com
Mickaël Salaün
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
Quoted 38 lines · Paul Moore
Quoted 20 lines · Jann Horn
Quoted 10 lines · Paul Moore
Quoted 1 line · Jann Horn
On Mon, Aug 12, 2024 at 12:04 AM Paul Moore <[email protected]> wrote:
...
Quoted 5 lines
Quoted 4 lines
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.Quoted 8 lines
Quoted 4 lines
The only thing LSMs currently do there is capture state from
current->cred. So if the VFS takes care of capturingcurrent->credthere, we should be able to rip out all thefile_set_fownerstuff. Something like this (totally untested):I've very hesitant to drop the LSM hook from the
F_SETOWNpath both because it is reasonable that other LSMs may want to do other things here,Quoted 5 lines
What is an example for other things an LSM might want to do there? As far as I understand, the whole point of this hook is to record the identity of the sender of signals - are you talking about an LSM that might not be storing credentials in struct cred, or something like that?
Sure. The LSM framework is intentionally very vague and limited on what restrictions it places on individual LSMs; we want to be able to support a wide range of security models and concepts. I view the
F_SETOWNhook are important because it is a control point that is used to set/copy/transfer/whatever security attributes from the current process to a file/fd for the purpose of managing signals on the fd.Quoted 4 lines
Quoted 3 lines
and adding a LSM hook to the kernel, even if it is re-adding a hook that was previously removed, is a difficult and painful process with an uncertain outcome.
Quoted 8 lines
Quoted 3 lines
Do you mean that even if the LSM hook ends up with zero users remaining, you'd still want to keep it around in case it's needed again later?
I want the
security_file_set_fowner()hook to remain a viable hook for capturing the current task's security attributes, regardless of what security attributes the LSM is interested in capturing and where those attributes are stored.
I don't see the point to keep an unused hook, we could add it back later if there is a valid use case, but I'll send a v2 without this removal.
Paul Moore
maintainerRe: f_modown and LSM inconsistency (was [PATCH v2 1/4] Landlock: Add signal control)
Quoted 43 lines · Mickaël Salaün
Quoted 42 lines · Paul Moore
Quoted 20 lines · Jann Horn
Quoted 10 lines · Paul Moore
Quoted 1 line · Jann Horn
On Mon, Aug 12, 2024 at 12:04 AM Paul Moore <[email protected]> wrote:
...
Quoted 5 lines
Quoted 4 lines
From a LSM perspective I suspect we are always going to need some sort of hook in the
F_SETOWNcode path as the LSM needs to potentially capture state/attributes/something-LSM-specific at that context/point-in-time.Quoted 8 lines
Quoted 4 lines
The only thing LSMs currently do there is capture state from
current->cred. So if the VFS takes care of capturingcurrent->credthere, we should be able to rip out all thefile_set_fownerstuff. Something like this (totally untested):I've very hesitant to drop the LSM hook from the
F_SETOWNpath both because it is reasonable that other LSMs may want to do other things here,Quoted 5 lines
What is an example for other things an LSM might want to do there? As far as I understand, the whole point of this hook is to record the identity of the sender of signals - are you talking about an LSM that might not be storing credentials in struct cred, or something like that?
Sure. The LSM framework is intentionally very vague and limited on what restrictions it places on individual LSMs; we want to be able to support a wide range of security models and concepts. I view the
F_SETOWNhook are important because it is a control point that is used to set/copy/transfer/whatever security attributes from the current process to a file/fd for the purpose of managing signals on the fd.Quoted 7 lines
Quoted 3 lines
and adding a LSM hook to the kernel, even if it is re-adding a hook that was previously removed, is a difficult and painful process with an uncertain outcome.
Do you mean that even if the LSM hook ends up with zero users remaining, you'd still want to keep it around in case it's needed again later?
Quoted 7 lines
Quoted 4 lines
I want the
security_file_set_fowner()hook to remain a viable hook for capturing the current task's security attributes, regardless of what security attributes the LSM is interested in capturing and where those attributes are stored.I don't see the point to keep an unused hook, we could add it back later if there is a valid use case, but I'll send a v2 without this removal.
If it was simple to add LSM hooks, then I would agree, but history has shown that not to be the case.
Signature · 1 line
paul-moore.com
kernel test robot
build botRe: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies
In reply to Mickaël Salaünkernel test robot noticed the following build errors:
Hi Mickaël,
kernel test robot noticed the following build errors:
[auto build test ERROR on pcmoore-selinux/next]
[also build test ERROR on linus/master v6.11-rc3 next-20240812] [cannot apply to brauner-vfs/vfs.all]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Micka-l-Sala-n/fs-security-Fix-file_set_fowner-LSM-hook-inconsistencies/20240813-004648 base: https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git next patch link: https://lore.kernel.org/r/20240812144936.1616628-1-mic%40digikod.net patch subject: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies config: i386-buildonly-randconfig-002-20240813 (https://download.01.org/0day-ci/archive/20240813/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240813/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/linux/rculist.h:11,
from include/linux/dcache.h:8,
from include/linux/fs.h:8,
from include/uapi/linux/aio_abi.h:31,
from include/linux/syscalls.h:82,
from fs/fcntl.c:8:
fs/fcntl.c: In function 'f_getowner_uids':
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/rcupdate.h:527:17: note: in definition of macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/rcupdate.h:527:38: note: in definition of macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
In file included from <command-line>:
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/compiler_types.h:490:23: note: in definition of macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
include/linux/compiler_types.h:510:9: note: in expansion of macro '_compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:28: note: in expansion of macro '__native_word'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~
include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
49 | compiletime_assert_rwonce_type(x); \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/compiler_types.h:490:23: note: in definition of macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
include/linux/compiler_types.h:510:9: note: in expansion of macro '_compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:28: note: in expansion of macro '__native_word'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~
include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
49 | compiletime_assert_rwonce_type(x); \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/compiler_types.h:490:23: note: in definition of macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
include/linux/compiler_types.h:510:9: note: in expansion of macro '_compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:28: note: in expansion of macro '__native_word'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~
include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
49 | compiletime_assert_rwonce_type(x); \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/compiler_types.h:490:23: note: in definition of macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
include/linux/compiler_types.h:510:9: note: in expansion of macro '_compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:28: note: in expansion of macro '__native_word'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~
include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
49 | compiletime_assert_rwonce_type(x); \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/compiler_types.h:490:23: note: in definition of macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
include/linux/compiler_types.h:510:9: note: in expansion of macro '_compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
36 | compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
| ^~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
49 | compiletime_assert_rwonce_type(x); \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/compiler_types.h:466:27: note: in definition of macro '__unqual_scalar_typeof'
466 | _Generic((x), \
| ^
include/asm-generic/rwonce.h:50:9: note: in expansion of macro '__READ_ONCE'
50 | __READ_ONCE(x); \
| ^~~~~~~~~~~
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
In file included from ./arch/x86/include/generated/asm/rwonce.h:1,
from include/linux/compiler.h:305,
from include/linux/export.h:5,
from include/linux/linkage.h:7,
from include/linux/fs.h:5:
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/asm-generic/rwonce.h:44:73: note: in definition of macro '__READ_ONCE'
44 | #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
| ^
include/linux/rcupdate.h:527:50: note: in expansion of macro 'READ_ONCE'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^~~~~~~~~
include/linux/rcupdate.h:675:9: note: in expansion of macro '__rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~
>> fs/fcntl.c:250:50: error: invalid type argument of '->' (have 'struct fown_struct')
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~
include/linux/rcupdate.h:530:19: note: in definition of macro '__rcu_dereference_check'
530 | ((typeof(*p) __force __kernel *)(local)); \
| ^
include/linux/rcupdate.h:747:28: note: in expansion of macro 'rcu_dereference_check'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^~~~~~~~~~~~~~~~~~~~~
fs/fcntl.c:250:21: note: in expansion of macro 'rcu_dereference'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^~~~~~~~~~~~~~~vim +250 fs/fcntl.c
239
240 #ifdef CONFIG_CHECKPOINT_RESTORE
241 static int f_getowner_uids(struct file *filp, unsigned long arg)
242 {
243 struct user_namespace *user_ns = current_user_ns();
244 uid_t __user *dst = (void __user *)arg;
245 const struct cred *fown_cred;
246 uid_t src[2];
247 int err;
248
249 rcu_read_lock();
> 250 fown_cred = rcu_dereference(filp->f_owner->cred);
251 src[0] = from_kuid(user_ns, fown_cred->uid);
252 src[1] = from_kuid(user_ns, fown_cred->euid);
253 rcu_read_unlock();
254
255 err = put_user(src[0], &dst[0]);
256 err |= put_user(src[1], &dst[1]);
257
258 return err;
259 }
260 #else
261 static int f_getowner_uids(struct file *filp, unsigned long arg)
262 {
263 return -EINVAL;
264 }
265 #endif
266
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies
In reply to Mickaël Salaünkernel test robot noticed the following build warnings:
Hi Mickaël,
kernel test robot noticed the following build warnings:
[auto build test WARNING on pcmoore-selinux/next]
[also build test WARNING on linus/master v6.11-rc3 next-20240812] [cannot apply to brauner-vfs/vfs.all]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Micka-l-Sala-n/fs-security-Fix-file_set_fowner-LSM-hook-inconsistencies/20240813-004648 base: https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git next patch link: https://lore.kernel.org/r/20240812144936.1616628-1-mic%40digikod.net patch subject: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20240813/[email protected]/config) compiler: m68k-linux-gcc (GCC) 14.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240813/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
security/smack/smack_lsm.c: In function 'smack_file_send_sigiotask':
>> security/smack/smack_lsm.c:1913:22: warning: variable 'file' set but not used [-Wunused-but-set-variable]
1913 | struct file *file;
| ^~~~vim +/file +1913 security/smack/smack_lsm.c
7898e1f8e9eb1b Casey Schaufler 2011-01-17 1895
e114e473771c84 Casey Schaufler 2008-02-04 1896 /**
e114e473771c84 Casey Schaufler 2008-02-04 1897 * smack_file_send_sigiotask - Smack on sigio
e114e473771c84 Casey Schaufler 2008-02-04 1898 * @tsk: The target task
e114e473771c84 Casey Schaufler 2008-02-04 1899 * @fown: the object the signal come from
e114e473771c84 Casey Schaufler 2008-02-04 1900 * @signum: unused
e114e473771c84 Casey Schaufler 2008-02-04 1901 *
e114e473771c84 Casey Schaufler 2008-02-04 1902 * Allow a privileged task to get signals even if it shouldn't
e114e473771c84 Casey Schaufler 2008-02-04 1903 *
e114e473771c84 Casey Schaufler 2008-02-04 1904 * Returns 0 if a subject with the object's smack could
e114e473771c84 Casey Schaufler 2008-02-04 1905 * write to the task, an error code otherwise.
e114e473771c84 Casey Schaufler 2008-02-04 1906 */
e114e473771c84 Casey Schaufler 2008-02-04 1907 static int smack_file_send_sigiotask(struct task_struct *tsk,
e114e473771c84 Casey Schaufler 2008-02-04 1908 struct fown_struct *fown, int signum)
e114e473771c84 Casey Schaufler 2008-02-04 1909 {
2f823ff8bec03a Casey Schaufler 2013-05-22 1910 struct smack_known *skp;
b17103a8b8ae9c Casey Schaufler 2018-11-09 1911 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
dcb569cf6ac99c Casey Schaufler 2018-09-18 1912 const struct cred *tcred;
e114e473771c84 Casey Schaufler 2008-02-04 @1913 struct file *file;
e114e473771c84 Casey Schaufler 2008-02-04 1914 int rc;
ecfcc53fef3c35 Etienne Basset 2009-04-08 1915 struct smk_audit_info ad;
e114e473771c84 Casey Schaufler 2008-02-04 1916
e114e473771c84 Casey Schaufler 2008-02-04 1917 /*
e114e473771c84 Casey Schaufler 2008-02-04 1918 * struct fown_struct is never outside the context of a struct file
e114e473771c84 Casey Schaufler 2008-02-04 1919 */
e114e473771c84 Casey Schaufler 2008-02-04 1920 file = container_of(fown, struct file, f_owner);
7898e1f8e9eb1b Casey Schaufler 2011-01-17 1921
ecfcc53fef3c35 Etienne Basset 2009-04-08 1922 /* we don't log here as rc can be overriden */
32192c8e14ea34 Mickaël Salaün 2024-08-12 1923 skp = smk_of_task(smack_cred(rcu_dereference(fown->cred)));
c60b906673eebb Casey Schaufler 2016-08-30 1924 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
c60b906673eebb Casey Schaufler 2016-08-30 1925 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
dcb569cf6ac99c Casey Schaufler 2018-09-18 1926
dcb569cf6ac99c Casey Schaufler 2018-09-18 1927 rcu_read_lock();
dcb569cf6ac99c Casey Schaufler 2018-09-18 1928 tcred = __task_cred(tsk);
dcb569cf6ac99c Casey Schaufler 2018-09-18 1929 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
ecfcc53fef3c35 Etienne Basset 2009-04-08 1930 rc = 0;
dcb569cf6ac99c Casey Schaufler 2018-09-18 1931 rcu_read_unlock();
ecfcc53fef3c35 Etienne Basset 2009-04-08 1932
ecfcc53fef3c35 Etienne Basset 2009-04-08 1933 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
ecfcc53fef3c35 Etienne Basset 2009-04-08 1934 smk_ad_setfield_u_tsk(&ad, tsk);
c60b906673eebb Casey Schaufler 2016-08-30 1935 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
e114e473771c84 Casey Schaufler 2008-02-04 1936 return rc;
e114e473771c84 Casey Schaufler 2008-02-04 1937 }
e114e473771c84 Casey Schaufler 2008-02-04 1938 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies
In reply to Mickaël Salaünkernel test robot noticed the following build errors:
Hi Mickaël,
kernel test robot noticed the following build errors:
[auto build test ERROR on pcmoore-selinux/next]
[also build test ERROR on linus/master v6.11-rc3 next-20240812] [cannot apply to brauner-vfs/vfs.all]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Micka-l-Sala-n/fs-security-Fix-file_set_fowner-LSM-hook-inconsistencies/20240813-004648 base: https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git next patch link: https://lore.kernel.org/r/20240812144936.1616628-1-mic%40digikod.net patch subject: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies config: x86_64-buildonly-randconfig-003-20240813 (https://download.01.org/0day-ci/archive/20240813/[email protected]/config) compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240813/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:10: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:31: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:510:22: note: expanded from macro 'compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~
include/linux/compiler_types.h:498:23: note: expanded from macro '_compiletime_assert'
498 | __compiletime_assert(condition, msg, prefix, suffix)
| ^~~~~~~~~
include/linux/compiler_types.h:490:9: note: expanded from macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:510:22: note: expanded from macro 'compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~
include/linux/compiler_types.h:498:23: note: expanded from macro '_compiletime_assert'
498 | __compiletime_assert(condition, msg, prefix, suffix)
| ^~~~~~~~~
include/linux/compiler_types.h:490:9: note: expanded from macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:510:22: note: expanded from macro 'compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~
include/linux/compiler_types.h:498:23: note: expanded from macro '_compiletime_assert'
498 | __compiletime_assert(condition, msg, prefix, suffix)
| ^~~~~~~~~
include/linux/compiler_types.h:490:9: note: expanded from macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:510:22: note: expanded from macro 'compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~
include/linux/compiler_types.h:498:23: note: expanded from macro '_compiletime_assert'
498 | __compiletime_assert(condition, msg, prefix, suffix)
| ^~~~~~~~~
include/linux/compiler_types.h:490:9: note: expanded from macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
note: (skipping 2 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:510:22: note: expanded from macro 'compiletime_assert'
510 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~
include/linux/compiler_types.h:498:23: note: expanded from macro '_compiletime_assert'
498 | __compiletime_assert(condition, msg, prefix, suffix)
| ^~~~~~~~~
include/linux/compiler_types.h:490:9: note: expanded from macro '__compiletime_assert'
490 | if (!(condition)) \
| ^~~~~~~~~
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
include/asm-generic/rwonce.h:50:14: note: expanded from macro 'READ_ONCE'
50 | __READ_ONCE(x); \
| ^
include/asm-generic/rwonce.h:44:65: note: expanded from macro '__READ_ONCE'
44 | #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
| ^
include/linux/compiler_types.h:466:13: note: expanded from macro '__unqual_scalar_typeof'
466 | _Generic((x), \
| ^
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
include/asm-generic/rwonce.h:50:14: note: expanded from macro 'READ_ONCE'
50 | __READ_ONCE(x); \
| ^
include/asm-generic/rwonce.h:44:65: note: expanded from macro '__READ_ONCE'
44 | #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
| ^
include/linux/compiler_types.h:473:15: note: expanded from macro '__unqual_scalar_typeof'
473 | default: (x)))
| ^
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:527:53: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^
include/asm-generic/rwonce.h:50:14: note: expanded from macro 'READ_ONCE'
50 | __READ_ONCE(x); \
| ^
include/asm-generic/rwonce.h:44:72: note: expanded from macro '__READ_ONCE'
44 | #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
| ^
>> fs/fcntl.c:250:14: error: cannot take the address of an rvalue of type 'const struct cred *'
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ^ ~~~~~~~~~~~~~~~~~~~
include/linux/rcupdate.h:747:28: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^ ~
include/linux/rcupdate.h:675:2: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^ ~
include/linux/rcupdate.h:527:43: note: expanded from macro '__rcu_dereference_check'
527 | typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
| ^ ~
include/asm-generic/rwonce.h:50:2: note: expanded from macro 'READ_ONCE'
50 | __READ_ONCE(x); \
| ^ ~
include/asm-generic/rwonce.h:44:70: note: expanded from macro '__READ_ONCE'
44 | #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
| ^ ~
>> fs/fcntl.c:250:43: error: member reference type 'struct fown_struct' is not a pointer; did you mean to use '.'?
250 | fown_cred = rcu_dereference(filp->f_owner->cred);
| ~~~~~~~~~~~~~^~
| .
include/linux/rcupdate.h:747:50: note: expanded from macro 'rcu_dereference'
747 | #define rcu_dereference(p) rcu_dereference_check(p, 0)
| ^
include/linux/rcupdate.h:675:27: note: expanded from macro 'rcu_dereference_check'
675 | __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
| ^
include/linux/rcupdate.h:530:12: note: expanded from macro '__rcu_dereference_check'
530 | ((typeof(*p) __force __kernel *)(local)); \
| ^12 errors generated.
vim +250 fs/fcntl.c
239
240 #ifdef CONFIG_CHECKPOINT_RESTORE
241 static int f_getowner_uids(struct file *filp, unsigned long arg)
242 {
243 struct user_namespace *user_ns = current_user_ns();
244 uid_t __user *dst = (void __user *)arg;
245 const struct cred *fown_cred;
246 uid_t src[2];
247 int err;
248
249 rcu_read_lock();
> 250 fown_cred = rcu_dereference(filp->f_owner->cred);
251 src[0] = from_kuid(user_ns, fown_cred->uid);
252 src[1] = from_kuid(user_ns, fown_cred->euid);
253 rcu_read_unlock();
254
255 err = put_user(src[0], &dst[0]);
256 err |= put_user(src[1], &dst[1]);
257
258 return err;
259 }
260 #else
261 static int f_getowner_uids(struct file *filp, unsigned long arg)
262 {
263 return -EINVAL;
264 }
265 #endif
266
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies
In reply to Mickaël Salaünkernel test robot noticed the following build warnings:
Hi Mickaël,
kernel test robot noticed the following build warnings:
[auto build test WARNING on pcmoore-selinux/next]
[also build test WARNING on linus/master v6.11-rc3]
[cannot apply to brauner-vfs/vfs.all next-20240813]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Micka-l-Sala-n/fs-security-Fix-file_set_fowner-LSM-hook-inconsistencies/20240813-004648 base: https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux.git next patch link: https://lore.kernel.org/r/20240812144936.1616628-1-mic%40digikod.net patch subject: [PATCH] fs,security: Fix file_set_fowner LSM hook inconsistencies config: x86_64-randconfig-122-20240813 (https://download.01.org/0day-ci/archive/20240813/[email protected]/config) compiler: gcc-11 (Debian 11.3.0-12) 11.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240813/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> fs/file_table.c:153:25: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct cred const [noderef] __rcu *cred @@ got struct cred const * @@ fs/file_table.c:153:25: sparse: expected struct cred const [noderef] __rcu *cred fs/file_table.c:153:25: sparse: got struct cred const * >> fs/file_table.c:157:36: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct cred const *cred @@ got struct cred const [noderef] __rcu *cred @@
fs/file_table.c:157:36: sparse: expected struct cred const *cred
fs/file_table.c:157:36: sparse: got struct cred const [noderef] __rcu *cred
fs/file_table.c:69:28: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct cred const *cred @@ got struct cred const [noderef] __rcu *cred @@
fs/file_table.c:69:28: sparse: expected struct cred const *cred
fs/file_table.c:69:28: sparse: got struct cred const [noderef] __rcu *cred
fs/file_table.c:69:28: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct cred const *cred @@ got struct cred const [noderef] __rcu *cred @@
fs/file_table.c:69:28: sparse: expected struct cred const *cred
fs/file_table.c:69:28: sparse: got struct cred const [noderef] __rcu *cred
vim +153 fs/file_table.c
147
148 static int init_file(struct file *f, int flags, const struct cred *cred)
149 {
150 int error;
151
152 f->f_cred = get_cred(cred);
> 153 f->f_owner.cred = get_cred(cred);
154 error = security_file_alloc(f);
155 if (unlikely(error)) {
156 put_cred(f->f_cred);
> 157 put_cred(f->f_owner.cred); 158 return error;
159 }
160
161 rwlock_init(&f->f_owner.lock);
162 spin_lock_init(&f->f_lock);
163 mutex_init(&f->f_pos_lock);
164 f->f_flags = flags;
165 f->f_mode = OPEN_FMODE(flags);
166 /* f->f_version: 0 */
167
168 /*
169 * We're SLAB_TYPESAFE_BY_RCU so initialize f_count last. While
170 * fget-rcu pattern users need to be able to handle spurious
171 * refcount bumps we should reinitialize the reused file first.
172 */
173 atomic_long_set(&f->f_count, 1);
174 return 0;
175 }
176
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki