forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
landlock: Support socket access-control
* Add new landlock rule type that corresponds to the restriction of socket protocols. This is represented as an landlock_socket_attr structure. Protocol allowed by landlock must be described by a family-type pair (see socket(2)). * Support socket rule storage in landlock ruleset. * Add flag LANDLOCK_ACCESS_SOCKET_CREATE that will provide the ability to control socket creation. * Add socket.c file that will contain socket rules management and hooks. Implement helper pack_socket_key() to convert 32-bit family and type values into uintptr_t. This is possible due to the fact that these values are limited to AF_MAX (=46), SOCK_MAX (=11) constants. Assumption is checked in build-time by the helper. * Support socket rules in landlock syscalls. Change ABI version to 6. Closes: landlock-lsm#6 Signed-off-by: Mikhail Ivanov <[email protected]>
- Loading branch information
1 parent
6d69b6c
commit ec135c7
Showing
9 changed files
with
272 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o | ||
|
||
landlock-y := setup.o syscalls.o object.o ruleset.o \ | ||
cred.o task.o fs.o | ||
cred.o task.o fs.o socket.o | ||
|
||
landlock-$(CONFIG_INET) += net.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
/* | ||
* Landlock LSM - Socket management and hooks | ||
* | ||
* Copyright © 2024 Huawei Tech. Co., Ltd. | ||
*/ | ||
|
||
#include <linux/net.h> | ||
#include <linux/socket.h> | ||
#include <linux/stddef.h> | ||
|
||
#include "limits.h" | ||
#include "ruleset.h" | ||
#include "socket.h" | ||
|
||
static uintptr_t pack_socket_key(const int family, const int type) | ||
{ | ||
union { | ||
struct { | ||
unsigned short family, type; | ||
} __packed data; | ||
uintptr_t packed; | ||
} socket_key; | ||
|
||
/* Checks that all supported socket families and types can be stored in socket_key. */ | ||
BUILD_BUG_ON(AF_MAX > (typeof(socket_key.data.family))~0); | ||
BUILD_BUG_ON(SOCK_MAX > (typeof(socket_key.data.type))~0); | ||
|
||
/* Checks that socket_key can be stored in landlock_key. */ | ||
BUILD_BUG_ON(sizeof(socket_key.data) > sizeof(socket_key.packed)); | ||
BUILD_BUG_ON(sizeof(socket_key.packed) > | ||
sizeof_field(union landlock_key, data)); | ||
|
||
socket_key.data.family = (unsigned short)family; | ||
socket_key.data.type = (unsigned short)type; | ||
|
||
return socket_key.packed; | ||
} | ||
|
||
int landlock_append_socket_rule(struct landlock_ruleset *const ruleset, | ||
const int family, const int type, | ||
access_mask_t access_rights) | ||
{ | ||
int err; | ||
|
||
const struct landlock_id id = { | ||
.key.data = pack_socket_key(family, type), | ||
.type = LANDLOCK_KEY_SOCKET, | ||
}; | ||
|
||
/* Transforms relative access rights to absolute ones. */ | ||
access_rights |= LANDLOCK_MASK_ACCESS_SOCKET & | ||
~landlock_get_socket_access_mask(ruleset, 0); | ||
|
||
mutex_lock(&ruleset->lock); | ||
err = landlock_insert_rule(ruleset, id, access_rights); | ||
mutex_unlock(&ruleset->lock); | ||
|
||
return err; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
/* | ||
* Landlock LSM - Socket management and hooks | ||
* | ||
* Copyright © 2024 Huawei Tech. Co., Ltd. | ||
*/ | ||
|
||
#ifndef _SECURITY_LANDLOCK_SOCKET_H | ||
#define _SECURITY_LANDLOCK_SOCKET_H | ||
|
||
#include "ruleset.h" | ||
|
||
int landlock_append_socket_rule(struct landlock_ruleset *const ruleset, | ||
const int family, const int type, | ||
access_mask_t access_rights); | ||
|
||
#endif /* _SECURITY_LANDLOCK_SOCKET_H */ |
Oops, something went wrong.