Skip to content

Commit ae388ed

Browse files
committed
Merge tag 'landlock-6.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux
Pull landlock update from Mickaël Salaün: "Fix test issues, improve build compatibility, and add new tests" * tag 'landlock-6.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mic/linux: landlock: Fix cosmetic change samples/landlock: Fix building on musl libc landlock: Fix warning from KUnit tests selftests/landlock: Add test to check rule tied to covered mount point selftests/landlock: Fix build of audit_test selftests/landlock: Fix readlink check
2 parents e833f7d + 6803b6e commit ae388ed

6 files changed

Lines changed: 92 additions & 31 deletions

File tree

samples/landlock/sandboxer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <errno.h>
1414
#include <fcntl.h>
1515
#include <linux/landlock.h>
16-
#include <linux/prctl.h>
1716
#include <linux/socket.h>
1817
#include <stddef.h>
1918
#include <stdio.h>
@@ -25,6 +24,10 @@
2524
#include <unistd.h>
2625
#include <stdbool.h>
2726

27+
#if defined(__GLIBC__)
28+
#include <linux/prctl.h>
29+
#endif
30+
2831
#ifndef landlock_create_ruleset
2932
static inline int
3033
landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,

security/landlock/fs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ static bool is_access_to_paths_allowed(
895895
/* Stops when a rule from each layer grants access. */
896896
if (allowed_parent1 && allowed_parent2)
897897
break;
898+
898899
jump_up:
899900
if (walker_path.dentry == walker_path.mnt->mnt_root) {
900901
if (follow_up(&walker_path)) {

security/landlock/id.c

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,
119119

120120
#ifdef CONFIG_SECURITY_LANDLOCK_KUNIT_TEST
121121

122+
static u8 get_random_u8_positive(void)
123+
{
124+
/* max() evaluates its arguments once. */
125+
return max(1, get_random_u8());
126+
}
127+
122128
static void test_range1_rand0(struct kunit *const test)
123129
{
124130
atomic64_t counter;
@@ -127,9 +133,10 @@ static void test_range1_rand0(struct kunit *const test)
127133
init = get_random_u32();
128134
atomic64_set(&counter, init);
129135
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 0), init);
130-
KUNIT_EXPECT_EQ(
131-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
132-
init + 1);
136+
KUNIT_EXPECT_EQ(test,
137+
get_id_range(get_random_u8_positive(), &counter,
138+
get_random_u8()),
139+
init + 1);
133140
}
134141

135142
static void test_range1_rand1(struct kunit *const test)
@@ -140,9 +147,10 @@ static void test_range1_rand1(struct kunit *const test)
140147
init = get_random_u32();
141148
atomic64_set(&counter, init);
142149
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 1), init);
143-
KUNIT_EXPECT_EQ(
144-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
145-
init + 2);
150+
KUNIT_EXPECT_EQ(test,
151+
get_id_range(get_random_u8_positive(), &counter,
152+
get_random_u8()),
153+
init + 2);
146154
}
147155

148156
static void test_range1_rand15(struct kunit *const test)
@@ -153,9 +161,10 @@ static void test_range1_rand15(struct kunit *const test)
153161
init = get_random_u32();
154162
atomic64_set(&counter, init);
155163
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 15), init);
156-
KUNIT_EXPECT_EQ(
157-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
158-
init + 16);
164+
KUNIT_EXPECT_EQ(test,
165+
get_id_range(get_random_u8_positive(), &counter,
166+
get_random_u8()),
167+
init + 16);
159168
}
160169

161170
static void test_range1_rand16(struct kunit *const test)
@@ -166,9 +175,10 @@ static void test_range1_rand16(struct kunit *const test)
166175
init = get_random_u32();
167176
atomic64_set(&counter, init);
168177
KUNIT_EXPECT_EQ(test, get_id_range(1, &counter, 16), init);
169-
KUNIT_EXPECT_EQ(
170-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
171-
init + 1);
178+
KUNIT_EXPECT_EQ(test,
179+
get_id_range(get_random_u8_positive(), &counter,
180+
get_random_u8()),
181+
init + 1);
172182
}
173183

174184
static void test_range2_rand0(struct kunit *const test)
@@ -179,9 +189,10 @@ static void test_range2_rand0(struct kunit *const test)
179189
init = get_random_u32();
180190
atomic64_set(&counter, init);
181191
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 0), init);
182-
KUNIT_EXPECT_EQ(
183-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
184-
init + 2);
192+
KUNIT_EXPECT_EQ(test,
193+
get_id_range(get_random_u8_positive(), &counter,
194+
get_random_u8()),
195+
init + 2);
185196
}
186197

187198
static void test_range2_rand1(struct kunit *const test)
@@ -192,9 +203,10 @@ static void test_range2_rand1(struct kunit *const test)
192203
init = get_random_u32();
193204
atomic64_set(&counter, init);
194205
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 1), init);
195-
KUNIT_EXPECT_EQ(
196-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
197-
init + 3);
206+
KUNIT_EXPECT_EQ(test,
207+
get_id_range(get_random_u8_positive(), &counter,
208+
get_random_u8()),
209+
init + 3);
198210
}
199211

200212
static void test_range2_rand2(struct kunit *const test)
@@ -205,9 +217,10 @@ static void test_range2_rand2(struct kunit *const test)
205217
init = get_random_u32();
206218
atomic64_set(&counter, init);
207219
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 2), init);
208-
KUNIT_EXPECT_EQ(
209-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
210-
init + 4);
220+
KUNIT_EXPECT_EQ(test,
221+
get_id_range(get_random_u8_positive(), &counter,
222+
get_random_u8()),
223+
init + 4);
211224
}
212225

213226
static void test_range2_rand15(struct kunit *const test)
@@ -218,9 +231,10 @@ static void test_range2_rand15(struct kunit *const test)
218231
init = get_random_u32();
219232
atomic64_set(&counter, init);
220233
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 15), init);
221-
KUNIT_EXPECT_EQ(
222-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
223-
init + 17);
234+
KUNIT_EXPECT_EQ(test,
235+
get_id_range(get_random_u8_positive(), &counter,
236+
get_random_u8()),
237+
init + 17);
224238
}
225239

226240
static void test_range2_rand16(struct kunit *const test)
@@ -231,9 +245,10 @@ static void test_range2_rand16(struct kunit *const test)
231245
init = get_random_u32();
232246
atomic64_set(&counter, init);
233247
KUNIT_EXPECT_EQ(test, get_id_range(2, &counter, 16), init);
234-
KUNIT_EXPECT_EQ(
235-
test, get_id_range(get_random_u8(), &counter, get_random_u8()),
236-
init + 2);
248+
KUNIT_EXPECT_EQ(test,
249+
get_id_range(get_random_u8_positive(), &counter,
250+
get_random_u8()),
251+
init + 2);
237252
}
238253

239254
#endif /* CONFIG_SECURITY_LANDLOCK_KUNIT_TEST */

tools/testing/selftests/landlock/audit.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,12 @@ static int audit_init_filter_exe(struct audit_filter *filter, const char *path)
403403
/* It is assume that there is not already filtering rules. */
404404
filter->record_type = AUDIT_EXE;
405405
if (!path) {
406-
filter->exe_len = readlink("/proc/self/exe", filter->exe,
407-
sizeof(filter->exe) - 1);
408-
if (filter->exe_len < 0)
406+
int ret = readlink("/proc/self/exe", filter->exe,
407+
sizeof(filter->exe) - 1);
408+
if (ret < 0)
409409
return -errno;
410410

411+
filter->exe_len = ret;
411412
return 0;
412413
}
413414

tools/testing/selftests/landlock/audit_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#define _GNU_SOURCE
99
#include <errno.h>
10+
#include <fcntl.h>
1011
#include <limits.h>
1112
#include <linux/landlock.h>
1213
#include <pthread.h>

tools/testing/selftests/landlock/fs_test.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,46 @@ TEST_F_FORK(layout1, release_inodes)
18321832
ASSERT_EQ(ENOENT, test_open(dir_s3d3, O_RDONLY));
18331833
}
18341834

1835+
/*
1836+
* This test checks that a rule on a directory used as a mount point does not
1837+
* grant access to the mount covering it. It is a generalization of the bind
1838+
* mount case in layout3_fs.hostfs.release_inodes that tests hidden mount points.
1839+
*/
1840+
TEST_F_FORK(layout1, covered_rule)
1841+
{
1842+
const struct rule layer1[] = {
1843+
{
1844+
.path = dir_s3d2,
1845+
.access = LANDLOCK_ACCESS_FS_READ_DIR,
1846+
},
1847+
{},
1848+
};
1849+
int ruleset_fd;
1850+
1851+
/* Unmount to simplify FIXTURE_TEARDOWN. */
1852+
set_cap(_metadata, CAP_SYS_ADMIN);
1853+
ASSERT_EQ(0, umount(dir_s3d2));
1854+
clear_cap(_metadata, CAP_SYS_ADMIN);
1855+
1856+
/* Creates a ruleset with the future hidden directory. */
1857+
ruleset_fd =
1858+
create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, layer1);
1859+
ASSERT_LE(0, ruleset_fd);
1860+
1861+
/* Covers with a new mount point. */
1862+
set_cap(_metadata, CAP_SYS_ADMIN);
1863+
ASSERT_EQ(0, mount_opt(&mnt_tmp, dir_s3d2));
1864+
clear_cap(_metadata, CAP_SYS_ADMIN);
1865+
1866+
ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY));
1867+
1868+
enforce_ruleset(_metadata, ruleset_fd);
1869+
ASSERT_EQ(0, close(ruleset_fd));
1870+
1871+
/* Checks that access to the new mount point is denied. */
1872+
ASSERT_EQ(EACCES, test_open(dir_s3d2, O_RDONLY));
1873+
}
1874+
18351875
enum relative_access {
18361876
REL_OPEN,
18371877
REL_CHDIR,

0 commit comments

Comments
 (0)