-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0002-UBUNTU-SAUCE-no-up-apparmor-Sync-to-apparmor3-RC1-sn.patch
11644 lines (11191 loc) · 331 KB
/
0002-UBUNTU-SAUCE-no-up-apparmor-Sync-to-apparmor3-RC1-sn.patch
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
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
281
282
283
284
285
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
466
467
468
469
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
499
500
501
502
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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From ba9ec6a8686c99f38285e19f356c7667a10f40c9 Mon Sep 17 00:00:00 2001
From: John Johansen <[email protected]>
Date: Wed, 10 Jun 2015 03:43:23 -0700
Subject: [PATCH 02/10] UBUNTU: SAUCE: (no-up) apparmor: Sync to apparmor3 -
RC1 snapshot
This is a sync and squash to the apparmor 3 RC 1 development snapshot.
The set of patches in this squash are available at the apparmor-3.RC1
tag in git://kernel.ubuntu.com/jj/ubuntu-utopic.git.
This cleans up several functions over the alpha6 sync, and includes
multiple bug fixes. In addition it picks up
- new network mediation
- fine grained mediation of all unix socket types
BugLink: http://bugs.launchpad.net/bugs/1362199
Signed-off-by: John Johansen <[email protected]>
Signed-off-by: Tyler Hicks <[email protected]>
Signed-off-by: BirdZhang <[email protected]>
---
security/apparmor/Kconfig | 35 +
security/apparmor/Makefile | 43 +-
security/apparmor/af_unix.c | 632 ++++++++
security/apparmor/apparmorfs.c | 319 +++-
security/apparmor/audit.c | 75 +-
security/apparmor/capability.c | 52 +-
security/apparmor/context.c | 88 +-
security/apparmor/domain.c | 165 ++-
security/apparmor/file.c | 555 +++++--
security/apparmor/include/af_unix.h | 121 ++
security/apparmor/include/apparmor.h | 60 +-
security/apparmor/include/apparmorfs.h | 2 +
security/apparmor/include/audit.h | 72 +-
security/apparmor/include/capability.h | 6 +-
security/apparmor/include/context.h | 178 ++-
security/apparmor/include/domain.h | 2 +
security/apparmor/include/file.h | 92 +-
security/apparmor/include/ipc.h | 22 +-
security/apparmor/include/label.h | 419 ++++++
security/apparmor/include/match.h | 15 +
security/apparmor/include/mount.h | 54 +
security/apparmor/include/net.h | 110 ++
security/apparmor/include/path.h | 63 +-
security/apparmor/include/perms.h | 174 +++
security/apparmor/include/policy.h | 173 +--
security/apparmor/include/procattr.h | 2 +-
security/apparmor/include/resource.h | 4 +-
security/apparmor/include/sig_names.h | 95 ++
security/apparmor/ipc.c | 235 ++-
security/apparmor/label.c | 1844 ++++++++++++++++++++++++
security/apparmor/lib.c | 434 +++++-
security/apparmor/lsm.c | 815 +++++++++--
security/apparmor/mount.c | 703 +++++++++
security/apparmor/net.c | 397 +++++
security/apparmor/path.c | 175 +--
security/apparmor/policy.c | 409 ++++--
security/apparmor/policy_unpack.c | 125 +-
security/apparmor/procattr.c | 60 +-
security/apparmor/resource.c | 112 +-
39 files changed, 7823 insertions(+), 1114 deletions(-)
create mode 100644 security/apparmor/af_unix.c
create mode 100644 security/apparmor/include/af_unix.h
create mode 100644 security/apparmor/include/label.h
create mode 100644 security/apparmor/include/mount.h
create mode 100644 security/apparmor/include/net.h
create mode 100644 security/apparmor/include/perms.h
create mode 100644 security/apparmor/include/sig_names.h
create mode 100644 security/apparmor/label.c
create mode 100644 security/apparmor/mount.c
create mode 100644 security/apparmor/net.c
diff --git a/security/apparmor/Kconfig b/security/apparmor/Kconfig
index d49c5396..a738fee2 100644
--- a/security/apparmor/Kconfig
+++ b/security/apparmor/Kconfig
@@ -30,6 +30,41 @@ config SECURITY_APPARMOR_BOOTPARAM_VALUE
If you are unsure how to answer this question, answer 1.
+config SECURITY_APPARMOR_STATS
+ bool "enable debug statistics"
+ depends on SECURITY_APPARMOR
+ select APPARMOR_LABEL_STATS
+ default n
+ help
+ This enables keeping statistics on various internal structures
+ and functions in apparmor.
+
+ If you are unsure how to answer this question, answer N.
+
+config SECURITY_APPARMOR_UNCONFINED_INIT
+ bool "Set init to unconfined on boot"
+ depends on SECURITY_APPARMOR
+ default y
+ help
+ This option determines policy behavior during early boot by
+ placing the init process in the unconfined state, or the
+ 'default' profile.
+
+ This option determines policy behavior during early boot by
+ placing the init process in the unconfined state, or the
+ 'default' profile.
+
+ 'Y' means init and its children are not confined, unless the
+ init process is re-execed after a policy load; loaded policy
+ will only apply to processes started after the load.
+
+ 'N' means init and its children are confined in a profile
+ named 'default', which can be replaced later and thus
+ provide for confinement for processes started early at boot,
+ though not confined during early boot.
+
+ If you are unsure how to answer this question, answer Y.
+
config SECURITY_APPARMOR_HASH
bool "SHA1 hash of loaded profiles"
depends on SECURITY_APPARMOR
diff --git a/security/apparmor/Makefile b/security/apparmor/Makefile
index d693df87..69ce26e2 100644
--- a/security/apparmor/Makefile
+++ b/security/apparmor/Makefile
@@ -4,11 +4,44 @@ obj-$(CONFIG_SECURITY_APPARMOR) += apparmor.o
apparmor-y := apparmorfs.o audit.o capability.o context.o ipc.o lib.o match.o \
path.o domain.o policy.o policy_unpack.o procattr.o lsm.o \
- resource.o sid.o file.o
+ resource.o sid.o file.o label.o mount.o net.o af_unix.o
apparmor-$(CONFIG_SECURITY_APPARMOR_HASH) += crypto.o
-clean-files := capability_names.h rlim_names.h
+clean-files := capability_names.h rlim_names.h net_names.h
+# Build a lower case string table of address family names
+# Transform lines from
+# define AF_LOCAL 1 /* POSIX name for AF_UNIX */
+# #define AF_INET 2 /* Internet IP Protocol */
+# to
+# [1] = "local",
+# [2] = "inet",
+#
+# and build the securityfs entries for the mapping.
+# Transforms lines from
+# #define AF_INET 2 /* Internet IP Protocol */
+# to
+# #define AA_FS_AF_MASK "local inet"
+quiet_cmd_make-af = GEN $@
+cmd_make-af = echo "static const char *address_family_names[] = {" > $@ ;\
+ sed $< >>$@ -r -n -e "/AF_MAX/d" -e "/AF_LOCAL/d" -e "/AF_ROUTE/d" -e \
+ 's/^\#define[ \t]+AF_([A-Z0-9_]+)[ \t]+([0-9]+)(.*)/[\2] = "\L\1",/p';\
+ echo "};" >> $@ ;\
+ echo -n '\#define AA_FS_AF_MASK "' >> $@ ;\
+ sed -r -n -e "/AF_MAX/d" -e "/AF_LOCAL/d" -e "/AF_ROUTE/d" -e \
+ 's/^\#define[ \t]+AF_([A-Z0-9_]+)[ \t]+([0-9]+)(.*)/\L\1/p'\
+ $< | tr '\n' ' ' | sed -e 's/ $$/"\n/' >> $@
+
+# Build a lower case string table of sock type names
+# Transform lines from
+# SOCK_STREAM = 1,
+# to
+# [1] = "stream",
+quiet_cmd_make-sock = GEN $@
+cmd_make-sock = echo "static const char *sock_type_names[] = {" >> $@ ;\
+ sed $^ >>$@ -r -n \
+ -e 's/^\tSOCK_([A-Z0-9_]+)[\t]+=[ \t]+([0-9]+)(.*)/[\2] = "\L\1",/p';\
+ echo "};" >> $@
# Build a lower case string table of capability names
# Transforms lines from
@@ -61,6 +94,7 @@ cmd_make-rlim = echo "static const char *const rlim_names[RLIM_NLIMITS] = {" \
tr '\n' ' ' | sed -e 's/ $$/"\n/' >> $@
$(obj)/capability.o : $(obj)/capability_names.h
+$(obj)/net.o : $(obj)/net_names.h
$(obj)/resource.o : $(obj)/rlim_names.h
$(obj)/capability_names.h : $(srctree)/include/uapi/linux/capability.h \
$(src)/Makefile
@@ -68,3 +102,8 @@ $(obj)/capability_names.h : $(srctree)/include/uapi/linux/capability.h \
$(obj)/rlim_names.h : $(srctree)/include/uapi/asm-generic/resource.h \
$(src)/Makefile
$(call cmd,make-rlim)
+$(obj)/net_names.h : $(srctree)/include/linux/socket.h \
+ $(srctree)/include/linux/net.h \
+ $(src)/Makefile
+ $(call cmd,make-af)
+ $(call cmd,make-sock)
diff --git a/security/apparmor/af_unix.c b/security/apparmor/af_unix.c
new file mode 100644
index 00000000..73bd94d0
--- /dev/null
+++ b/security/apparmor/af_unix.c
@@ -0,0 +1,632 @@
+/*
+ * AppArmor security module
+ *
+ * This file contains AppArmor af_unix fine grained mediation
+ *
+ * Copyright 2014 Canonical Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, version 2 of the
+ * License.
+ */
+
+#include <net/tcp_states.h>
+
+#include "include/af_unix.h"
+#include "include/apparmor.h"
+#include "include/context.h"
+#include "include/file.h"
+#include "include/label.h"
+#include "include/path.h"
+#include "include/policy.h"
+
+static inline int unix_fs_perm(int op, u32 mask, struct aa_label *label,
+ struct unix_sock *u, int flags)
+{
+ AA_BUG(!label);
+ AA_BUG(!u);
+ AA_BUG(!UNIX_FS(u));
+
+ if (unconfined(label) || !LABEL_MEDIATES(label, AA_CLASS_FILE))
+ return 0;
+
+ if (!u->path.dentry) {
+ struct path_cond cond = { };
+ struct file_perms perms = { };
+ struct aa_profile *profile;
+
+ /* socket path has been cleared because it is being shutdown */
+ /* TODO: fix flags */
+ if (!(flags & PATH_MEDIATE_DELETED))
+ return -EACCES;
+ /* Mediate at original socket location */
+ /* TODO: ns disconnected paths */
+ /* TODO: after switch to newer audit provide deleted/shutdown
+ * message as part of audit info
+ */
+ return fn_for_each_confined(label, profile,
+ __aa_path_perm(op, profile,
+ u->addr->name->sun_path,
+ mask, &cond, flags, &perms));
+ } else {
+ /* the sunpath may not be valid for this ns so use the path */
+ struct path_cond cond = { u->path.dentry->d_inode->i_uid,
+ u->path.dentry->d_inode->i_mode
+ };
+
+ return aa_path_perm(op, label, &u->path, flags, mask & NET_FS_PERMS,
+ &cond);
+ }
+
+ return 0;
+}
+
+/* passing in state returned by PROFILE_MEDIATES_AF */
+static unsigned int match_to_prot(struct aa_profile *profile,
+ unsigned int state, int type, int protocol,
+ const char **info)
+{
+ u16 buffer[2];
+ buffer[0] = cpu_to_be16(type);
+ buffer[1] = cpu_to_be16(protocol);
+ state = aa_dfa_match_len(profile->policy.dfa, state, (char *) &buffer,
+ 4);
+ if (!state)
+ *info = "failed type and protocol match";
+ return state;
+}
+
+static unsigned int match_addr(struct aa_profile *profile, unsigned int state,
+ struct sockaddr_un *addr, int addrlen)
+{
+ if (addr)
+ /* include leading \0 */
+ state = aa_dfa_match_len(profile->policy.dfa, state,
+ addr->sun_path,
+ unix_addr_len(addrlen));
+ else
+ /* anonymous end point */
+ state = aa_dfa_match_len(profile->policy.dfa, state, "\x01",
+ 1);
+ /* todo change to out of band */
+ state = aa_dfa_null_transition(profile->policy.dfa, state);
+ return state;
+}
+
+static unsigned int match_to_local(struct aa_profile *profile,
+ unsigned int state, int type, int protocol,
+ struct sockaddr_un *addr, int addrlen,
+ const char **info)
+{
+ state = match_to_prot(profile, state, type, protocol, info);
+ if (state) {
+ state = match_addr(profile, state, addr, addrlen);
+ if (state) {
+ /* todo: local label matching */
+ state = aa_dfa_null_transition(profile->policy.dfa,
+ state);
+ if (!state)
+ *info = "failed local label match";
+ } else
+ *info = "failed local address match";
+ }
+
+ return state;
+}
+
+static unsigned int match_to_sk(struct aa_profile *profile,
+ unsigned int state, struct unix_sock *u,
+ const char **info)
+{
+ struct sockaddr_un *addr = NULL;
+ int addrlen = 0;
+
+ if (u->addr) {
+ addr = u->addr->name;
+ addrlen = u->addr->len;
+ }
+
+ return match_to_local(profile, state, u->sk.sk_type, u->sk.sk_protocol,
+ addr, addrlen, info);
+}
+
+#define CMD_ADDR 1
+#define CMD_LISTEN 2
+#define CMD_OPT 4
+
+static inline unsigned int match_to_cmd(struct aa_profile *profile,
+ unsigned int state, struct unix_sock *u,
+ char cmd, const char **info)
+{
+ state = match_to_sk(profile, state, u, info);
+ if (state) {
+ state = aa_dfa_match_len(profile->policy.dfa, state, &cmd, 1);
+ if (!state)
+ *info = "failed cmd selection match";
+ }
+
+ return state;
+}
+
+static inline unsigned int match_to_peer(struct aa_profile *profile,
+ unsigned int state,
+ struct unix_sock *u,
+ struct sockaddr_un *peer_addr,
+ int peer_addrlen,
+ const char **info)
+{
+ state = match_to_cmd(profile, state, u, CMD_ADDR, info);
+ if (state) {
+ state = match_addr(profile, state, peer_addr, peer_addrlen);
+ if (!state)
+ *info = "failed peer address match";
+ }
+ return state;
+}
+
+static int do_perms(struct aa_profile *profile, unsigned int state, u32 request,
+ struct common_audit_data *sa)
+{
+ struct aa_perms perms;
+
+ AA_BUG(!profile);
+
+ aa_compute_perms(profile->policy.dfa, state, &perms);
+ aa_apply_modes_to_perms(profile, &perms);
+ return aa_check_perms(profile, &perms, request, sa,
+ audit_net_cb);
+}
+
+static int match_label(struct aa_profile *profile, struct aa_profile *peer,
+ unsigned int state, u32 request,
+ struct common_audit_data *sa)
+{
+ AA_BUG(!profile);
+ AA_BUG(!peer);
+
+ aad(sa)->target = aa_peer_name(peer);
+
+ if (state) {
+ state = aa_dfa_match(profile->policy.dfa, state, aa_peer_name(peer));
+ if (!state)
+ aad(sa)->info = "failed peer label match";
+ }
+ return do_perms(profile, state, request, sa);
+}
+
+
+/* unix sock creation comes before we know if the socket will be an fs
+ * socket
+ * v6 - semantics are handled by mapping in profile load
+ * v7 - semantics require sock create for tasks creating an fs socket.
+ */
+static int profile_create_perm(struct aa_profile *profile, int family,
+ int type, int protocol)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(profile_unconfined(profile));
+
+ if ((state = PROFILE_MEDIATES_AF(profile, AF_UNIX))) {
+ DEFINE_AUDIT_UNIX(sa, OP_CREATE, NULL, type, protocol);
+
+ state = match_to_prot(profile, state, type, protocol,
+ &aad(&sa)->info);
+ return do_perms(profile, state, AA_MAY_CREATE, &sa);
+ }
+
+ return aa_profile_af_perm(profile, OP_CREATE, family, type, protocol,
+ NULL);
+}
+
+int aa_unix_create_perm(struct aa_label *label, int family, int type,
+ int protocol)
+{
+ struct aa_profile *profile;
+
+ if (unconfined(label))
+ return 0;
+
+ return fn_for_each_confined(label, profile,
+ profile_create_perm(profile, family, type, protocol));
+}
+
+
+static inline int profile_sk_perm(struct aa_profile *profile, int op,
+ u32 request, struct sock *sk)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(!sk);
+ AA_BUG(UNIX_FS(sk));
+ AA_BUG(profile_unconfined(profile));
+
+ state = PROFILE_MEDIATES_AF(profile, AF_UNIX);
+ if (state) {
+ DEFINE_AUDIT_UNIX(sa, op, sk, sk->sk_type, sk->sk_protocol);
+
+ state = match_to_sk(profile, state, unix_sk(sk),
+ &aad(&sa)->info);
+ return do_perms(profile, state, request, &sa);
+ }
+
+ return aa_profile_af_perm(profile, op, sk->sk_family, sk->sk_type,
+ sk->sk_protocol, sk);
+}
+
+int aa_unix_label_sk_perm(struct aa_label *label, int op, u32 request,
+ struct sock *sk)
+{
+ struct aa_profile *profile;
+
+ return fn_for_each_confined(label, profile,
+ profile_sk_perm(profile, op, request, sk));
+}
+
+static int unix_label_sock_perm(struct aa_label *label, int op, u32 request,
+ struct socket *sock)
+{
+ if (unconfined(label))
+ return 0;
+ if (UNIX_FS(sock->sk))
+ return unix_fs_perm(op, request, label, unix_sk(sock->sk), 0);
+
+ return aa_unix_label_sk_perm(label, op, request, sock->sk);
+}
+
+/* revaliation, get/set attr */
+int aa_unix_sock_perm(int op, u32 request, struct socket *sock)
+{
+ return unix_label_sock_perm(aa_current_label(), op, request, sock);
+}
+
+static int profile_bind_perm(struct aa_profile *profile, struct sock *sk,
+ struct sockaddr *addr, int addrlen)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(!sk);
+ AA_BUG(addr->sa_family != AF_UNIX);
+ AA_BUG(profile_unconfined(profile));
+ AA_BUG(unix_addr_fs(addr, addrlen));
+
+ state = PROFILE_MEDIATES_AF(profile, AF_UNIX);
+ if (state) {
+ /* bind for abstract socket */
+ DEFINE_AUDIT_UNIX(sa, OP_BIND, sk, sk->sk_type,
+ sk->sk_protocol);
+ aad(&sa)->net.addr = unix_addr(addr);
+ aad(&sa)->net.addrlen = addrlen;
+
+ state = match_to_local(profile, state,
+ sk->sk_type, sk->sk_protocol,
+ unix_addr(addr), addrlen,
+ &aad(&sa)->info);
+ return do_perms(profile, state, AA_MAY_BIND, &sa);
+ }
+
+ return aa_profile_af_perm(profile, OP_BIND, sk->sk_family, sk->sk_type,
+ sk->sk_protocol, sk);
+}
+
+int aa_unix_bind_perm(struct socket *sock, struct sockaddr *address,
+ int addrlen)
+{
+ struct aa_profile *profile;
+ struct aa_label *label = aa_current_label();
+
+ /* fs bind is handled by mknod */
+ if (unconfined(label) || unix_addr_fs(address, addrlen))
+ return 0;
+
+ return fn_for_each_confined(label, profile,
+ profile_bind_perm(profile, sock->sk, address, addrlen));
+}
+
+int aa_unix_connect_perm(struct socket *sock, struct sockaddr *address,
+ int addrlen)
+{
+ /* unix connections are covered by the
+ * - unix_stream_connect (stream) and unix_may_send hooks (dgram)
+ * - fs connect is handled by open
+ */
+ return 0;
+}
+
+static int profile_listen_perm(struct aa_profile *profile, struct sock *sk,
+ int backlog)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(!sk);
+ AA_BUG(UNIX_FS(sk));
+ AA_BUG(profile_unconfined(profile));
+
+ state = PROFILE_MEDIATES_AF(profile, AF_UNIX);
+ if (state) {
+ u16 b = cpu_to_be16(backlog);
+ DEFINE_AUDIT_UNIX(sa, OP_LISTEN, sk, sk->sk_type,
+ sk->sk_protocol);
+
+ state = match_to_cmd(profile, state, unix_sk(sk), CMD_LISTEN,
+ &aad(&sa)->info);
+ if (state) {
+ state = aa_dfa_match_len(profile->policy.dfa, state,
+ (char *) &b, 2);
+ if (!state)
+ aad(&sa)->info = "failed listen backlog match";
+ }
+ return do_perms(profile, state, AA_MAY_LISTEN, &sa);
+ }
+
+ return aa_profile_af_perm(profile, OP_LISTEN, sk->sk_family,
+ sk->sk_type, sk->sk_protocol, sk);
+}
+
+int aa_unix_listen_perm(struct socket *sock, int backlog)
+{
+ struct aa_profile *profile;
+ struct aa_label *label = aa_current_label();
+
+ if (unconfined(label) || UNIX_FS(sock->sk))
+ return 0;
+
+ return fn_for_each_confined(label, profile,
+ profile_listen_perm(profile, sock->sk, backlog));
+}
+
+
+static inline int profile_accept_perm(struct aa_profile *profile,
+ struct sock *sk,
+ struct sock *newsk)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(!sk);
+ AA_BUG(UNIX_FS(sk));
+ AA_BUG(profile_unconfined(profile));
+
+ state = PROFILE_MEDIATES_AF(profile, AF_UNIX);
+ if (state) {
+ DEFINE_AUDIT_UNIX(sa, OP_ACCEPT, sk, sk->sk_type,
+ sk->sk_protocol);
+
+ state = match_to_sk(profile, state, unix_sk(sk),
+ &aad(&sa)->info);
+ return do_perms(profile, state, AA_MAY_ACCEPT, &sa);
+ }
+
+ return aa_profile_af_perm(profile, OP_ACCEPT, sk->sk_family,
+ sk->sk_type, sk->sk_protocol, sk);
+}
+
+/* ability of sock to connect, not peer address binding */
+int aa_unix_accept_perm(struct socket *sock, struct socket *newsock)
+{
+ struct aa_profile *profile;
+ struct aa_label *label = aa_current_label();
+
+ if (unconfined(label) || UNIX_FS(sock->sk))
+ return 0;
+
+ return fn_for_each_confined(label, profile,
+ profile_accept_perm(profile, sock->sk, newsock->sk));
+}
+
+
+/* dgram handled by unix_may_sendmsg, right to send on stream done at connect
+ * could do per msg unix_stream here
+ */
+/* sendmsg, recvmsg */
+int aa_unix_msg_perm(int op, u32 request, struct socket *sock,
+ struct msghdr *msg, int size)
+{
+ return 0;
+}
+
+
+static int profile_opt_perm(struct aa_profile *profile, int op, u32 request,
+ struct sock *sk, int level, int optname)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(!sk);
+ AA_BUG(UNIX_FS(sk));
+ AA_BUG(profile_unconfined(profile));
+
+ state = PROFILE_MEDIATES_AF(profile, AF_UNIX);
+ if (state) {
+ u16 b = cpu_to_be16(optname);
+ DEFINE_AUDIT_UNIX(sa, op, sk, sk->sk_type, sk->sk_protocol);
+
+ state = match_to_cmd(profile, state, unix_sk(sk), CMD_OPT,
+ &aad(&sa)->info);
+ if (state) {
+ state = aa_dfa_match_len(profile->policy.dfa, state,
+ (char *) &b, 2);
+ if (!state)
+ aad(&sa)->info = "failed sockopt match";
+ }
+ return do_perms(profile, state, request, &sa);
+ }
+
+ return aa_profile_af_perm(profile, op, sk->sk_family,
+ sk->sk_type, sk->sk_protocol, sk);
+}
+
+int aa_unix_opt_perm(int op, u32 request, struct socket *sock, int level,
+ int optname)
+{
+ struct aa_profile *profile;
+ struct aa_label *label = aa_current_label();
+
+ if (unconfined(label) || UNIX_FS(sock->sk))
+ return 0;
+
+ return fn_for_each_confined(label, profile,
+ profile_opt_perm(profile, op, request, sock->sk,
+ level, optname));
+}
+
+/* null peer_label is allowed, in which case the peer_sk label is used */
+static int profile_peer_perm(struct aa_profile *profile, int op, u32 request,
+ struct sock *sk, struct sock *peer_sk,
+ struct aa_label *peer_label,
+ struct common_audit_data *sa)
+{
+ unsigned int state;
+
+ AA_BUG(!profile);
+ AA_BUG(profile_unconfined(profile));
+ AA_BUG(!sk);
+ AA_BUG(!peer_sk);
+ AA_BUG(UNIX_FS(peer_sk));
+
+ state = PROFILE_MEDIATES_AF(profile, AF_UNIX);
+ if (state) {
+ struct aa_sk_cxt *peer_cxt = SK_CXT(peer_sk);
+ struct aa_profile *peerp;
+ struct sockaddr_un *addr = NULL;
+ int len = 0;
+ if (unix_sk(peer_sk)->addr) {
+ addr = unix_sk(peer_sk)->addr->name;
+ len = unix_sk(peer_sk)->addr->len;
+ }
+ state = match_to_peer(profile, state, unix_sk(sk),
+ addr, len, &aad(sa)->info);
+ if (!peer_label)
+ peer_label = peer_cxt->label;
+ return fn_for_each(peer_label, peerp,
+ match_label(profile, peerp, state, request,
+ sa));
+ }
+
+ return aa_profile_af_perm(profile, op, sk->sk_family, sk->sk_type,
+ sk->sk_protocol, sk);
+}
+
+/**
+ *
+ * Requires: lock held on both @sk and @peer_sk
+ */
+int aa_unix_peer_perm(struct aa_label *label, int op, u32 request,
+ struct sock *sk, struct sock *peer_sk,
+ struct aa_label *peer_label)
+{
+ struct unix_sock *peeru = unix_sk(peer_sk);
+ struct unix_sock *u = unix_sk(sk);
+
+ AA_BUG(!label);
+ AA_BUG(!sk);
+ AA_BUG(!peer_sk);
+
+ if (UNIX_FS(peeru))
+ return unix_fs_perm(op, request, label, peeru, 0);
+ else if (UNIX_FS(u))
+ return unix_fs_perm(op, request, label, u, 0);
+ else {
+ struct aa_profile *profile;
+ DEFINE_AUDIT_UNIX(sa, op, sk, sk->sk_type, sk->sk_protocol);
+ aad(&sa)->net.peer_sk = peer_sk;
+
+ /* TODO: ns!!! */
+ if (!net_eq(sock_net(sk), sock_net(peer_sk))) {
+ ;
+ }
+
+ if (unconfined(label))
+ return 0;
+
+ return fn_for_each_confined(label, profile,
+ profile_peer_perm(profile, op, request, sk,
+ peer_sk, peer_label, &sa));
+ }
+}
+
+
+/* from net/unix/af_unix.c */
+static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
+{
+ if (unlikely(sk1 == sk2) || !sk2) {
+ unix_state_lock(sk1);
+ return;
+ }
+ if (sk1 < sk2) {
+ unix_state_lock(sk1);
+ unix_state_lock_nested(sk2);
+ } else {
+ unix_state_lock(sk2);
+ unix_state_lock_nested(sk1);
+ }
+}
+
+static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
+{
+ if (unlikely(sk1 == sk2) || !sk2) {
+ unix_state_unlock(sk1);
+ return;
+ }
+ unix_state_unlock(sk1);
+ unix_state_unlock(sk2);
+}
+
+int aa_unix_file_perm(struct aa_label *label, int op, u32 request,
+ struct socket *sock)
+{
+ struct sock *peer_sk = NULL;
+ u32 sk_req = request & ~NET_PEER_MASK;
+ int error = 0;
+
+ AA_BUG(!label);
+ AA_BUG(!sock);
+ AA_BUG(!sock->sk);
+ AA_BUG(sock->sk->sk_family != AF_UNIX);
+
+ /* TODO: update sock label with new task label */
+ unix_state_lock(sock->sk);
+ peer_sk = unix_peer(sock->sk);
+ if (peer_sk)
+ sock_hold(peer_sk);
+ if (!unix_connected(sock) && sk_req) {
+ error = unix_label_sock_perm(label, op, sk_req, sock);
+ if (!error) {
+ // update label
+ }
+ }
+ unix_state_unlock(sock->sk);
+ if (!peer_sk)
+ return error;
+
+ unix_state_double_lock(sock->sk, peer_sk);
+ if (UNIX_FS(sock->sk)) {
+ error = unix_fs_perm(op, request, label, unix_sk(sock->sk),
+ PATH_SOCK_COND);
+ } else if (UNIX_FS(peer_sk)) {
+ error = unix_fs_perm(op, request, label, unix_sk(peer_sk),
+ PATH_SOCK_COND);
+ } else {
+ struct aa_sk_cxt *pcxt = SK_CXT(peer_sk);
+ if (sk_req)
+ error = aa_unix_label_sk_perm(label, op, sk_req,
+ sock->sk);
+ last_error(error,
+ xcheck(aa_unix_peer_perm(label, op,
+ MAY_READ | MAY_WRITE,
+ sock->sk, peer_sk, NULL),
+ aa_unix_peer_perm(pcxt->label, op,
+ MAY_READ | MAY_WRITE,
+ peer_sk, sock->sk, label)));
+ }
+
+ unix_state_double_unlock(sock->sk, peer_sk);
+ sock_put(peer_sk);
+
+ return error;
+}
diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c
index 7db9954f..f86f56e8 100644
--- a/security/apparmor/apparmorfs.c
+++ b/security/apparmor/apparmorfs.c
@@ -18,15 +18,18 @@
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
+#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/capability.h>
#include <linux/rcupdate.h>
+#include <uapi/linux/major.h>
#include "include/apparmor.h"
#include "include/apparmorfs.h"
#include "include/audit.h"
#include "include/context.h"
#include "include/crypto.h"
+#include "include/ipc.h"
#include "include/policy.h"
#include "include/resource.h"
@@ -37,7 +40,7 @@
*
* Returns: length of mangled name
*/
-static int mangle_name(char *name, char *target)
+static int mangle_name(const char *name, char *target)
{
char *t = target;
@@ -186,6 +189,147 @@ static const struct file_operations aa_fs_profile_remove = {
.llseek = default_llseek,
};
+/**
+ * query_label - queries a label and writes permissions to buf
+ * @buf: the resulting permissions string is stored here (NOT NULL)
+ * @buf_len: size of buf
+ * @query: binary query string to match against the dfa
+ * @query_len: size of query
+ *
+ * The buffers pointed to by buf and query may overlap. The query buffer is
+ * parsed before buf is written to.
+ *
+ * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
+ * the name of the label, in the current namespace, that is to be queried and
+ * DFA_STRING is a binary string to match against the label(s)'s DFA.
+ *
+ * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
+ * but must *not* be NUL terminated.
+ *
+ * Returns: number of characters written to buf or -errno on failure
+ */
+static ssize_t query_label(char *buf, size_t buf_len,
+ char *query, size_t query_len)
+{
+ struct aa_profile *profile;
+ struct aa_label *label;
+ char *label_name, *match_str;
+ size_t label_name_len, match_len;
+ struct aa_perms perms;
+ unsigned int state = 0;
+ struct label_it i;
+
+ if (!query_len)
+ return -EINVAL;
+
+ label_name = query;
+ label_name_len = strnlen(query, query_len);
+ if (!label_name_len || label_name_len == query_len)
+ return -EINVAL;
+
+ /**
+ * The extra byte is to account for the null byte between the
+ * profile name and dfa string. profile_name_len is greater
+ * than zero and less than query_len, so a byte can be safely
+ * added or subtracted.
+ */
+ match_str = label_name + label_name_len + 1;
+ match_len = query_len - label_name_len - 1;
+
+ label = aa_label_parse(aa_current_label(), label_name, GFP_KERNEL,
+ false);
+ if (IS_ERR(label))
+ return PTR_ERR(label);
+
+ aa_perms_all(&perms);
+ label_for_each_confined(i, label, profile) {
+ struct aa_perms tmp;
+ struct aa_dfa *dfa;
+ if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
+ dfa = profile->file.dfa;
+ state = aa_dfa_match_len(dfa, profile->file.start,
+ match_str + 1, match_len - 1);
+ } else if (profile->policy.dfa) {
+ if (!PROFILE_MEDIATES_SAFE(profile, *match_str))
+ continue; /* no change to current perms */
+ dfa = profile->policy.dfa;
+ state = aa_dfa_match_len(dfa, profile->policy.start[0],
+ match_str, match_len);
+ }
+ if (state)
+ aa_compute_perms(dfa, state, &tmp);
+ else
+ aa_perms_clear(&tmp);
+ aa_apply_modes_to_perms(profile, &tmp);
+ aa_perms_accum_raw(&perms, &tmp);
+ }
+ aa_put_label(label);
+
+ return scnprintf(buf, buf_len,
+ "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
+ perms.allow, perms.deny, perms.audit, perms.quiet);
+}
+
+#define QUERY_CMD_LABEL "label\0"
+#define QUERY_CMD_LABEL_LEN 6
+#define QUERY_CMD_PROFILE "profile\0"
+#define QUERY_CMD_PROFILE_LEN 8
+
+/**
+ * aa_write_access - generic permissions query
+ * @file: pointer to open apparmorfs/access file
+ * @ubuf: user buffer containing the complete query string (NOT NULL)
+ * @count: size of ubuf
+ * @ppos: position in the file (MUST BE ZERO)
+ *
+ * Allows for one permission query per open(), write(), and read() sequence.
+ * The only query currently supported is a label-based query. For this query
+ * ubuf must begin with "label\0", followed by the profile query specific
+ * format described in the query_label() function documentation.
+ *
+ * Returns: number of bytes written or -errno on failure
+ */
+static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
+ size_t count, loff_t *ppos)
+{
+ char *buf;
+ ssize_t len;
+
+ if (*ppos)
+ return -ESPIPE;
+
+ buf = simple_transaction_get(file, ubuf, count);
+ if (IS_ERR(buf))
+ return PTR_ERR(buf);
+
+ if (count > QUERY_CMD_PROFILE_LEN &&
+ !memcmp(buf, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
+ len = query_label(buf, SIMPLE_TRANSACTION_LIMIT,
+ buf + QUERY_CMD_PROFILE_LEN,
+ count - QUERY_CMD_PROFILE_LEN);
+ } else if (count > QUERY_CMD_LABEL_LEN &&
+ !memcmp(buf, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
+ len = query_label(buf, SIMPLE_TRANSACTION_LIMIT,
+ buf + QUERY_CMD_LABEL_LEN,
+ count - QUERY_CMD_LABEL_LEN);
+ } else
+ len = -EINVAL;
+
+ if (len < 0)
+ return len;
+
+ simple_transaction_set(file, len);
+
+ return count;
+}
+
+static const struct file_operations aa_fs_access = {
+ .write = aa_write_access,
+ .read = simple_transaction_read,
+ .release = simple_transaction_release,
+ .llseek = generic_file_llseek,
+};