1 // SPDX-License-Identifier: GPL-2.0
3 * Linux Security Module infrastructure tests
5 * Copyright © 2023 Casey Schaufler <casey@schaufler-ca.com>
15 #include <sys/types.h>
18 #define PROCATTR "/proc/self/attr/"
20 int read_proc_attr(const char *attr
, char *value
, size_t size
)
26 len
= strlen(PROCATTR
) + strlen(attr
) + 1;
27 path
= calloc(len
, 1);
30 sprintf(path
, "%s%s", PROCATTR
, attr
);
32 fd
= open(path
, O_RDONLY
);
37 len
= read(fd
, value
, size
);
41 /* Ensure value is terminated */
42 if (len
<= 0 || len
== size
)
46 path
= strchr(value
, '\n');
53 int read_sysfs_lsms(char *lsms
, size_t size
)
58 fp
= fopen("/sys/kernel/security/lsm", "r");
61 red
= fread(lsms
, 1, size
, fp
);
64 if (red
<= 0 || red
== size
)
70 int attr_lsm_count(void)
72 char *names
= calloc(sysconf(_SC_PAGESIZE
), 1);
78 if (read_sysfs_lsms(names
, sysconf(_SC_PAGESIZE
)))
81 if (strstr(names
, "selinux"))
83 if (strstr(names
, "smack"))
85 if (strstr(names
, "apparmor"))