security/landlock/net.c
Source file repositories/reference/linux-study-clean/security/landlock/net.c
File Facts
- System
- Linux kernel
- Corpus path
security/landlock/net.c- Extension
.c- Size
- 11084 bytes
- Lines
- 380
- Domain
- Core OS
- Bucket
- Security And Isolation
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/in.hlinux/lsm_audit.hlinux/net.hlinux/socket.hnet/ipv6.haudit.hcommon.hcred.hlimits.hnet.hruleset.h
Detected Declarations
function landlock_append_net_rulefunction current_check_access_socketfunction landlock_log_denialfunction setsockoptfunction current_check_autobind_udp_socketfunction hook_socket_bindfunction hook_socket_connectfunction hook_socket_sendmsgfunction landlock_add_net_hooks
Annotated Snippet
if (sock_family == AF_INET6) {
/*
* We cannot allow sending UDP datagrams to an
* explicit AF_UNSPEC address on IPv6 sockets,
* even if AF_UNSPEC is treated as "no address"
* on such sockets (so it should always be
* allowed). That's because the socket's family
* can change under our feet (if another thread
* calls setsockopt(IPV6_ADDRFORM)) to IPv4,
* which would then treat AF_UNSPEC as AF_INET.
*/
audit_net.family = AF_UNSPEC;
audit_net.sk = sock->sk;
landlock_init_layer_masks(
subject->domain, access_request,
&layer_masks, LANDLOCK_KEY_NET_PORT);
landlock_log_denial(
subject,
&(struct landlock_request){
.type = LANDLOCK_REQUEST_NET_ACCESS,
.audit.type =
LSM_AUDIT_DATA_NET,
.audit.u.net = &audit_net,
.access = access_request,
.layer_masks = &layer_masks,
});
return -EACCES;
}
} else if (access_request == LANDLOCK_ACCESS_NET_BIND_TCP ||
access_request == LANDLOCK_ACCESS_NET_BIND_UDP) {
/*
* Binding to an AF_UNSPEC address is treated
* differently by IPv4 and IPv6 sockets. The socket's
* family may change under our feet due to
* setsockopt(IPV6_ADDRFORM), but that's ok: we either
* reject entirely for IPv6 or require
* %LANDLOCK_ACCESS_NET_BIND_TCP or
* %LANDLOCK_ACCESS_NET_BIND_UDP for IPv4, so it cannot
* be used to bypass the policy.
*
* IPv4 sockets map AF_UNSPEC to AF_INET for
* retrocompatibility for bind accesses, only if the
* address is INADDR_ANY (cf. __inet_bind). IPv6
* sockets always reject it.
*
* Checking the address is required to not wrongfully
* return -EACCES instead of -EAFNOSUPPORT or -EINVAL.
* We could return 0 and let the network stack handle
* these checks, but it is safer to return a proper
* error and test consistency thanks to kselftest.
*/
if (sock_family == AF_INET) {
const struct sockaddr_in *const sockaddr =
(struct sockaddr_in *)address;
if (addrlen < sizeof(struct sockaddr_in))
return -EINVAL;
if (sockaddr->sin_addr.s_addr !=
htonl(INADDR_ANY))
return -EAFNOSUPPORT;
} else {
if (addrlen < SIN6_LEN_RFC2133)
return -EINVAL;
else
return -EAFNOSUPPORT;
}
} else {
WARN_ON_ONCE(1);
}
/*
* AF_UNSPEC is treated as AF_INET only in
* bind(AF_UNSPEC+INADDR_ANY) on IPv4 sockets and when sending
* to AF_UNSPEC addresses on IPv4 sockets.
*/
fallthrough;
case AF_INET: {
const struct sockaddr_in *addr4;
if (addrlen < sizeof(struct sockaddr_in))
return -EINVAL;
addr4 = (struct sockaddr_in *)address;
port = addr4->sin_port;
if (access_request == LANDLOCK_ACCESS_NET_CONNECT_TCP ||
access_request == LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP) {
audit_net.dport = port;
audit_net.v4info.daddr = addr4->sin_addr.s_addr;
} else if (access_request == LANDLOCK_ACCESS_NET_BIND_TCP ||
Annotation
- Immediate include surface: `linux/in.h`, `linux/lsm_audit.h`, `linux/net.h`, `linux/socket.h`, `net/ipv6.h`, `audit.h`, `common.h`, `cred.h`.
- Detected declarations: `function landlock_append_net_rule`, `function current_check_access_socket`, `function landlock_log_denial`, `function setsockopt`, `function current_check_autobind_udp_socket`, `function hook_socket_bind`, `function hook_socket_connect`, `function hook_socket_sendmsg`, `function landlock_add_net_hooks`.
- Atlas domain: Core OS / Security And Isolation.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.