Skip to content

Commit 86b405a

Browse files
committed
Merge tag 'landlock-6.11-rc1-houdini-fix' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux
Pull landlock fix from Mickaël Salaün: "Jann Horn reported a sandbox bypass for Landlock. This includes the fix and new tests. This should be backported" * tag 'landlock-6.11-rc1-houdini-fix' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux: selftests/landlock: Add cred_transfer test landlock: Don't lose track of restrictions on cred_transfer
2 parents 8e33379 + cc37478 commit 86b405a

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

security/landlock/cred.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@
1414
#include "ruleset.h"
1515
#include "setup.h"
1616

17-
static int hook_cred_prepare(struct cred *const new,
18-
const struct cred *const old, const gfp_t gfp)
17+
static void hook_cred_transfer(struct cred *const new,
18+
const struct cred *const old)
1919
{
2020
struct landlock_ruleset *const old_dom = landlock_cred(old)->domain;
2121

2222
if (old_dom) {
2323
landlock_get_ruleset(old_dom);
2424
landlock_cred(new)->domain = old_dom;
2525
}
26+
}
27+
28+
static int hook_cred_prepare(struct cred *const new,
29+
const struct cred *const old, const gfp_t gfp)
30+
{
31+
hook_cred_transfer(new, old);
2632
return 0;
2733
}
2834

@@ -36,6 +42,7 @@ static void hook_cred_free(struct cred *const cred)
3642

3743
static struct security_hook_list landlock_hooks[] __ro_after_init = {
3844
LSM_HOOK_INIT(cred_prepare, hook_cred_prepare),
45+
LSM_HOOK_INIT(cred_transfer, hook_cred_transfer),
3946
LSM_HOOK_INIT(cred_free, hook_cred_free),
4047
};
4148

tools/testing/selftests/landlock/base_test.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define _GNU_SOURCE
1010
#include <errno.h>
1111
#include <fcntl.h>
12+
#include <linux/keyctl.h>
1213
#include <linux/landlock.h>
1314
#include <string.h>
1415
#include <sys/prctl.h>
@@ -326,4 +327,77 @@ TEST(ruleset_fd_transfer)
326327
ASSERT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
327328
}
328329

330+
TEST(cred_transfer)
331+
{
332+
struct landlock_ruleset_attr ruleset_attr = {
333+
.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR,
334+
};
335+
int ruleset_fd, dir_fd;
336+
pid_t child;
337+
int status;
338+
339+
drop_caps(_metadata);
340+
341+
dir_fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
342+
EXPECT_LE(0, dir_fd);
343+
EXPECT_EQ(0, close(dir_fd));
344+
345+
/* Denies opening directories. */
346+
ruleset_fd =
347+
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
348+
ASSERT_LE(0, ruleset_fd);
349+
EXPECT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
350+
ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
351+
EXPECT_EQ(0, close(ruleset_fd));
352+
353+
/* Checks ruleset enforcement. */
354+
EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
355+
EXPECT_EQ(EACCES, errno);
356+
357+
/* Needed for KEYCTL_SESSION_TO_PARENT permission checks */
358+
EXPECT_NE(-1, syscall(__NR_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL, 0,
359+
0, 0))
360+
{
361+
TH_LOG("Failed to join session keyring: %s", strerror(errno));
362+
}
363+
364+
child = fork();
365+
ASSERT_LE(0, child);
366+
if (child == 0) {
367+
/* Checks ruleset enforcement. */
368+
EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
369+
EXPECT_EQ(EACCES, errno);
370+
371+
/*
372+
* KEYCTL_SESSION_TO_PARENT is a no-op unless we have a
373+
* different session keyring in the child, so make that happen.
374+
*/
375+
EXPECT_NE(-1, syscall(__NR_keyctl, KEYCTL_JOIN_SESSION_KEYRING,
376+
NULL, 0, 0, 0));
377+
378+
/*
379+
* KEYCTL_SESSION_TO_PARENT installs credentials on the parent
380+
* that never go through the cred_prepare hook, this path uses
381+
* cred_transfer instead.
382+
*/
383+
EXPECT_EQ(0, syscall(__NR_keyctl, KEYCTL_SESSION_TO_PARENT, 0,
384+
0, 0, 0));
385+
386+
/* Re-checks ruleset enforcement. */
387+
EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
388+
EXPECT_EQ(EACCES, errno);
389+
390+
_exit(_metadata->exit_code);
391+
return;
392+
}
393+
394+
EXPECT_EQ(child, waitpid(child, &status, 0));
395+
EXPECT_EQ(1, WIFEXITED(status));
396+
EXPECT_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
397+
398+
/* Re-checks ruleset enforcement. */
399+
EXPECT_EQ(-1, open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
400+
EXPECT_EQ(EACCES, errno);
401+
}
402+
329403
TEST_HARNESS_MAIN

tools/testing/selftests/landlock/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ CONFIG_CGROUPS=y
22
CONFIG_CGROUP_SCHED=y
33
CONFIG_INET=y
44
CONFIG_IPV6=y
5+
CONFIG_KEYS=y
56
CONFIG_NET=y
67
CONFIG_NET_NS=y
78
CONFIG_OVERLAY_FS=y

0 commit comments

Comments
 (0)