Skip to content

Commit 56974a6

Browse files
committed
apparmor: add base infastructure for socket mediation
version 2 - Force an abi break. Network mediation will only be available in v8 abi complaint policy. Provide a basic mediation of sockets. This is not a full net mediation but just whether a spcific family of socket can be used by an application, along with setting up some basic infrastructure for network mediation to follow. the user space rule hav the basic form of NETWORK RULE = [ QUALIFIERS ] 'network' [ DOMAIN ] [ TYPE | PROTOCOL ] DOMAIN = ( 'inet' | 'ax25' | 'ipx' | 'appletalk' | 'netrom' | 'bridge' | 'atmpvc' | 'x25' | 'inet6' | 'rose' | 'netbeui' | 'security' | 'key' | 'packet' | 'ash' | 'econet' | 'atmsvc' | 'sna' | 'irda' | 'pppox' | 'wanpipe' | 'bluetooth' | 'netlink' | 'unix' | 'rds' | 'llc' | 'can' | 'tipc' | 'iucv' | 'rxrpc' | 'isdn' | 'phonet' | 'ieee802154' | 'caif' | 'alg' | 'nfc' | 'vsock' | 'mpls' | 'ib' | 'kcm' ) ',' TYPE = ( 'stream' | 'dgram' | 'seqpacket' | 'rdm' | 'raw' | 'packet' ) PROTOCOL = ( 'tcp' | 'udp' | 'icmp' ) eg. network, network inet, Signed-off-by: John Johansen <[email protected]> Acked-by: Seth Arnold <[email protected]>
1 parent 21f6066 commit 56974a6

File tree

13 files changed

+786
-8
lines changed

13 files changed

+786
-8
lines changed

security/apparmor/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#
22
# Generated include files
33
#
4+
net_names.h
45
capability_names.h
56
rlim_names.h

security/apparmor/Makefile

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,44 @@ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor.o
55

66
apparmor-y := apparmorfs.o audit.o capability.o task.o ipc.o lib.o match.o \
77
path.o domain.o policy.o policy_unpack.o procattr.o lsm.o \
8-
resource.o secid.o file.o policy_ns.o label.o mount.o
8+
resource.o secid.o file.o policy_ns.o label.o mount.o net.o
99
apparmor-$(CONFIG_SECURITY_APPARMOR_HASH) += crypto.o
1010

11-
clean-files := capability_names.h rlim_names.h
11+
clean-files := capability_names.h rlim_names.h net_names.h
1212

13+
# Build a lower case string table of address family names
14+
# Transform lines from
15+
# #define AF_LOCAL 1 /* POSIX name for AF_UNIX */
16+
# #define AF_INET 2 /* Internet IP Protocol */
17+
# to
18+
# [1] = "local",
19+
# [2] = "inet",
20+
#
21+
# and build the securityfs entries for the mapping.
22+
# Transforms lines from
23+
# #define AF_INET 2 /* Internet IP Protocol */
24+
# to
25+
# #define AA_SFS_AF_MASK "local inet"
26+
quiet_cmd_make-af = GEN $@
27+
cmd_make-af = echo "static const char *address_family_names[] = {" > $@ ;\
28+
sed $< >>$@ -r -n -e "/AF_MAX/d" -e "/AF_LOCAL/d" -e "/AF_ROUTE/d" -e \
29+
's/^\#define[ \t]+AF_([A-Z0-9_]+)[ \t]+([0-9]+)(.*)/[\2] = "\L\1",/p';\
30+
echo "};" >> $@ ;\
31+
printf '%s' '\#define AA_SFS_AF_MASK "' >> $@ ;\
32+
sed -r -n -e "/AF_MAX/d" -e "/AF_LOCAL/d" -e "/AF_ROUTE/d" -e \
33+
's/^\#define[ \t]+AF_([A-Z0-9_]+)[ \t]+([0-9]+)(.*)/\L\1/p'\
34+
$< | tr '\n' ' ' | sed -e 's/ $$/"\n/' >> $@
35+
36+
# Build a lower case string table of sock type names
37+
# Transform lines from
38+
# SOCK_STREAM = 1,
39+
# to
40+
# [1] = "stream",
41+
quiet_cmd_make-sock = GEN $@
42+
cmd_make-sock = echo "static const char *sock_type_names[] = {" >> $@ ;\
43+
sed $^ >>$@ -r -n \
44+
-e 's/^\tSOCK_([A-Z0-9_]+)[\t]+=[ \t]+([0-9]+)(.*)/[\2] = "\L\1",/p';\
45+
echo "};" >> $@
1346

1447
# Build a lower case string table of capability names
1548
# Transforms lines from
@@ -62,10 +95,16 @@ cmd_make-rlim = echo "static const char *const rlim_names[RLIM_NLIMITS] = {" \
6295
tr '\n' ' ' | sed -e 's/ $$/"\n/' >> $@
6396

6497
$(obj)/capability.o : $(obj)/capability_names.h
98+
$(obj)/net.o : $(obj)/net_names.h
6599
$(obj)/resource.o : $(obj)/rlim_names.h
66100
$(obj)/capability_names.h : $(srctree)/include/uapi/linux/capability.h \
67101
$(src)/Makefile
68102
$(call cmd,make-caps)
69103
$(obj)/rlim_names.h : $(srctree)/include/uapi/asm-generic/resource.h \
70104
$(src)/Makefile
71105
$(call cmd,make-rlim)
106+
$(obj)/net_names.h : $(srctree)/include/linux/socket.h \
107+
$(srctree)/include/linux/net.h \
108+
$(src)/Makefile
109+
$(call cmd,make-af)
110+
$(call cmd,make-sock)

security/apparmor/apparmorfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,7 @@ static struct aa_sfs_entry aa_sfs_entry_versions[] = {
21692169
AA_SFS_FILE_BOOLEAN("v5", 1),
21702170
AA_SFS_FILE_BOOLEAN("v6", 1),
21712171
AA_SFS_FILE_BOOLEAN("v7", 1),
2172+
AA_SFS_FILE_BOOLEAN("v8", 1),
21722173
{ }
21732174
};
21742175

@@ -2204,6 +2205,7 @@ static struct aa_sfs_entry aa_sfs_entry_features[] = {
22042205
AA_SFS_DIR("policy", aa_sfs_entry_policy),
22052206
AA_SFS_DIR("domain", aa_sfs_entry_domain),
22062207
AA_SFS_DIR("file", aa_sfs_entry_file),
2208+
AA_SFS_DIR("network_v8", aa_sfs_entry_network),
22072209
AA_SFS_DIR("mount", aa_sfs_entry_mount),
22082210
AA_SFS_DIR("namespaces", aa_sfs_entry_ns),
22092211
AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),

security/apparmor/file.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "include/cred.h"
2222
#include "include/file.h"
2323
#include "include/match.h"
24+
#include "include/net.h"
2425
#include "include/path.h"
2526
#include "include/policy.h"
2627
#include "include/label.h"
@@ -560,6 +561,32 @@ static int __file_path_perm(const char *op, struct aa_label *label,
560561
return error;
561562
}
562563

564+
static int __file_sock_perm(const char *op, struct aa_label *label,
565+
struct aa_label *flabel, struct file *file,
566+
u32 request, u32 denied)
567+
{
568+
struct socket *sock = (struct socket *) file->private_data;
569+
int error;
570+
571+
AA_BUG(!sock);
572+
573+
/* revalidation due to label out of date. No revocation at this time */
574+
if (!denied && aa_label_is_subset(flabel, label))
575+
return 0;
576+
577+
/* TODO: improve to skip profiles cached in flabel */
578+
error = aa_sock_file_perm(label, op, request, sock);
579+
if (denied) {
580+
/* TODO: improve to skip profiles checked above */
581+
/* check every profile in file label to is cached */
582+
last_error(error, aa_sock_file_perm(flabel, op, request, sock));
583+
}
584+
if (!error)
585+
update_file_ctx(file_ctx(file), label, request);
586+
587+
return error;
588+
}
589+
563590
/**
564591
* aa_file_perm - do permission revalidation check & audit for @file
565592
* @op: operation being checked
@@ -604,6 +631,9 @@ int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
604631
error = __file_path_perm(op, label, flabel, file, request,
605632
denied);
606633

634+
else if (S_ISSOCK(file_inode(file)->i_mode))
635+
error = __file_sock_perm(op, label, flabel, file, request,
636+
denied);
607637
done:
608638
rcu_read_unlock();
609639

security/apparmor/include/apparmor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
#define AA_CLASS_UNKNOWN 1
2525
#define AA_CLASS_FILE 2
2626
#define AA_CLASS_CAP 3
27-
#define AA_CLASS_NET 4
27+
#define AA_CLASS_DEPRECATED 4
2828
#define AA_CLASS_RLIMITS 5
2929
#define AA_CLASS_DOMAIN 6
3030
#define AA_CLASS_MOUNT 7
3131
#define AA_CLASS_PTRACE 9
3232
#define AA_CLASS_SIGNAL 10
33+
#define AA_CLASS_NET 14
3334
#define AA_CLASS_LABEL 16
3435

3536
#define AA_CLASS_LAST AA_CLASS_LABEL

security/apparmor/include/audit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ struct apparmor_audit_data {
134134
int signal;
135135
int unmappedsig;
136136
};
137+
struct {
138+
int type, protocol;
139+
struct sock *peer_sk;
140+
void *addr;
141+
int addrlen;
142+
} net;
137143
};
138144
};
139145
struct {

security/apparmor/include/net.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* AppArmor security module
3+
*
4+
* This file contains AppArmor network mediation definitions.
5+
*
6+
* Copyright (C) 1998-2008 Novell/SUSE
7+
* Copyright 2009-2017 Canonical Ltd.
8+
*
9+
* This program is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, version 2 of the
12+
* License.
13+
*/
14+
15+
#ifndef __AA_NET_H
16+
#define __AA_NET_H
17+
18+
#include <net/sock.h>
19+
#include <linux/path.h>
20+
21+
#include "apparmorfs.h"
22+
#include "label.h"
23+
#include "perms.h"
24+
#include "policy.h"
25+
26+
#define AA_MAY_SEND AA_MAY_WRITE
27+
#define AA_MAY_RECEIVE AA_MAY_READ
28+
29+
#define AA_MAY_SHUTDOWN AA_MAY_DELETE
30+
31+
#define AA_MAY_CONNECT AA_MAY_OPEN
32+
#define AA_MAY_ACCEPT 0x00100000
33+
34+
#define AA_MAY_BIND 0x00200000
35+
#define AA_MAY_LISTEN 0x00400000
36+
37+
#define AA_MAY_SETOPT 0x01000000
38+
#define AA_MAY_GETOPT 0x02000000
39+
40+
#define NET_PERMS_MASK (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CREATE | \
41+
AA_MAY_SHUTDOWN | AA_MAY_BIND | AA_MAY_LISTEN | \
42+
AA_MAY_CONNECT | AA_MAY_ACCEPT | AA_MAY_SETATTR | \
43+
AA_MAY_GETATTR | AA_MAY_SETOPT | AA_MAY_GETOPT)
44+
45+
#define NET_FS_PERMS (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CREATE | \
46+
AA_MAY_SHUTDOWN | AA_MAY_CONNECT | AA_MAY_RENAME |\
47+
AA_MAY_SETATTR | AA_MAY_GETATTR | AA_MAY_CHMOD | \
48+
AA_MAY_CHOWN | AA_MAY_CHGRP | AA_MAY_LOCK | \
49+
AA_MAY_MPROT)
50+
51+
#define NET_PEER_MASK (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CONNECT | \
52+
AA_MAY_ACCEPT)
53+
struct aa_sk_ctx {
54+
struct aa_label *label;
55+
struct aa_label *peer;
56+
};
57+
58+
#define SK_CTX(X) ((X)->sk_security)
59+
#define SOCK_ctx(X) SOCK_INODE(X)->i_security
60+
#define DEFINE_AUDIT_NET(NAME, OP, SK, F, T, P) \
61+
struct lsm_network_audit NAME ## _net = { .sk = (SK), \
62+
.family = (F)}; \
63+
DEFINE_AUDIT_DATA(NAME, \
64+
((SK) && (F) != AF_UNIX) ? LSM_AUDIT_DATA_NET : \
65+
LSM_AUDIT_DATA_NONE, \
66+
OP); \
67+
NAME.u.net = &(NAME ## _net); \
68+
aad(&NAME)->net.type = (T); \
69+
aad(&NAME)->net.protocol = (P)
70+
71+
#define DEFINE_AUDIT_SK(NAME, OP, SK) \
72+
DEFINE_AUDIT_NET(NAME, OP, SK, (SK)->sk_family, (SK)->sk_type, \
73+
(SK)->sk_protocol)
74+
75+
76+
#define af_select(FAMILY, FN, DEF_FN) \
77+
({ \
78+
int __e; \
79+
switch ((FAMILY)) { \
80+
default: \
81+
__e = DEF_FN; \
82+
} \
83+
__e; \
84+
})
85+
86+
extern struct aa_sfs_entry aa_sfs_entry_network[];
87+
88+
void audit_net_cb(struct audit_buffer *ab, void *va);
89+
int aa_profile_af_perm(struct aa_profile *profile, struct common_audit_data *sa,
90+
u32 request, u16 family, int type);
91+
int aa_af_perm(struct aa_label *label, const char *op, u32 request, u16 family,
92+
int type, int protocol);
93+
static inline int aa_profile_af_sk_perm(struct aa_profile *profile,
94+
struct common_audit_data *sa,
95+
u32 request,
96+
struct sock *sk)
97+
{
98+
return aa_profile_af_perm(profile, sa, request, sk->sk_family,
99+
sk->sk_type);
100+
}
101+
int aa_sk_perm(const char *op, u32 request, struct sock *sk);
102+
103+
int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request,
104+
struct socket *sock);
105+
106+
#endif /* __AA_NET_H */

security/apparmor/include/perms.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ extern struct aa_perms allperms;
138138

139139

140140
void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask);
141-
void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask);
141+
void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
142+
u32 mask);
142143
void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
143-
u32 chrsmask, const char **names, u32 namesmask);
144+
u32 chrsmask, const char * const *names, u32 namesmask);
144145
void aa_apply_modes_to_perms(struct aa_profile *profile,
145146
struct aa_perms *perms);
146147
void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,

security/apparmor/include/policy.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "file.h"
3131
#include "lib.h"
3232
#include "label.h"
33+
#include "net.h"
3334
#include "perms.h"
3435
#include "resource.h"
3536

@@ -224,6 +225,16 @@ static inline unsigned int PROFILE_MEDIATES_SAFE(struct aa_profile *profile,
224225
return 0;
225226
}
226227

228+
static inline unsigned int PROFILE_MEDIATES_AF(struct aa_profile *profile,
229+
u16 AF) {
230+
unsigned int state = PROFILE_MEDIATES(profile, AA_CLASS_NET);
231+
__be16 be_af = cpu_to_be16(AF);
232+
233+
if (!state)
234+
return 0;
235+
return aa_dfa_match_len(profile->policy.dfa, state, (char *) &be_af, 2);
236+
}
237+
227238
/**
228239
* aa_get_profile - increment refcount on profile @p
229240
* @p: profile (MAYBE NULL)

security/apparmor/lib.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask)
211211
*str = '\0';
212212
}
213213

214-
void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask)
214+
void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names,
215+
u32 mask)
215216
{
216217
const char *fmt = "%s";
217218
unsigned int i, perm = 1;
@@ -229,7 +230,7 @@ void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask)
229230
}
230231

231232
void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs,
232-
u32 chrsmask, const char **names, u32 namesmask)
233+
u32 chrsmask, const char * const *names, u32 namesmask)
233234
{
234235
char str[33];
235236

0 commit comments

Comments
 (0)