samples/landlock/sandboxer.c
Source file repositories/reference/linux-study-clean/samples/landlock/sandboxer.c
File Facts
- System
- Linux kernel
- Corpus path
samples/landlock/sandboxer.c- Extension
.c- Size
- 21220 bytes
- Lines
- 700
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
arpa/inet.herrno.hfcntl.hlinux/landlock.hlinux/socket.hstddef.hstdio.hstdlib.hstring.hsys/prctl.hsys/stat.hsys/syscall.hunistd.hstdbool.hlinux/prctl.h
Detected Declarations
function landlock_create_rulesetfunction landlock_add_rulefunction landlock_restrict_selffunction str2numfunction parse_pathfunction populate_ruleset_fsfunction populate_ruleset_netfunction check_ruleset_scopefunction strsepfunction add_quiet_accessfunction mainfunction getenv
Annotated Snippet
if (path_beneath.parent_fd < 0) {
fprintf(stderr, "Failed to open \"%s\": %s\n",
path_list[i], strerror(errno));
continue;
}
if (fstat(path_beneath.parent_fd, &statbuf)) {
fprintf(stderr, "Failed to stat \"%s\": %s\n",
path_list[i], strerror(errno));
close(path_beneath.parent_fd);
goto out_free_name;
}
path_beneath.allowed_access = allowed_access;
if (!S_ISDIR(statbuf.st_mode))
path_beneath.allowed_access &= ACCESS_FILE;
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
&path_beneath, flags)) {
fprintf(stderr,
"Failed to update the ruleset with \"%s\": %s\n",
path_list[i], strerror(errno));
close(path_beneath.parent_fd);
goto out_free_name;
}
close(path_beneath.parent_fd);
}
ret = 0;
out_free_name:
free(path_list);
free(env_path_name);
return ret;
}
static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
const __u64 allowed_access, __u32 flags)
{
int ret = 1;
char *env_port_name, *env_port_name_next, *strport;
struct landlock_net_port_attr net_port = {
.allowed_access = allowed_access,
};
env_port_name = getenv(env_var);
if (!env_port_name)
return 0;
env_port_name = strdup(env_port_name);
unsetenv(env_var);
env_port_name_next = env_port_name;
while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
__u64 port;
if (strcmp(strport, "") == 0)
continue;
if (str2num(strport, &port)) {
fprintf(stderr, "Failed to parse port at \"%s\"\n",
strport);
goto out_free_name;
}
net_port.port = port;
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
&net_port, flags)) {
fprintf(stderr,
"Failed to update the ruleset with port \"%llu\": %s\n",
net_port.port, strerror(errno));
goto out_free_name;
}
}
ret = 0;
out_free_name:
free(env_port_name);
return ret;
}
/* Returns true on error, false otherwise. */
static bool check_ruleset_scope(const char *const env_var,
struct landlock_ruleset_attr *ruleset_attr)
{
char *env_type_scope, *env_type_scope_next, *ipc_scoping_name;
bool error = false;
bool abstract_scoping = false;
bool signal_scoping = false;
/* Scoping is not supported by Landlock ABI */
if (!(ruleset_attr->scoped &
(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL)))
goto out_unset;
env_type_scope = getenv(env_var);
Annotation
- Immediate include surface: `arpa/inet.h`, `errno.h`, `fcntl.h`, `linux/landlock.h`, `linux/socket.h`, `stddef.h`, `stdio.h`, `stdlib.h`.
- Detected declarations: `function landlock_create_ruleset`, `function landlock_add_rule`, `function landlock_restrict_self`, `function str2num`, `function parse_path`, `function populate_ruleset_fs`, `function populate_ruleset_net`, `function check_ruleset_scope`, `function strsep`, `function add_quiet_access`.
- Atlas domain: Support Tooling And Documentation / samples.
- Implementation status: source implementation candidate.
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.