libxt_owner.c 16.3 KB
Newer Older
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
/*
 *	libxt_owner - iptables addon for xt_owner
 *
 *	Copyright © CC Computer Consultants GmbH, 2007 - 2008
 *	Jan Engelhardt <jengelh@computergmbh.de>
 */
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <limits.h>
#include <xtables.h>
#include <linux/netfilter/xt_owner.h>

/* match and invert flags */
enum {
	IPT_OWNER_UID   = 0x01,
	IPT_OWNER_GID   = 0x02,
	IPT_OWNER_PID   = 0x04,
	IPT_OWNER_SID   = 0x08,
	IPT_OWNER_COMM  = 0x10,
	IP6T_OWNER_UID  = IPT_OWNER_UID,
	IP6T_OWNER_GID  = IPT_OWNER_GID,
	IP6T_OWNER_PID  = IPT_OWNER_PID,
	IP6T_OWNER_SID  = IPT_OWNER_SID,
	IP6T_OWNER_COMM = IPT_OWNER_COMM,
};

struct ipt_owner_info {
	uid_t uid;
	gid_t gid;
	pid_t pid;
	pid_t sid;
	char comm[16];
	uint8_t match, invert;	/* flags */
};

struct ip6t_owner_info {
	uid_t uid;
	gid_t gid;
	pid_t pid;
	pid_t sid;
	char comm[16];
	uint8_t match, invert;	/* flags */
};

/*
 *	Note: "UINT32_MAX - 1" is used in the code because -1 is a reserved
 *	UID/GID value anyway.
 */

enum {
	O_USER = 0,
	O_GROUP,
	O_SOCK_EXISTS,
	O_PROCESS,
	O_SESSION,
	O_COMM,
59
	O_SUPPL_GROUPS,
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
};

static void owner_mt_help_v0(void)
{
	printf(
"owner match options:\n"
"[!] --uid-owner userid       Match local UID\n"
"[!] --gid-owner groupid      Match local GID\n"
"[!] --pid-owner processid    Match local PID\n"
"[!] --sid-owner sessionid    Match local SID\n"
"[!] --cmd-owner name         Match local command name\n"
"NOTE: PID, SID and command matching are broken on SMP\n");
}

static void owner_mt6_help_v0(void)
{
	printf(
"owner match options:\n"
"[!] --uid-owner userid       Match local UID\n"
"[!] --gid-owner groupid      Match local GID\n"
"[!] --pid-owner processid    Match local PID\n"
"[!] --sid-owner sessionid    Match local SID\n"
"NOTE: PID and SID matching are broken on SMP\n");
}

static void owner_mt_help(void)
{
	printf(
"owner match options:\n"
"[!] --uid-owner userid[-userid]      Match local UID\n"
"[!] --gid-owner groupid[-groupid]    Match local GID\n"
91
92
"[!] --socket-exists                  Match if socket exists\n"
"    --suppl-groups                   Also match supplementary groups set with --gid-owner\n");
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
}

#define s struct ipt_owner_info
static const struct xt_option_entry owner_mt_opts_v0[] = {
	{.name = "uid-owner", .id = O_USER, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "gid-owner", .id = O_GROUP, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "pid-owner", .id = O_PROCESS, .type = XTTYPE_UINT32,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, pid),
	 .max = INT_MAX},
	{.name = "sid-owner", .id = O_SESSION, .type = XTTYPE_UINT32,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, sid),
	 .max = INT_MAX},
	{.name = "cmd-owner", .id = O_COMM, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, comm)},
	XTOPT_TABLEEND,
};
#undef s

#define s struct ip6t_owner_info
static const struct xt_option_entry owner_mt6_opts_v0[] = {
	{.name = "uid-owner", .id = O_USER, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "gid-owner", .id = O_GROUP, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "pid-owner", .id = O_PROCESS, .type = XTTYPE_UINT32,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, pid),
	 .max = INT_MAX},
	{.name = "sid-owner", .id = O_SESSION, .type = XTTYPE_UINT32,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, sid),
	 .max = INT_MAX},
	XTOPT_TABLEEND,
};
#undef s

static const struct xt_option_entry owner_mt_opts[] = {
	{.name = "uid-owner", .id = O_USER, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "gid-owner", .id = O_GROUP, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "socket-exists", .id = O_SOCK_EXISTS, .type = XTTYPE_NONE,
	 .flags = XTOPT_INVERT},
136
	{.name = "suppl-groups", .id = O_SUPPL_GROUPS, .type = XTTYPE_NONE},
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
	XTOPT_TABLEEND,
};

static void owner_mt_parse_v0(struct xt_option_call *cb)
{
	struct ipt_owner_info *info = cb->data;
	struct passwd *pwd;
	struct group *grp;
	unsigned int id;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_USER:
		if ((pwd = getpwnam(cb->arg)) != NULL)
			id = pwd->pw_uid;
		else if (!xtables_strtoui(cb->arg, NULL, &id, 0, UINT32_MAX - 1))
			xtables_param_act(XTF_BAD_VALUE, "owner", "--uid-owner", cb->arg);
		if (cb->invert)
			info->invert |= IPT_OWNER_UID;
		info->match |= IPT_OWNER_UID;
		info->uid    = id;
		break;
	case O_GROUP:
		if ((grp = getgrnam(cb->arg)) != NULL)
			id = grp->gr_gid;
		else if (!xtables_strtoui(cb->arg, NULL, &id, 0, UINT32_MAX - 1))
			xtables_param_act(XTF_BAD_VALUE, "owner", "--gid-owner", cb->arg);
		if (cb->invert)
			info->invert |= IPT_OWNER_GID;
		info->match |= IPT_OWNER_GID;
		info->gid    = id;
		break;
	case O_PROCESS:
		if (cb->invert)
			info->invert |= IPT_OWNER_PID;
		info->match |= IPT_OWNER_PID;
		break;
	case O_SESSION:
		if (cb->invert)
			info->invert |= IPT_OWNER_SID;
		info->match |= IPT_OWNER_SID;
		break;
	case O_COMM:
		if (cb->invert)
			info->invert |= IPT_OWNER_COMM;
		info->match |= IPT_OWNER_COMM;
		break;
	}
}

static void owner_mt6_parse_v0(struct xt_option_call *cb)
{
	struct ip6t_owner_info *info = cb->data;
	struct passwd *pwd;
	struct group *grp;
	unsigned int id;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_USER:
		if ((pwd = getpwnam(cb->arg)) != NULL)
			id = pwd->pw_uid;
		else if (!xtables_strtoui(cb->arg, NULL, &id, 0, UINT32_MAX - 1))
			xtables_param_act(XTF_BAD_VALUE, "owner", "--uid-owner", cb->arg);
		if (cb->invert)
			info->invert |= IP6T_OWNER_UID;
		info->match |= IP6T_OWNER_UID;
		info->uid    = id;
		break;
	case O_GROUP:
		if ((grp = getgrnam(cb->arg)) != NULL)
			id = grp->gr_gid;
		else if (!xtables_strtoui(cb->arg, NULL, &id, 0, UINT32_MAX - 1))
			xtables_param_act(XTF_BAD_VALUE, "owner", "--gid-owner", cb->arg);
		if (cb->invert)
			info->invert |= IP6T_OWNER_GID;
		info->match |= IP6T_OWNER_GID;
		info->gid    = id;
		break;
	case O_PROCESS:
		if (cb->invert)
			info->invert |= IP6T_OWNER_PID;
		info->match |= IP6T_OWNER_PID;
		break;
	case O_SESSION:
		if (cb->invert)
			info->invert |= IP6T_OWNER_SID;
		info->match |= IP6T_OWNER_SID;
		break;
	}
}

static void owner_parse_range(const char *s, unsigned int *from,
                              unsigned int *to, const char *opt)
{
	char *end;

	/* -1 is reversed, so the max is one less than that. */
	if (!xtables_strtoui(s, &end, from, 0, UINT32_MAX - 1))
		xtables_param_act(XTF_BAD_VALUE, "owner", opt, s);
	*to = *from;
	if (*end == '-' || *end == ':')
		if (!xtables_strtoui(end + 1, &end, to, 0, UINT32_MAX - 1))
			xtables_param_act(XTF_BAD_VALUE, "owner", opt, s);
	if (*end != '\0')
		xtables_param_act(XTF_BAD_VALUE, "owner", opt, s);
}

static void owner_mt_parse(struct xt_option_call *cb)
{
	struct xt_owner_match_info *info = cb->data;
	struct passwd *pwd;
	struct group *grp;
	unsigned int from, to;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_USER:
		if ((pwd = getpwnam(cb->arg)) != NULL)
			from = to = pwd->pw_uid;
		else
			owner_parse_range(cb->arg, &from, &to, "--uid-owner");
		if (cb->invert)
			info->invert |= XT_OWNER_UID;
		info->match  |= XT_OWNER_UID;
		info->uid_min = from;
		info->uid_max = to;
		break;
	case O_GROUP:
		if ((grp = getgrnam(cb->arg)) != NULL)
			from = to = grp->gr_gid;
		else
			owner_parse_range(cb->arg, &from, &to, "--gid-owner");
		if (cb->invert)
			info->invert |= XT_OWNER_GID;
		info->match  |= XT_OWNER_GID;
		info->gid_min = from;
		info->gid_max = to;
		break;
	case O_SOCK_EXISTS:
		if (cb->invert)
			info->invert |= XT_OWNER_SOCKET;
		info->match |= XT_OWNER_SOCKET;
		break;
281
282
283
284
285
	case O_SUPPL_GROUPS:
		if (!(info->match & XT_OWNER_GID))
			xtables_param_act(XTF_BAD_VALUE, "owner", "--suppl-groups", "you need to use --gid-owner first");
		info->match |= XT_OWNER_SUPPL_GROUPS;
		break;
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
	}
}

static void owner_mt_check(struct xt_fcheck_call *cb)
{
	if (cb->xflags == 0)
		xtables_error(PARAMETER_PROBLEM, "owner: At least one of "
		           "--uid-owner, --gid-owner or --socket-exists "
		           "is required");
}

static void
owner_mt_print_item_v0(const struct ipt_owner_info *info, const char *label,
                       uint8_t flag, bool numeric)
{
	if (!(info->match & flag))
		return;
	if (info->invert & flag)
		printf(" !");
	printf(" %s", label);

	switch (info->match & flag) {
	case IPT_OWNER_UID:
		if (!numeric) {
			struct passwd *pwd = getpwuid(info->uid);

			if (pwd != NULL && pwd->pw_name != NULL) {
				printf(" %s", pwd->pw_name);
				break;
			}
		}
		printf(" %u", (unsigned int)info->uid);
		break;

	case IPT_OWNER_GID:
		if (!numeric) {
			struct group *grp = getgrgid(info->gid);

			if (grp != NULL && grp->gr_name != NULL) {
				printf(" %s", grp->gr_name);
				break;
			}
		}
		printf(" %u", (unsigned int)info->gid);
		break;

	case IPT_OWNER_PID:
		printf(" %u", (unsigned int)info->pid);
		break;

	case IPT_OWNER_SID:
		printf(" %u", (unsigned int)info->sid);
		break;

	case IPT_OWNER_COMM:
		printf(" %.*s", (int)sizeof(info->comm), info->comm);
		break;
	}
}

static void
owner_mt6_print_item_v0(const struct ip6t_owner_info *info, const char *label,
                        uint8_t flag, bool numeric)
{
	if (!(info->match & flag))
		return;
	if (info->invert & flag)
		printf(" !");
	printf(" %s", label);

	switch (info->match & flag) {
	case IP6T_OWNER_UID:
		if (!numeric) {
			struct passwd *pwd = getpwuid(info->uid);

			if (pwd != NULL && pwd->pw_name != NULL) {
				printf(" %s", pwd->pw_name);
				break;
			}
		}
		printf(" %u", (unsigned int)info->uid);
		break;

	case IP6T_OWNER_GID:
		if (!numeric) {
			struct group *grp = getgrgid(info->gid);

			if (grp != NULL && grp->gr_name != NULL) {
				printf(" %s", grp->gr_name);
				break;
			}
		}
		printf(" %u", (unsigned int)info->gid);
		break;

	case IP6T_OWNER_PID:
		printf(" %u", (unsigned int)info->pid);
		break;

	case IP6T_OWNER_SID:
		printf(" %u", (unsigned int)info->sid);
		break;
	}
}

static void
owner_mt_print_item(const struct xt_owner_match_info *info, const char *label,
                    uint8_t flag, bool numeric)
{
	if (!(info->match & flag))
		return;
	if (info->invert & flag)
		printf(" !");
	printf(" %s", label);

	switch (info->match & flag) {
	case XT_OWNER_UID:
		if (info->uid_min != info->uid_max) {
			printf(" %u-%u", (unsigned int)info->uid_min,
			       (unsigned int)info->uid_max);
			break;
		} else if (!numeric) {
			const struct passwd *pwd = getpwuid(info->uid_min);

			if (pwd != NULL && pwd->pw_name != NULL) {
				printf(" %s", pwd->pw_name);
				break;
			}
		}
		printf(" %u", (unsigned int)info->uid_min);
		break;

	case XT_OWNER_GID:
		if (info->gid_min != info->gid_max) {
			printf(" %u-%u", (unsigned int)info->gid_min,
			       (unsigned int)info->gid_max);
			break;
		} else if (!numeric) {
			const struct group *grp = getgrgid(info->gid_min);

			if (grp != NULL && grp->gr_name != NULL) {
				printf(" %s", grp->gr_name);
				break;
			}
		}
		printf(" %u", (unsigned int)info->gid_min);
		break;
	}
}

static void
owner_mt_print_v0(const void *ip, const struct xt_entry_match *match,
                  int numeric)
{
	const struct ipt_owner_info *info = (void *)match->data;

	owner_mt_print_item_v0(info, "owner UID match", IPT_OWNER_UID, numeric);
	owner_mt_print_item_v0(info, "owner GID match", IPT_OWNER_GID, numeric);
	owner_mt_print_item_v0(info, "owner PID match", IPT_OWNER_PID, numeric);
	owner_mt_print_item_v0(info, "owner SID match", IPT_OWNER_SID, numeric);
	owner_mt_print_item_v0(info, "owner CMD match", IPT_OWNER_COMM, numeric);
}

static void
owner_mt6_print_v0(const void *ip, const struct xt_entry_match *match,
                   int numeric)
{
	const struct ip6t_owner_info *info = (void *)match->data;

	owner_mt6_print_item_v0(info, "owner UID match", IPT_OWNER_UID, numeric);
	owner_mt6_print_item_v0(info, "owner GID match", IPT_OWNER_GID, numeric);
	owner_mt6_print_item_v0(info, "owner PID match", IPT_OWNER_PID, numeric);
	owner_mt6_print_item_v0(info, "owner SID match", IPT_OWNER_SID, numeric);
}

static void owner_mt_print(const void *ip, const struct xt_entry_match *match,
                           int numeric)
{
	const struct xt_owner_match_info *info = (void *)match->data;

466
467
468
469
	owner_mt_print_item(info, "owner socket exists", XT_OWNER_SOCKET,       numeric);
	owner_mt_print_item(info, "owner UID match",     XT_OWNER_UID,          numeric);
	owner_mt_print_item(info, "owner GID match",     XT_OWNER_GID,          numeric);
	owner_mt_print_item(info, "incl. suppl. groups", XT_OWNER_SUPPL_GROUPS, numeric);
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
}

static void
owner_mt_save_v0(const void *ip, const struct xt_entry_match *match)
{
	const struct ipt_owner_info *info = (void *)match->data;

	owner_mt_print_item_v0(info, "--uid-owner", IPT_OWNER_UID, true);
	owner_mt_print_item_v0(info, "--gid-owner", IPT_OWNER_GID, true);
	owner_mt_print_item_v0(info, "--pid-owner", IPT_OWNER_PID, true);
	owner_mt_print_item_v0(info, "--sid-owner", IPT_OWNER_SID, true);
	owner_mt_print_item_v0(info, "--cmd-owner", IPT_OWNER_COMM, true);
}

static void
owner_mt6_save_v0(const void *ip, const struct xt_entry_match *match)
{
	const struct ip6t_owner_info *info = (void *)match->data;

	owner_mt6_print_item_v0(info, "--uid-owner", IPT_OWNER_UID, true);
	owner_mt6_print_item_v0(info, "--gid-owner", IPT_OWNER_GID, true);
	owner_mt6_print_item_v0(info, "--pid-owner", IPT_OWNER_PID, true);
	owner_mt6_print_item_v0(info, "--sid-owner", IPT_OWNER_SID, true);
}

static void owner_mt_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_owner_match_info *info = (void *)match->data;

499
500
501
502
	owner_mt_print_item(info, "--socket-exists",  XT_OWNER_SOCKET,       true);
	owner_mt_print_item(info, "--uid-owner",      XT_OWNER_UID,          true);
	owner_mt_print_item(info, "--gid-owner",      XT_OWNER_GID,          true);
	owner_mt_print_item(info, "--suppl-groups",   XT_OWNER_SUPPL_GROUPS, true);
503
504
}

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
static int
owner_mt_print_uid_xlate(const struct xt_owner_match_info *info,
			 struct xt_xlate *xl)
{
	xt_xlate_add(xl, "skuid%s ", info->invert ? " !=" : "");

	if (info->uid_min != info->uid_max)
		xt_xlate_add(xl, "%u-%u", (unsigned int)info->uid_min,
			     (unsigned int)info->uid_max);
	else
		xt_xlate_add(xl, "%u", (unsigned int)info->uid_min);

	return 1;
}

static int
owner_mt_print_gid_xlate(const struct xt_owner_match_info *info,
			 struct xt_xlate *xl)
{
	xt_xlate_add(xl, "skgid%s ", info->invert ? " !=" : "");

	if (info->gid_min != info->gid_max)
		xt_xlate_add(xl, "%u-%u", (unsigned int)info->gid_min,
			     (unsigned int)info->gid_max);
	else
		xt_xlate_add(xl, "%u", (unsigned int)info->gid_min);

	return 1;
}

static int owner_mt_xlate(struct xt_xlate *xl,
			  const struct xt_xlate_mt_params *params)
{
	const struct xt_owner_match_info *info = (void *)params->match->data;
	int ret;

	switch (info->match) {
	case XT_OWNER_UID:
		ret = owner_mt_print_uid_xlate(info, xl);
		break;
	case XT_OWNER_GID:
		ret = owner_mt_print_gid_xlate(info, xl);
		break;
	default:
		ret = 0;
	}

	return ret;
}

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
static struct xtables_match owner_mt_reg[] = {
	{
		.version       = XTABLES_VERSION,
		.name          = "owner",
		.revision      = 0,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct ipt_owner_info)),
		.userspacesize = XT_ALIGN(sizeof(struct ipt_owner_info)),
		.help          = owner_mt_help_v0,
		.x6_parse      = owner_mt_parse_v0,
		.x6_fcheck     = owner_mt_check,
		.print         = owner_mt_print_v0,
		.save          = owner_mt_save_v0,
		.x6_options    = owner_mt_opts_v0,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "owner",
		.revision      = 0,
		.family        = NFPROTO_IPV6,
		.size          = XT_ALIGN(sizeof(struct ip6t_owner_info)),
		.userspacesize = XT_ALIGN(sizeof(struct ip6t_owner_info)),
		.help          = owner_mt6_help_v0,
		.x6_parse      = owner_mt6_parse_v0,
		.x6_fcheck     = owner_mt_check,
		.print         = owner_mt6_print_v0,
		.save          = owner_mt6_save_v0,
		.x6_options    = owner_mt6_opts_v0,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "owner",
		.revision      = 1,
		.family        = NFPROTO_UNSPEC,
		.size          = XT_ALIGN(sizeof(struct xt_owner_match_info)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_owner_match_info)),
		.help          = owner_mt_help,
		.x6_parse      = owner_mt_parse,
		.x6_fcheck     = owner_mt_check,
		.print         = owner_mt_print,
		.save          = owner_mt_save,
		.x6_options    = owner_mt_opts,
597
		.xlate	       = owner_mt_xlate,
598
599
600
601
602
603
604
	},
};

void _init(void)
{
	xtables_register_matches(owner_mt_reg, ARRAY_SIZE(owner_mt_reg));
}