1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Linux Security Module infrastructure tests
4 *
5 * Copyright © 2023 Casey Schaufler <casey@schaufler-ca.com>
6 */
7
8 #define _GNU_SOURCE
9 #include <linux/lsm.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include "common.h"
17
18 #define PROCATTR "/proc/self/attr/"
19
read_proc_attr(const char * attr,char * value,size_t size)20 int read_proc_attr(const char *attr, char *value, size_t size)
21 {
22 int fd;
23 int len;
24 char *path;
25
26 len = strlen(PROCATTR) + strlen(attr) + 1;
27 path = calloc(len, 1);
28 if (path == NULL)
29 return -1;
30 sprintf(path, "%s%s", PROCATTR, attr);
31
32 fd = open(path, O_RDONLY);
33 free(path);
34
35 if (fd < 0)
36 return -1;
37 len = read(fd, value, size);
38
39 close(fd);
40
41 /* Ensure value is terminated */
42 if (len <= 0 || len == size)
43 return -1;
44 value[len] = '\0';
45
46 path = strchr(value, '\n');
47 if (path)
48 *path = '\0';
49
50 return 0;
51 }
52
read_sysfs_lsms(char * lsms,size_t size)53 int read_sysfs_lsms(char *lsms, size_t size)
54 {
55 FILE *fp;
56 size_t red;
57
58 fp = fopen("/sys/kernel/security/lsm", "r");
59 if (fp == NULL)
60 return -1;
61 red = fread(lsms, 1, size, fp);
62 fclose(fp);
63
64 if (red <= 0 || red == size)
65 return -1;
66 lsms[red] = '\0';
67 return 0;
68 }
69
attr_lsm_count(void)70 int attr_lsm_count(void)
71 {
72 char *names = calloc(sysconf(_SC_PAGESIZE), 1);
73 int count = 0;
74
75 if (!names)
76 return 0;
77
78 if (read_sysfs_lsms(names, sysconf(_SC_PAGESIZE)))
79 return 0;
80
81 if (strstr(names, "selinux"))
82 count++;
83 if (strstr(names, "smack"))
84 count++;
85 if (strstr(names, "apparmor"))
86 count++;
87
88 return count;
89 }
90