Skip to content

Commit a46e32d

Browse files
gnoackl0kod
authored andcommitted
landlock: Clarify BUILD_BUG_ON check in scoping logic
The BUILD_BUG_ON check in domain_is_scoped() and unmask_scoped_access() should check that the loop that counts down client_layer finishes. We therefore check that the numbers LANDLOCK_MAX_NUM_LAYERS-1 and -1 are both representable by that integer. If they are representable, the numbers in between are representable too, and the loop finishes. Signed-off-by: Günther Noack <gnoack3000@gmail.com> Link: https://lore.kernel.org/r/20260327164838.38231-6-gnoack3000@gmail.com Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent ae97330 commit a46e32d

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

security/landlock/fs.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,10 +1595,13 @@ static void unmask_scoped_access(const struct landlock_ruleset *const client,
15951595
return;
15961596

15971597
/*
1598-
* client_layer must be a signed integer with greater capacity
1599-
* than client->num_layers to ensure the following loop stops.
1598+
* client_layer must be able to represent all numbers from
1599+
* LANDLOCK_MAX_NUM_LAYERS - 1 to -1 for the loop below to terminate.
1600+
* (It must be large enough, and it must be signed.)
16001601
*/
1601-
BUILD_BUG_ON(sizeof(client_layer) > sizeof(client->num_layers));
1602+
BUILD_BUG_ON(!is_signed_type(typeof(client_layer)));
1603+
BUILD_BUG_ON(LANDLOCK_MAX_NUM_LAYERS - 1 >
1604+
type_max(typeof(client_layer)));
16021605

16031606
client_layer = client->num_layers - 1;
16041607
client_walker = client->hierarchy;

security/landlock/task.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,13 @@ static bool domain_is_scoped(const struct landlock_ruleset *const client,
191191
client_layer = client->num_layers - 1;
192192
client_walker = client->hierarchy;
193193
/*
194-
* client_layer must be a signed integer with greater capacity
195-
* than client->num_layers to ensure the following loop stops.
194+
* client_layer must be able to represent all numbers from
195+
* LANDLOCK_MAX_NUM_LAYERS - 1 to -1 for the loop below to terminate.
196+
* (It must be large enough, and it must be signed.)
196197
*/
197-
BUILD_BUG_ON(sizeof(client_layer) > sizeof(client->num_layers));
198+
BUILD_BUG_ON(!is_signed_type(typeof(client_layer)));
199+
BUILD_BUG_ON(LANDLOCK_MAX_NUM_LAYERS - 1 >
200+
type_max(typeof(client_layer)));
198201

199202
server_layer = server ? (server->num_layers - 1) : -1;
200203
server_walker = server ? server->hierarchy : NULL;

0 commit comments

Comments
 (0)