-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_landlock.c
218 lines (191 loc) · 4.7 KB
/
pam_landlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <security/pam_modules.h>
#include <security/pam_ext.h>
#include <syslog.h>
#include <stdbool.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "prelude.h"
#include "parse.h"
#include "landlock.h"
INTERNAL pam_handle_t *pamhandle;
struct args {
char* config_path;
bool allow_privs;
bool explicit_user;
char* user;
#ifdef AS_EXECUTABLE
char** command;
#endif
};
static int parse_args(struct args* a, int argc, char** argv) {
for(;;) {
static struct option long_options[] = {
#ifdef AS_EXECUTABLE
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
#endif
{"configfile", required_argument, 0, 'c'},
{"allow-privs", no_argument, 0, 'p'},
{"for-user", required_argument, 0, 'u'},
{0,0,0,0}
};
int choice = getopt_long( argc, argv, ":"
#ifdef AS_EXECUTABLE
"hv"
#endif
"c:pu:", long_options, NULL);
if (choice == -1)
break;
switch(choice) {
#ifdef AS_EXECUTABLE
case 'v':
puts("pam_landlock 0.1\nBuilt on " __DATE__ ", " __TIME__);
exit(EXIT_SUCCESS);
case 'h':
printf ("Usage: %s [options] [--] command\n\n"
"Options:\n"
" -h --help Print this help message and exit\n"
" -c --configfile=<path> Configuration file. Default is /etc/security/landlock.conf\n"
" -p --allow-privs Allow subsequent command to execute setUID/setGID executables (know the risks!)\n"
" -u --for-user=<user> Apply the rules as you would for user <user>. <user> be a name or numeric id\n"
" -v --version Print the version and exit\n"
, argv[0] ?: "pam_landlock");
exit(EXIT_SUCCESS);
break;
#endif
case 'c':
a->config_path = optarg;
break;
case 'p':
a->allow_privs = true;
break;
case 'u':
a->explicit_user = true;
a->user = optarg;
break;
case ':':
// FIXME how is a long option passed into the int optopt?
log_err("Option %c requires an argument", optopt);
exit(EXIT_FAILURE);
break;
case '?':
// FIXME how is a long option passed into the int optopt?
log_err("Unknown option %c", optopt);
exit(EXIT_FAILURE);
break;
default:
exit(EXIT_FAILURE);
}
}
/* Deal with non-option arguments here */
if (optind < argc) {
#ifndef AS_EXECUTABLE
pam_syslog(pamhandle, LOG_WARNING, "Surplus arguments, ignored: %s", argv[optind]);
#else
a->command = argv + optind;
} else {
log_err("Missing command");
exit(EXIT_FAILURE);
#endif
}
if (!a->config_path)
a->config_path = "/etc/security/landlock.conf";
return 0;
}
static int parse_user(const char* name, uid_t *user) {
if ((*name >= '0' && *name <= '9') || *name == '-') {
char *endptr;
long long r = strtoll(name, &endptr, 0);
if(*endptr) {
log_err("UID \"%s\" is not a valid integer", name);
return -1;
}
if (r < 0 || r > 0xFFFFFFFF) {
log_err("UID %lld is out of range", r);
return -1;
}
*user = (uid_t) r;
} else {
errno = 0;
struct passwd* pw = getpwnam(name);
if (!pw) {
log_err("Could not find user %s: %s", name, errno ? strerror(errno) : "No such user");
return -1;
}
*user = pw->pw_uid;
}
return 0;
}
static int doit(struct args* a, uid_t user) {
int r = -1;
if (a->explicit_user) {
r = parse_user(a->user, &user);
if (r)
return r;
}
char* configdata = read_file(a->config_path);
if (!configdata)
return -1;
struct landlock_rule** rules = parse_file(configdata, a->config_path);
if (!rules) {
r = -1;
goto out_free;
}
//debug_ruleset(rules);
r = restrict_to_ruleset(rules, a->allow_privs, user);
if (r)
goto out_cleanup;
r = 0;
out_cleanup:
cleanup_ruleset(rules);
out_free:
free(configdata);
return r;
}
#if 1
#ifdef AS_EXECUTABLE
int main(int argc, char** argv) {
struct args a = {0};
int r = parse_args(&a, argc, argv);
if (r)
return r;
r = doit(&a, getuid());
if (r)
return r;
execvp(*a.command, a.command);
fprintf(stderr, "Error: Could not exec %s: %s\n", *a.command, strerror(errno));
}
#else
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
(void) flags;
pamhandle = pamh;
struct args a = {0};
int r = parse_args(&a, argc, (char**) argv);
if (r)
return PAM_SESSION_ERR;
uid_t uid = -1;
if (!a.explicit_user) {
const char* user = NULL;
r = pam_get_user(pamh, &user, NULL);
if (r)
return r;
r = parse_user(user, &uid);
if (r)
return r;
}
r = doit(&a, uid);
if (r)
return PAM_SESSION_ERR;
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char** argv) {
(void) pamh, (void) flags, (void) argc, (void) argv;
return PAM_SUCCESS;
}
#endif
#endif