tools/testing/selftests/lsm/common.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/lsm/common.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/lsm/common.c- Extension
.c- Size
- 1495 bytes
- Lines
- 90
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- 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.
Dependency Surface
linux/lsm.hfcntl.hstring.hstdio.hstdlib.hunistd.hsys/types.hcommon.h
Detected Declarations
function read_proc_attrfunction read_sysfs_lsmsfunction attr_lsm_count
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Linux Security Module infrastructure tests
*
* Copyright © 2023 Casey Schaufler <casey@schaufler-ca.com>
*/
#define _GNU_SOURCE
#include <linux/lsm.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "common.h"
#define PROCATTR "/proc/self/attr/"
int read_proc_attr(const char *attr, char *value, size_t size)
{
int fd;
int len;
char *path;
len = strlen(PROCATTR) + strlen(attr) + 1;
path = calloc(len, 1);
if (path == NULL)
return -1;
sprintf(path, "%s%s", PROCATTR, attr);
fd = open(path, O_RDONLY);
free(path);
if (fd < 0)
return -1;
len = read(fd, value, size);
close(fd);
/* Ensure value is terminated */
if (len <= 0 || len == size)
return -1;
value[len] = '\0';
path = strchr(value, '\n');
if (path)
*path = '\0';
return 0;
}
int read_sysfs_lsms(char *lsms, size_t size)
{
FILE *fp;
size_t red;
fp = fopen("/sys/kernel/security/lsm", "r");
if (fp == NULL)
return -1;
red = fread(lsms, 1, size, fp);
fclose(fp);
if (red <= 0 || red == size)
return -1;
lsms[red] = '\0';
return 0;
}
int attr_lsm_count(void)
{
char *names = calloc(sysconf(_SC_PAGESIZE), 1);
int count = 0;
if (!names)
return 0;
if (read_sysfs_lsms(names, sysconf(_SC_PAGESIZE)))
return 0;
if (strstr(names, "selinux"))
count++;
if (strstr(names, "smack"))
count++;
if (strstr(names, "apparmor"))
count++;
return count;
}
Annotation
- Immediate include surface: `linux/lsm.h`, `fcntl.h`, `string.h`, `stdio.h`, `stdlib.h`, `unistd.h`, `sys/types.h`, `common.h`.
- Detected declarations: `function read_proc_attr`, `function read_sysfs_lsms`, `function attr_lsm_count`.
- Atlas domain: Support Tooling And Documentation / tools.
- 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.