]> www.infradead.org Git - users/jedix/linux-maple.git/commit
kernel/sys.c: fix and improve control flow in __sys_setres[ug]id()
authorOndrej Mosnacek <omosnace@redhat.com>
Fri, 17 Feb 2023 16:21:54 +0000 (17:21 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 5 Apr 2023 23:01:30 +0000 (16:01 -0700)
commit69fdeec5dde7b04ab8e0165d67e1d7d5203a3972
treef52545d302dd73321cae89368cdc2379158e8bed
parentbdd034de3a28ffdacab528aebad17f1df968180c
kernel/sys.c: fix and improve control flow in __sys_setres[ug]id()

Linux Security Modules (LSMs) that implement the "capable" hook will
usually emit an access denial message to the audit log whenever they
"block" the current task from using the given capability based on their
security policy.

The occurrence of a denial is used as an indication that the given task
has attempted an operation that requires the given access permission, so
the callers of functions that perform LSM permission checks must take care
to avoid calling them too early (before it is decided if the permission is
actually needed to perform the requested operation).

The __sys_setres[ug]id() functions violate this convention by first
calling ns_capable_setid() and only then checking if the operation
requires the capability or not.  It means that any caller that has the
capability granted by DAC (task's capability set) but not by MAC (LSMs)
will generate a "denied" audit record, even if is doing an operation for
which the capability is not required.

Fix this by reordering the checks such that ns_capable_setid() is checked
last and -EPERM is returned immediately if it returns false.

While there, also do two small optimizations:
* move the capability check before prepare_creds() and
* bail out early in case of a no-op.

Link: https://lkml.kernel.org/r/20230217162154.837549-1-omosnace@redhat.com
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/sys.c