| 1 | From 0c2a9707c125f60857639cee4e3dd31278d8b09f Mon Sep 17 00:00:00 2001 |
|---|---|
| 2 | From: Ikey Doherty <ikey@solus-project.com> |
| 3 | Date: Sun, 3 Dec 2017 13:17:29 +0000 |
| 4 | Subject: [PATCH] apparmor: Merge items still missing from upstream |
| 5 | |
| 6 | Also do some backport/forward-port magic to actually get it to build |
| 7 | with 4.14. |
| 8 | |
| 9 | Signed-off-by: Ikey Doherty <ikey@solus-project.com> |
| 10 | --- |
| 11 | security/apparmor/.gitignore | 1 + |
| 12 | security/apparmor/Makefile | 45 ++- |
| 13 | security/apparmor/af_unix.c | 651 ++++++++++++++++++++++++++++++++++ |
| 14 | security/apparmor/apparmorfs.c | 15 +- |
| 15 | security/apparmor/crypto.c | 19 + |
| 16 | security/apparmor/file.c | 34 +- |
| 17 | security/apparmor/include/af_unix.h | 114 ++++++ |
| 18 | security/apparmor/include/audit.h | 14 +- |
| 19 | security/apparmor/include/crypto.h | 11 + |
| 20 | security/apparmor/include/net.h | 124 +++++++ |
| 21 | security/apparmor/include/path.h | 1 + |
| 22 | security/apparmor/include/perms.h | 5 +- |
| 23 | security/apparmor/include/policy.h | 13 + |
| 24 | security/apparmor/include/sig_names.h | 5 +- |
| 25 | security/apparmor/ipc.c | 4 +- |
| 26 | security/apparmor/lib.c | 5 +- |
| 27 | security/apparmor/lsm.c | 434 +++++++++++++++++++++++ |
| 28 | security/apparmor/net.c | 356 +++++++++++++++++++ |
| 29 | security/apparmor/policy_unpack.c | 47 ++- |
| 30 | 19 files changed, 1875 insertions(+), 23 deletions(-) |
| 31 | create mode 100644 security/apparmor/af_unix.c |
| 32 | create mode 100644 security/apparmor/include/af_unix.h |
| 33 | create mode 100644 security/apparmor/include/net.h |
| 34 | create mode 100644 security/apparmor/net.c |
| 35 | |
| 36 | diff --git a/security/apparmor/.gitignore b/security/apparmor/.gitignore |
| 37 | index 9cdec70d..d5b291e9 100644 |
| 38 | --- a/security/apparmor/.gitignore |
| 39 | +++ b/security/apparmor/.gitignore |
| 40 | @@ -1,5 +1,6 @@ |
| 41 | # |
| 42 | # Generated include files |
| 43 | # |
| 44 | +net_names.h |
| 45 | capability_names.h |
| 46 | rlim_names.h |
| 47 | diff --git a/security/apparmor/Makefile b/security/apparmor/Makefile |
| 48 | index 9a6b4033..ef39226f 100644 |
| 49 | --- a/security/apparmor/Makefile |
| 50 | +++ b/security/apparmor/Makefile |
| 51 | @@ -1,15 +1,48 @@ |
| 52 | -# SPDX-License-Identifier: GPL-2.0 |
| 53 | # Makefile for AppArmor Linux Security Module |
| 54 | # |
| 55 | obj-$(CONFIG_SECURITY_APPARMOR) += apparmor.o |
| 56 | |
| 57 | apparmor-y := apparmorfs.o audit.o capability.o context.o ipc.o lib.o match.o \ |
| 58 | path.o domain.o policy.o policy_unpack.o procattr.o lsm.o \ |
| 59 | - resource.o secid.o file.o policy_ns.o label.o mount.o |
| 60 | + resource.o secid.o file.o policy_ns.o label.o mount.o net.o \ |
| 61 | + af_unix.o |
| 62 | apparmor-$(CONFIG_SECURITY_APPARMOR_HASH) += crypto.o |
| 63 | |
| 64 | -clean-files := capability_names.h rlim_names.h |
| 65 | +clean-files := capability_names.h rlim_names.h net_names.h |
| 66 | |
| 67 | +# Build a lower case string table of address family names |
| 68 | +# Transform lines from |
| 69 | +# #define AF_LOCAL 1 /* POSIX name for AF_UNIX */ |
| 70 | +# #define AF_INET 2 /* Internet IP Protocol */ |
| 71 | +# to |
| 72 | +# [1] = "local", |
| 73 | +# [2] = "inet", |
| 74 | +# |
| 75 | +# and build the securityfs entries for the mapping. |
| 76 | +# Transforms lines from |
| 77 | +# #define AF_INET 2 /* Internet IP Protocol */ |
| 78 | +# to |
| 79 | +# #define AA_SFS_AF_MASK "local inet" |
| 80 | +quiet_cmd_make-af = GEN $@ |
| 81 | +cmd_make-af = echo "static const char *address_family_names[] = {" > $@ ;\ |
| 82 | + sed $< >>$@ -r -n -e "/AF_MAX/d" -e "/AF_LOCAL/d" -e "/AF_ROUTE/d" -e \ |
| 83 | + 's/^\#define[ \t]+AF_([A-Z0-9_]+)[ \t]+([0-9]+)(.*)/[\2] = "\L\1",/p';\ |
| 84 | + echo "};" >> $@ ;\ |
| 85 | + printf '%s' '\#define AA_SFS_AF_MASK "' >> $@ ;\ |
| 86 | + sed -r -n -e "/AF_MAX/d" -e "/AF_LOCAL/d" -e "/AF_ROUTE/d" -e \ |
| 87 | + 's/^\#define[ \t]+AF_([A-Z0-9_]+)[ \t]+([0-9]+)(.*)/\L\1/p'\ |
| 88 | + $< | tr '\n' ' ' | sed -e 's/ $$/"\n/' >> $@ |
| 89 | + |
| 90 | +# Build a lower case string table of sock type names |
| 91 | +# Transform lines from |
| 92 | +# SOCK_STREAM = 1, |
| 93 | +# to |
| 94 | +# [1] = "stream", |
| 95 | +quiet_cmd_make-sock = GEN $@ |
| 96 | +cmd_make-sock = echo "static const char *sock_type_names[] = {" >> $@ ;\ |
| 97 | + sed $^ >>$@ -r -n \ |
| 98 | + -e 's/^\tSOCK_([A-Z0-9_]+)[\t]+=[ \t]+([0-9]+)(.*)/[\2] = "\L\1",/p';\ |
| 99 | + echo "};" >> $@ |
| 100 | |
| 101 | # Build a lower case string table of capability names |
| 102 | # Transforms lines from |
| 103 | @@ -62,6 +95,7 @@ cmd_make-rlim = echo "static const char *const rlim_names[RLIM_NLIMITS] = {" \ |
| 104 | tr '\n' ' ' | sed -e 's/ $$/"\n/' >> $@ |
| 105 | |
| 106 | $(obj)/capability.o : $(obj)/capability_names.h |
| 107 | +$(obj)/net.o : $(obj)/net_names.h |
| 108 | $(obj)/resource.o : $(obj)/rlim_names.h |
| 109 | $(obj)/capability_names.h : $(srctree)/include/uapi/linux/capability.h \ |
| 110 | $(src)/Makefile |
| 111 | @@ -69,3 +103,8 @@ $(obj)/capability_names.h : $(srctree)/include/uapi/linux/capability.h \ |
| 112 | $(obj)/rlim_names.h : $(srctree)/include/uapi/asm-generic/resource.h \ |
| 113 | $(src)/Makefile |
| 114 | $(call cmd,make-rlim) |
| 115 | +$(obj)/net_names.h : $(srctree)/include/linux/socket.h \ |
| 116 | + $(srctree)/include/linux/net.h \ |
| 117 | + $(src)/Makefile |
| 118 | + $(call cmd,make-af) |
| 119 | + $(call cmd,make-sock) |
| 120 | diff --git a/security/apparmor/af_unix.c b/security/apparmor/af_unix.c |
| 121 | new file mode 100644 |
| 122 | index 00000000..c6876db2 |
| 123 | --- /dev/null |
| 124 | +++ b/security/apparmor/af_unix.c |
| 125 | @@ -0,0 +1,651 @@ |
| 126 | +/* |
| 127 | + * AppArmor security module |
| 128 | + * |
| 129 | + * This file contains AppArmor af_unix fine grained mediation |
| 130 | + * |
| 131 | + * Copyright 2014 Canonical Ltd. |
| 132 | + * |
| 133 | + * This program is free software; you can redistribute it and/or |
| 134 | + * modify it under the terms of the GNU General Public License as |
| 135 | + * published by the Free Software Foundation, version 2 of the |
| 136 | + * License. |
| 137 | + */ |
| 138 | + |
| 139 | +#include <net/tcp_states.h> |
| 140 | + |
| 141 | +#include "include/af_unix.h" |
| 142 | +#include "include/apparmor.h" |
| 143 | +#include "include/context.h" |
| 144 | +#include "include/file.h" |
| 145 | +#include "include/label.h" |
| 146 | +#include "include/path.h" |
| 147 | +#include "include/policy.h" |
| 148 | + |
| 149 | +static inline struct sock *aa_sock(struct unix_sock *u) |
| 150 | +{ |
| 151 | + return &u->sk; |
| 152 | +} |
| 153 | + |
| 154 | +static inline int unix_fs_perm(const char *op, u32 mask, struct aa_label *label, |
| 155 | + struct unix_sock *u, int flags) |
| 156 | +{ |
| 157 | + AA_BUG(!label); |
| 158 | + AA_BUG(!u); |
| 159 | + AA_BUG(!UNIX_FS(aa_sock(u))); |
| 160 | + |
| 161 | + if (unconfined(label) || !LABEL_MEDIATES(label, AA_CLASS_FILE)) |
| 162 | + return 0; |
| 163 | + |
| 164 | + mask &= NET_FS_PERMS; |
| 165 | + if (!u->path.dentry) { |
| 166 | + struct path_cond cond = { }; |
| 167 | + struct aa_perms perms = { }; |
| 168 | + struct aa_profile *profile; |
| 169 | + |
| 170 | + /* socket path has been cleared because it is being shutdown |
| 171 | + * can only fall back to original sun_path request |
| 172 | + */ |
| 173 | + struct aa_sk_ctx *ctx = SK_CTX(&u->sk); |
| 174 | + if (ctx->path.dentry) |
| 175 | + return aa_path_perm(op, label, &ctx->path, flags, mask, |
| 176 | + &cond); |
| 177 | + return fn_for_each_confined(label, profile, |
| 178 | + ((flags | profile->path_flags) & PATH_MEDIATE_DELETED) ? |
| 179 | + __aa_path_perm(op, profile, |
| 180 | + u->addr->name->sun_path, mask, |
| 181 | + &cond, flags, &perms) : |
| 182 | + aa_audit_file(profile, &nullperms, op, mask, |
| 183 | + u->addr->name->sun_path, NULL, |
| 184 | + NULL, cond.uid, |
| 185 | + "Failed name lookup - " |
| 186 | + "deleted entry", -EACCES)); |
| 187 | + } else { |
| 188 | + /* the sunpath may not be valid for this ns so use the path */ |
| 189 | + struct path_cond cond = { u->path.dentry->d_inode->i_uid, |
| 190 | + u->path.dentry->d_inode->i_mode |
| 191 | + }; |
| 192 | + |
| 193 | + return aa_path_perm(op, label, &u->path, flags, mask, &cond); |
| 194 | + } |
| 195 | + |
| 196 | + return 0; |
| 197 | +} |
| 198 | + |
| 199 | +/* passing in state returned by PROFILE_MEDIATES_AF */ |
| 200 | +static unsigned int match_to_prot(struct aa_profile *profile, |
| 201 | + unsigned int state, int type, int protocol, |
| 202 | + const char **info) |
| 203 | +{ |
| 204 | + __be16 buffer[2]; |
| 205 | + buffer[0] = cpu_to_be16(type); |
| 206 | + buffer[1] = cpu_to_be16(protocol); |
| 207 | + state = aa_dfa_match_len(profile->policy.dfa, state, (char *) &buffer, |
| 208 | + 4); |
| 209 | + if (!state) |
| 210 | + *info = "failed type and protocol match"; |
| 211 | + return state; |
| 212 | +} |
| 213 | + |
| 214 | +static unsigned int match_addr(struct aa_profile *profile, unsigned int state, |
| 215 | + struct sockaddr_un *addr, int addrlen) |
| 216 | +{ |
| 217 | + if (addr) |
| 218 | + /* include leading \0 */ |
| 219 | + state = aa_dfa_match_len(profile->policy.dfa, state, |
| 220 | + addr->sun_path, |
| 221 | + unix_addr_len(addrlen)); |
| 222 | + else |
| 223 | + /* anonymous end point */ |
| 224 | + state = aa_dfa_match_len(profile->policy.dfa, state, "\x01", |
| 225 | + 1); |
| 226 | + /* todo change to out of band */ |
| 227 | + state = aa_dfa_null_transition(profile->policy.dfa, state); |
| 228 | + return state; |
| 229 | +} |
| 230 | + |
| 231 | +static unsigned int match_to_local(struct aa_profile *profile, |
| 232 | + unsigned int state, int type, int protocol, |
| 233 | + struct sockaddr_un *addr, int addrlen, |
| 234 | + const char **info) |
| 235 | +{ |
| 236 | + state = match_to_prot(profile, state, type, protocol, info); |
| 237 | + if (state) { |
| 238 | + state = match_addr(profile, state, addr, addrlen); |
| 239 | + if (state) { |
| 240 | + /* todo: local label matching */ |
| 241 | + state = aa_dfa_null_transition(profile->policy.dfa, |
| 242 | + state); |
| 243 | + if (!state) |
| 244 | + *info = "failed local label match"; |
| 245 | + } else |
| 246 | + *info = "failed local address match"; |
| 247 | + } |
| 248 | + |
| 249 | + return state; |
| 250 | +} |
| 251 | + |
| 252 | +static unsigned int match_to_sk(struct aa_profile *profile, |
| 253 | + unsigned int state, struct unix_sock *u, |
| 254 | + const char **info) |
| 255 | +{ |
| 256 | + struct sockaddr_un *addr = NULL; |
| 257 | + int addrlen = 0; |
| 258 | + |
| 259 | + if (u->addr) { |
| 260 | + addr = u->addr->name; |
| 261 | + addrlen = u->addr->len; |
| 262 | + } |
| 263 | + |
| 264 | + return match_to_local(profile, state, u->sk.sk_type, u->sk.sk_protocol, |
| 265 | + addr, addrlen, info); |
| 266 | +} |
| 267 | + |
| 268 | +#define CMD_ADDR 1 |
| 269 | +#define CMD_LISTEN 2 |
| 270 | +#define CMD_OPT 4 |
| 271 | + |
| 272 | +static inline unsigned int match_to_cmd(struct aa_profile *profile, |
| 273 | + unsigned int state, struct unix_sock *u, |
| 274 | + char cmd, const char **info) |
| 275 | +{ |
| 276 | + state = match_to_sk(profile, state, u, info); |
| 277 | + if (state) { |
| 278 | + state = aa_dfa_match_len(profile->policy.dfa, state, &cmd, 1); |
| 279 | + if (!state) |
| 280 | + *info = "failed cmd selection match"; |
| 281 | + } |
| 282 | + |
| 283 | + return state; |
| 284 | +} |
| 285 | + |
| 286 | +static inline unsigned int match_to_peer(struct aa_profile *profile, |
| 287 | + unsigned int state, |
| 288 | + struct unix_sock *u, |
| 289 | + struct sockaddr_un *peer_addr, |
| 290 | + int peer_addrlen, |
| 291 | + const char **info) |
| 292 | +{ |
| 293 | + state = match_to_cmd(profile, state, u, CMD_ADDR, info); |
| 294 | + if (state) { |
| 295 | + state = match_addr(profile, state, peer_addr, peer_addrlen); |
| 296 | + if (!state) |
| 297 | + *info = "failed peer address match"; |
| 298 | + } |
| 299 | + return state; |
| 300 | +} |
| 301 | + |
| 302 | +static int do_perms(struct aa_profile *profile, unsigned int state, u32 request, |
| 303 | + struct common_audit_data *sa) |
| 304 | +{ |
| 305 | + struct aa_perms perms; |
| 306 | + |
| 307 | + AA_BUG(!profile); |
| 308 | + |
| 309 | + aa_compute_perms(profile->policy.dfa, state, &perms); |
| 310 | + aa_apply_modes_to_perms(profile, &perms); |
| 311 | + return aa_check_perms(profile, &perms, request, sa, |
| 312 | + audit_net_cb); |
| 313 | +} |
| 314 | + |
| 315 | +static int match_label(struct aa_profile *profile, struct aa_profile *peer, |
| 316 | + unsigned int state, u32 request, |
| 317 | + struct common_audit_data *sa) |
| 318 | +{ |
| 319 | + AA_BUG(!profile); |
| 320 | + AA_BUG(!peer); |
| 321 | + |
| 322 | + aad(sa)->peer = &peer->label; |
| 323 | + |
| 324 | + if (state) { |
| 325 | + state = aa_dfa_match(profile->policy.dfa, state, |
| 326 | + peer->base.hname); |
| 327 | + if (!state) |
| 328 | + aad(sa)->info = "failed peer label match"; |
| 329 | + } |
| 330 | + return do_perms(profile, state, request, sa); |
| 331 | +} |
| 332 | + |
| 333 | + |
| 334 | +/* unix sock creation comes before we know if the socket will be an fs |
| 335 | + * socket |
| 336 | + * v6 - semantics are handled by mapping in profile load |
| 337 | + * v7 - semantics require sock create for tasks creating an fs socket. |
| 338 | + */ |
| 339 | +static int profile_create_perm(struct aa_profile *profile, int family, |
| 340 | + int type, int protocol) |
| 341 | +{ |
| 342 | + unsigned int state; |
| 343 | + DEFINE_AUDIT_NET(sa, OP_CREATE, NULL, family, type, protocol); |
| 344 | + |
| 345 | + AA_BUG(!profile); |
| 346 | + AA_BUG(profile_unconfined(profile)); |
| 347 | + |
| 348 | + if ((state = PROFILE_MEDIATES_AF(profile, AF_UNIX))) { |
| 349 | + state = match_to_prot(profile, state, type, protocol, |
| 350 | + &aad(&sa)->info); |
| 351 | + return do_perms(profile, state, AA_MAY_CREATE, &sa); |
| 352 | + } |
| 353 | + |
| 354 | + return aa_profile_af_perm(profile, &sa, AA_MAY_CREATE, family, type); |
| 355 | +} |
| 356 | + |
| 357 | +int aa_unix_create_perm(struct aa_label *label, int family, int type, |
| 358 | + int protocol) |
| 359 | +{ |
| 360 | + struct aa_profile *profile; |
| 361 | + |
| 362 | + if (unconfined(label)) |
| 363 | + return 0; |
| 364 | + |
| 365 | + return fn_for_each_confined(label, profile, |
| 366 | + profile_create_perm(profile, family, type, protocol)); |
| 367 | +} |
| 368 | + |
| 369 | + |
| 370 | +static inline int profile_sk_perm(struct aa_profile *profile, const char *op, |
| 371 | + u32 request, struct sock *sk) |
| 372 | +{ |
| 373 | + unsigned int state; |
| 374 | + DEFINE_AUDIT_SK(sa, op, sk); |
| 375 | + |
| 376 | + AA_BUG(!profile); |
| 377 | + AA_BUG(!sk); |
| 378 | + AA_BUG(UNIX_FS(sk)); |
| 379 | + AA_BUG(profile_unconfined(profile)); |
| 380 | + |
| 381 | + state = PROFILE_MEDIATES_AF(profile, AF_UNIX); |
| 382 | + if (state) { |
| 383 | + state = match_to_sk(profile, state, unix_sk(sk), |
| 384 | + &aad(&sa)->info); |
| 385 | + return do_perms(profile, state, request, &sa); |
| 386 | + } |
| 387 | + |
| 388 | + return aa_profile_af_sk_perm(profile, &sa, request, sk); |
| 389 | +} |
| 390 | + |
| 391 | +int aa_unix_label_sk_perm(struct aa_label *label, const char *op, u32 request, |
| 392 | + struct sock *sk) |
| 393 | +{ |
| 394 | + struct aa_profile *profile; |
| 395 | + |
| 396 | + return fn_for_each_confined(label, profile, |
| 397 | + profile_sk_perm(profile, op, request, sk)); |
| 398 | +} |
| 399 | + |
| 400 | +static int unix_label_sock_perm(struct aa_label *label, const char *op, u32 request, |
| 401 | + struct socket *sock) |
| 402 | +{ |
| 403 | + if (unconfined(label)) |
| 404 | + return 0; |
| 405 | + if (UNIX_FS(sock->sk)) |
| 406 | + return unix_fs_perm(op, request, label, unix_sk(sock->sk), 0); |
| 407 | + |
| 408 | + return aa_unix_label_sk_perm(label, op, request, sock->sk); |
| 409 | +} |
| 410 | + |
| 411 | +/* revaliation, get/set attr */ |
| 412 | +int aa_unix_sock_perm(const char *op, u32 request, struct socket *sock) |
| 413 | +{ |
| 414 | + struct aa_label *label; |
| 415 | + int error; |
| 416 | + |
| 417 | + label = begin_current_label_crit_section(); |
| 418 | + error = unix_label_sock_perm(label, op, request, sock); |
| 419 | + end_current_label_crit_section(label); |
| 420 | + |
| 421 | + return error; |
| 422 | +} |
| 423 | + |
| 424 | +static int profile_bind_perm(struct aa_profile *profile, struct sock *sk, |
| 425 | + struct sockaddr *addr, int addrlen) |
| 426 | +{ |
| 427 | + unsigned int state; |
| 428 | + DEFINE_AUDIT_SK(sa, OP_BIND, sk); |
| 429 | + |
| 430 | + AA_BUG(!profile); |
| 431 | + AA_BUG(!sk); |
| 432 | + AA_BUG(addr->sa_family != AF_UNIX); |
| 433 | + AA_BUG(profile_unconfined(profile)); |
| 434 | + AA_BUG(unix_addr_fs(addr, addrlen)); |
| 435 | + |
| 436 | + state = PROFILE_MEDIATES_AF(profile, AF_UNIX); |
| 437 | + if (state) { |
| 438 | + /* bind for abstract socket */ |
| 439 | + aad(&sa)->net.addr = unix_addr(addr); |
| 440 | + aad(&sa)->net.addrlen = addrlen; |
| 441 | + |
| 442 | + state = match_to_local(profile, state, |
| 443 | + sk->sk_type, sk->sk_protocol, |
| 444 | + unix_addr(addr), addrlen, |
| 445 | + &aad(&sa)->info); |
| 446 | + return do_perms(profile, state, AA_MAY_BIND, &sa); |
| 447 | + } |
| 448 | + |
| 449 | + return aa_profile_af_sk_perm(profile, &sa, AA_MAY_BIND, sk); |
| 450 | +} |
| 451 | + |
| 452 | +int aa_unix_bind_perm(struct socket *sock, struct sockaddr *address, |
| 453 | + int addrlen) |
| 454 | +{ |
| 455 | + struct aa_profile *profile; |
| 456 | + struct aa_label *label; |
| 457 | + int error = 0; |
| 458 | + |
| 459 | + label = begin_current_label_crit_section(); |
| 460 | + /* fs bind is handled by mknod */ |
| 461 | + if (!(unconfined(label) || unix_addr_fs(address, addrlen))) |
| 462 | + error = fn_for_each_confined(label, profile, |
| 463 | + profile_bind_perm(profile, sock->sk, address, |
| 464 | + addrlen)); |
| 465 | + end_current_label_crit_section(label); |
| 466 | + |
| 467 | + return error; |
| 468 | +} |
| 469 | + |
| 470 | +int aa_unix_connect_perm(struct socket *sock, struct sockaddr *address, |
| 471 | + int addrlen) |
| 472 | +{ |
| 473 | + /* unix connections are covered by the |
| 474 | + * - unix_stream_connect (stream) and unix_may_send hooks (dgram) |
| 475 | + * - fs connect is handled by open |
| 476 | + */ |
| 477 | + return 0; |
| 478 | +} |
| 479 | + |
| 480 | +static int profile_listen_perm(struct aa_profile *profile, struct sock *sk, |
| 481 | + int backlog) |
| 482 | +{ |
| 483 | + unsigned int state; |
| 484 | + DEFINE_AUDIT_SK(sa, OP_LISTEN, sk); |
| 485 | + |
| 486 | + AA_BUG(!profile); |
| 487 | + AA_BUG(!sk); |
| 488 | + AA_BUG(UNIX_FS(sk)); |
| 489 | + AA_BUG(profile_unconfined(profile)); |
| 490 | + |
| 491 | + state = PROFILE_MEDIATES_AF(profile, AF_UNIX); |
| 492 | + if (state) { |
| 493 | + __be16 b = cpu_to_be16(backlog); |
| 494 | + |
| 495 | + state = match_to_cmd(profile, state, unix_sk(sk), CMD_LISTEN, |
| 496 | + &aad(&sa)->info); |
| 497 | + if (state) { |
| 498 | + state = aa_dfa_match_len(profile->policy.dfa, state, |
| 499 | + (char *) &b, 2); |
| 500 | + if (!state) |
| 501 | + aad(&sa)->info = "failed listen backlog match"; |
| 502 | + } |
| 503 | + return do_perms(profile, state, AA_MAY_LISTEN, &sa); |
| 504 | + } |
| 505 | + |
| 506 | + return aa_profile_af_sk_perm(profile, &sa, AA_MAY_LISTEN, sk); |
| 507 | +} |
| 508 | + |
| 509 | +int aa_unix_listen_perm(struct socket *sock, int backlog) |
| 510 | +{ |
| 511 | + struct aa_profile *profile; |
| 512 | + struct aa_label *label; |
| 513 | + int error = 0; |
| 514 | + |
| 515 | + label = begin_current_label_crit_section(); |
| 516 | + if (!(unconfined(label) || UNIX_FS(sock->sk))) |
| 517 | + error = fn_for_each_confined(label, profile, |
| 518 | + profile_listen_perm(profile, sock->sk, |
| 519 | + backlog)); |
| 520 | + end_current_label_crit_section(label); |
| 521 | + |
| 522 | + return error; |
| 523 | +} |
| 524 | + |
| 525 | + |
| 526 | +static inline int profile_accept_perm(struct aa_profile *profile, |
| 527 | + struct sock *sk, |
| 528 | + struct sock *newsk) |
| 529 | +{ |
| 530 | + unsigned int state; |
| 531 | + DEFINE_AUDIT_SK(sa, OP_ACCEPT, sk); |
| 532 | + |
| 533 | + AA_BUG(!profile); |
| 534 | + AA_BUG(!sk); |
| 535 | + AA_BUG(UNIX_FS(sk)); |
| 536 | + AA_BUG(profile_unconfined(profile)); |
| 537 | + |
| 538 | + state = PROFILE_MEDIATES_AF(profile, AF_UNIX); |
| 539 | + if (state) { |
| 540 | + state = match_to_sk(profile, state, unix_sk(sk), |
| 541 | + &aad(&sa)->info); |
| 542 | + return do_perms(profile, state, AA_MAY_ACCEPT, &sa); |
| 543 | + } |
| 544 | + |
| 545 | + return aa_profile_af_sk_perm(profile, &sa, AA_MAY_ACCEPT, sk); |
| 546 | +} |
| 547 | + |
| 548 | +/* ability of sock to connect, not peer address binding */ |
| 549 | +int aa_unix_accept_perm(struct socket *sock, struct socket *newsock) |
| 550 | +{ |
| 551 | + struct aa_profile *profile; |
| 552 | + struct aa_label *label; |
| 553 | + int error = 0; |
| 554 | + |
| 555 | + label = begin_current_label_crit_section(); |
| 556 | + if (!(unconfined(label) || UNIX_FS(sock->sk))) |
| 557 | + error = fn_for_each_confined(label, profile, |
| 558 | + profile_accept_perm(profile, sock->sk, |
| 559 | + newsock->sk)); |
| 560 | + end_current_label_crit_section(label); |
| 561 | + |
| 562 | + return error; |
| 563 | +} |
| 564 | + |
| 565 | + |
| 566 | +/* dgram handled by unix_may_sendmsg, right to send on stream done at connect |
| 567 | + * could do per msg unix_stream here |
| 568 | + */ |
| 569 | +/* sendmsg, recvmsg */ |
| 570 | +int aa_unix_msg_perm(const char *op, u32 request, struct socket *sock, |
| 571 | + struct msghdr *msg, int size) |
| 572 | +{ |
| 573 | + return 0; |
| 574 | +} |
| 575 | + |
| 576 | + |
| 577 | +static int profile_opt_perm(struct aa_profile *profile, const char *op, u32 request, |
| 578 | + struct sock *sk, int level, int optname) |
| 579 | +{ |
| 580 | + unsigned int state; |
| 581 | + DEFINE_AUDIT_SK(sa, op, sk); |
| 582 | + |
| 583 | + AA_BUG(!profile); |
| 584 | + AA_BUG(!sk); |
| 585 | + AA_BUG(UNIX_FS(sk)); |
| 586 | + AA_BUG(profile_unconfined(profile)); |
| 587 | + |
| 588 | + state = PROFILE_MEDIATES_AF(profile, AF_UNIX); |
| 589 | + if (state) { |
| 590 | + __be16 b = cpu_to_be16(optname); |
| 591 | + |
| 592 | + state = match_to_cmd(profile, state, unix_sk(sk), CMD_OPT, |
| 593 | + &aad(&sa)->info); |
| 594 | + if (state) { |
| 595 | + state = aa_dfa_match_len(profile->policy.dfa, state, |
| 596 | + (char *) &b, 2); |
| 597 | + if (!state) |
| 598 | + aad(&sa)->info = "failed sockopt match"; |
| 599 | + } |
| 600 | + return do_perms(profile, state, request, &sa); |
| 601 | + } |
| 602 | + |
| 603 | + return aa_profile_af_sk_perm(profile, &sa, request, sk); |
| 604 | +} |
| 605 | + |
| 606 | +int aa_unix_opt_perm(const char *op, u32 request, struct socket *sock, int level, |
| 607 | + int optname) |
| 608 | +{ |
| 609 | + struct aa_profile *profile; |
| 610 | + struct aa_label *label; |
| 611 | + int error = 0; |
| 612 | + |
| 613 | + label = begin_current_label_crit_section(); |
| 614 | + if (!(unconfined(label) || UNIX_FS(sock->sk))) |
| 615 | + error = fn_for_each_confined(label, profile, |
| 616 | + profile_opt_perm(profile, op, request, |
| 617 | + sock->sk, level, optname)); |
| 618 | + end_current_label_crit_section(label); |
| 619 | + |
| 620 | + return error; |
| 621 | +} |
| 622 | + |
| 623 | +/* null peer_label is allowed, in which case the peer_sk label is used */ |
| 624 | +static int profile_peer_perm(struct aa_profile *profile, const char *op, u32 request, |
| 625 | + struct sock *sk, struct sock *peer_sk, |
| 626 | + struct aa_label *peer_label, |
| 627 | + struct common_audit_data *sa) |
| 628 | +{ |
| 629 | + unsigned int state; |
| 630 | + |
| 631 | + AA_BUG(!profile); |
| 632 | + AA_BUG(profile_unconfined(profile)); |
| 633 | + AA_BUG(!sk); |
| 634 | + AA_BUG(!peer_sk); |
| 635 | + AA_BUG(UNIX_FS(peer_sk)); |
| 636 | + |
| 637 | + state = PROFILE_MEDIATES_AF(profile, AF_UNIX); |
| 638 | + if (state) { |
| 639 | + struct aa_sk_ctx *peer_ctx = SK_CTX(peer_sk); |
| 640 | + struct aa_profile *peerp; |
| 641 | + struct sockaddr_un *addr = NULL; |
| 642 | + int len = 0; |
| 643 | + if (unix_sk(peer_sk)->addr) { |
| 644 | + addr = unix_sk(peer_sk)->addr->name; |
| 645 | + len = unix_sk(peer_sk)->addr->len; |
| 646 | + } |
| 647 | + state = match_to_peer(profile, state, unix_sk(sk), |
| 648 | + addr, len, &aad(sa)->info); |
| 649 | + if (!peer_label) |
| 650 | + peer_label = peer_ctx->label; |
| 651 | + return fn_for_each_in_ns(peer_label, peerp, |
| 652 | + match_label(profile, peerp, state, request, |
| 653 | + sa)); |
| 654 | + } |
| 655 | + |
| 656 | + return aa_profile_af_sk_perm(profile, sa, request, sk); |
| 657 | +} |
| 658 | + |
| 659 | +/** |
| 660 | + * |
| 661 | + * Requires: lock held on both @sk and @peer_sk |
| 662 | + */ |
| 663 | +int aa_unix_peer_perm(struct aa_label *label, const char *op, u32 request, |
| 664 | + struct sock *sk, struct sock *peer_sk, |
| 665 | + struct aa_label *peer_label) |
| 666 | +{ |
| 667 | + struct unix_sock *peeru = unix_sk(peer_sk); |
| 668 | + struct unix_sock *u = unix_sk(sk); |
| 669 | + |
| 670 | + AA_BUG(!label); |
| 671 | + AA_BUG(!sk); |
| 672 | + AA_BUG(!peer_sk); |
| 673 | + |
| 674 | + if (UNIX_FS(aa_sock(peeru))) |
| 675 | + return unix_fs_perm(op, request, label, peeru, 0); |
| 676 | + else if (UNIX_FS(aa_sock(u))) |
| 677 | + return unix_fs_perm(op, request, label, u, 0); |
| 678 | + else { |
| 679 | + struct aa_profile *profile; |
| 680 | + DEFINE_AUDIT_SK(sa, op, sk); |
| 681 | + aad(&sa)->net.peer_sk = peer_sk; |
| 682 | + |
| 683 | + /* TODO: ns!!! */ |
| 684 | + if (!net_eq(sock_net(sk), sock_net(peer_sk))) { |
| 685 | + ; |
| 686 | + } |
| 687 | + |
| 688 | + if (unconfined(label)) |
| 689 | + return 0; |
| 690 | + |
| 691 | + return fn_for_each_confined(label, profile, |
| 692 | + profile_peer_perm(profile, op, request, sk, |
| 693 | + peer_sk, peer_label, &sa)); |
| 694 | + } |
| 695 | +} |
| 696 | + |
| 697 | + |
| 698 | +/* from net/unix/af_unix.c */ |
| 699 | +static void unix_state_double_lock(struct sock *sk1, struct sock *sk2) |
| 700 | +{ |
| 701 | + if (unlikely(sk1 == sk2) || !sk2) { |
| 702 | + unix_state_lock(sk1); |
| 703 | + return; |
| 704 | + } |
| 705 | + if (sk1 < sk2) { |
| 706 | + unix_state_lock(sk1); |
| 707 | + unix_state_lock_nested(sk2); |
| 708 | + } else { |
| 709 | + unix_state_lock(sk2); |
| 710 | + unix_state_lock_nested(sk1); |
| 711 | + } |
| 712 | +} |
| 713 | + |
| 714 | +static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2) |
| 715 | +{ |
| 716 | + if (unlikely(sk1 == sk2) || !sk2) { |
| 717 | + unix_state_unlock(sk1); |
| 718 | + return; |
| 719 | + } |
| 720 | + unix_state_unlock(sk1); |
| 721 | + unix_state_unlock(sk2); |
| 722 | +} |
| 723 | + |
| 724 | +int aa_unix_file_perm(struct aa_label *label, const char *op, u32 request, |
| 725 | + struct socket *sock) |
| 726 | +{ |
| 727 | + struct sock *peer_sk = NULL; |
| 728 | + u32 sk_req = request & ~NET_PEER_MASK; |
| 729 | + int error = 0; |
| 730 | + |
| 731 | + AA_BUG(!label); |
| 732 | + AA_BUG(!sock); |
| 733 | + AA_BUG(!sock->sk); |
| 734 | + AA_BUG(sock->sk->sk_family != AF_UNIX); |
| 735 | + |
| 736 | + /* TODO: update sock label with new task label */ |
| 737 | + unix_state_lock(sock->sk); |
| 738 | + peer_sk = unix_peer(sock->sk); |
| 739 | + if (peer_sk) |
| 740 | + sock_hold(peer_sk); |
| 741 | + if (!unix_connected(sock) && sk_req) { |
| 742 | + error = unix_label_sock_perm(label, op, sk_req, sock); |
| 743 | + if (!error) { |
| 744 | + // update label |
| 745 | + } |
| 746 | + } |
| 747 | + unix_state_unlock(sock->sk); |
| 748 | + if (!peer_sk) |
| 749 | + return error; |
| 750 | + |
| 751 | + unix_state_double_lock(sock->sk, peer_sk); |
| 752 | + if (UNIX_FS(sock->sk)) { |
| 753 | + error = unix_fs_perm(op, request, label, unix_sk(sock->sk), |
| 754 | + PATH_SOCK_COND); |
| 755 | + } else if (UNIX_FS(peer_sk)) { |
| 756 | + error = unix_fs_perm(op, request, label, unix_sk(peer_sk), |
| 757 | + PATH_SOCK_COND); |
| 758 | + } else { |
| 759 | + struct aa_sk_ctx *pctx = SK_CTX(peer_sk); |
| 760 | + if (sk_req) |
| 761 | + error = aa_unix_label_sk_perm(label, op, sk_req, |
| 762 | + sock->sk); |
| 763 | + last_error(error, |
| 764 | + xcheck(aa_unix_peer_perm(label, op, |
| 765 | + MAY_READ | MAY_WRITE, |
| 766 | + sock->sk, peer_sk, NULL), |
| 767 | + aa_unix_peer_perm(pctx->label, op, |
| 768 | + MAY_READ | MAY_WRITE, |
| 769 | + peer_sk, sock->sk, label))); |
| 770 | + } |
| 771 | + |
| 772 | + unix_state_double_unlock(sock->sk, peer_sk); |
| 773 | + sock_put(peer_sk); |
| 774 | + |
| 775 | + return error; |
| 776 | +} |
| 777 | diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c |
| 778 | index caaf51dd..20cdb1c4 100644 |
| 779 | --- a/security/apparmor/apparmorfs.c |
| 780 | +++ b/security/apparmor/apparmorfs.c |
| 781 | @@ -2187,6 +2187,11 @@ static struct aa_sfs_entry aa_sfs_entry_ns[] = { |
| 782 | { } |
| 783 | }; |
| 784 | |
| 785 | +static struct aa_sfs_entry aa_sfs_entry_dbus[] = { |
| 786 | + AA_SFS_FILE_STRING("mask", "acquire send receive"), |
| 787 | + { } |
| 788 | +}; |
| 789 | + |
| 790 | static struct aa_sfs_entry aa_sfs_entry_query_label[] = { |
| 791 | AA_SFS_FILE_STRING("perms", "allow deny audit quiet"), |
| 792 | AA_SFS_FILE_BOOLEAN("data", 1), |
| 793 | @@ -2202,6 +2207,7 @@ static struct aa_sfs_entry aa_sfs_entry_features[] = { |
| 794 | AA_SFS_DIR("policy", aa_sfs_entry_policy), |
| 795 | AA_SFS_DIR("domain", aa_sfs_entry_domain), |
| 796 | AA_SFS_DIR("file", aa_sfs_entry_file), |
| 797 | + AA_SFS_DIR("network", aa_sfs_entry_network), |
| 798 | AA_SFS_DIR("mount", aa_sfs_entry_mount), |
| 799 | AA_SFS_DIR("namespaces", aa_sfs_entry_ns), |
| 800 | AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), |
| 801 | @@ -2209,17 +2215,18 @@ static struct aa_sfs_entry aa_sfs_entry_features[] = { |
| 802 | AA_SFS_DIR("caps", aa_sfs_entry_caps), |
| 803 | AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace), |
| 804 | AA_SFS_DIR("signal", aa_sfs_entry_signal), |
| 805 | + AA_SFS_DIR("dbus", aa_sfs_entry_dbus), |
| 806 | AA_SFS_DIR("query", aa_sfs_entry_query), |
| 807 | { } |
| 808 | }; |
| 809 | |
| 810 | static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { |
| 811 | - AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access), |
| 812 | + AA_SFS_FILE_FOPS(".access", 0640, &aa_sfs_access), |
| 813 | AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops), |
| 814 | AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops), |
| 815 | - AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops), |
| 816 | - AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops), |
| 817 | - AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops), |
| 818 | + AA_SFS_FILE_FOPS(".ns_level", 0666, &seq_ns_level_fops), |
| 819 | + AA_SFS_FILE_FOPS(".ns_name", 0640, &seq_ns_name_fops), |
| 820 | + AA_SFS_FILE_FOPS("profiles", 0440, &aa_sfs_profiles_fops), |
| 821 | AA_SFS_DIR("features", aa_sfs_entry_features), |
| 822 | { } |
| 823 | }; |
| 824 | diff --git a/security/apparmor/crypto.c b/security/apparmor/crypto.c |
| 825 | index 136f2a04..cfedacb0 100644 |
| 826 | --- a/security/apparmor/crypto.c |
| 827 | +++ b/security/apparmor/crypto.c |
| 828 | @@ -29,6 +29,25 @@ unsigned int aa_hash_size(void) |
| 829 | return apparmor_hash_size; |
| 830 | } |
| 831 | |
| 832 | +void aa_snprint_hashstr(char *out, unsigned char *hash, unsigned int hsize) |
| 833 | +{ |
| 834 | + unsigned int i; |
| 835 | + |
| 836 | + for (i = 0; i < hsize; i++) |
| 837 | + sprintf(out + i*2, "%.2x", hash[i]); |
| 838 | + out[hsize*2] = 0; |
| 839 | +} |
| 840 | + |
| 841 | +char *aa_asprint_hashstr(unsigned char *hash, unsigned int hsize, gfp_t gfp) |
| 842 | +{ |
| 843 | + char *buffer = kmalloc(hsize*2 + 1, gfp); |
| 844 | + if (!buffer) |
| 845 | + return NULL; |
| 846 | + aa_snprint_hashstr(buffer, hash, hsize); |
| 847 | + |
| 848 | + return buffer; |
| 849 | +} |
| 850 | + |
| 851 | char *aa_calc_hash(void *data, size_t len) |
| 852 | { |
| 853 | SHASH_DESC_ON_STACK(desc, apparmor_tfm); |
| 854 | diff --git a/security/apparmor/file.c b/security/apparmor/file.c |
| 855 | index 3382518b..e6279110 100644 |
| 856 | --- a/security/apparmor/file.c |
| 857 | +++ b/security/apparmor/file.c |
| 858 | @@ -16,11 +16,13 @@ |
| 859 | #include <linux/fdtable.h> |
| 860 | #include <linux/file.h> |
| 861 | |
| 862 | +#include "include/af_unix.h" |
| 863 | #include "include/apparmor.h" |
| 864 | #include "include/audit.h" |
| 865 | #include "include/context.h" |
| 866 | #include "include/file.h" |
| 867 | #include "include/match.h" |
| 868 | +#include "include/net.h" |
| 869 | #include "include/path.h" |
| 870 | #include "include/policy.h" |
| 871 | #include "include/label.h" |
| 872 | @@ -288,7 +290,8 @@ int __aa_path_perm(const char *op, struct aa_profile *profile, const char *name, |
| 873 | { |
| 874 | int e = 0; |
| 875 | |
| 876 | - if (profile_unconfined(profile)) |
| 877 | + if (profile_unconfined(profile) || |
| 878 | + ((flags & PATH_SOCK_COND) && !PROFILE_MEDIATES_AF(profile, AF_UNIX))) |
| 879 | return 0; |
| 880 | aa_str_perms(profile->file.dfa, profile->file.start, name, cond, perms); |
| 881 | if (request & ~perms->allow) |
| 882 | @@ -566,6 +569,32 @@ static int __file_path_perm(const char *op, struct aa_label *label, |
| 883 | return error; |
| 884 | } |
| 885 | |
| 886 | +static int __file_sock_perm(const char *op, struct aa_label *label, |
| 887 | + struct aa_label *flabel, struct file *file, |
| 888 | + u32 request, u32 denied) |
| 889 | +{ |
| 890 | + struct socket *sock = (struct socket *) file->private_data; |
| 891 | + int error; |
| 892 | + |
| 893 | + AA_BUG(!sock); |
| 894 | + |
| 895 | + /* revalidation due to label out of date. No revocation at this time */ |
| 896 | + if (!denied && aa_label_is_subset(flabel, label)) |
| 897 | + return 0; |
| 898 | + |
| 899 | + /* TODO: improve to skip profiles cached in flabel */ |
| 900 | + error = aa_sock_file_perm(label, op, request, sock); |
| 901 | + if (denied) { |
| 902 | + /* TODO: improve to skip profiles checked above */ |
| 903 | + /* check every profile in file label to is cached */ |
| 904 | + last_error(error, aa_sock_file_perm(flabel, op, request, sock)); |
| 905 | + } |
| 906 | + if (!error) |
| 907 | + update_file_ctx(file_ctx(file), label, request); |
| 908 | + |
| 909 | + return error; |
| 910 | +} |
| 911 | + |
| 912 | /** |
| 913 | * aa_file_perm - do permission revalidation check & audit for @file |
| 914 | * @op: operation being checked |
| 915 | @@ -610,6 +639,9 @@ int aa_file_perm(const char *op, struct aa_label *label, struct file *file, |
| 916 | error = __file_path_perm(op, label, flabel, file, request, |
| 917 | denied); |
| 918 | |
| 919 | + else if (S_ISSOCK(file_inode(file)->i_mode)) |
| 920 | + error = __file_sock_perm(op, label, flabel, file, request, |
| 921 | + denied); |
| 922 | done: |
| 923 | rcu_read_unlock(); |
| 924 | |
| 925 | diff --git a/security/apparmor/include/af_unix.h b/security/apparmor/include/af_unix.h |
| 926 | new file mode 100644 |
| 927 | index 00000000..d1b7f231 |
| 928 | --- /dev/null |
| 929 | +++ b/security/apparmor/include/af_unix.h |
| 930 | @@ -0,0 +1,114 @@ |
| 931 | +/* |
| 932 | + * AppArmor security module |
| 933 | + * |
| 934 | + * This file contains AppArmor af_unix fine grained mediation |
| 935 | + * |
| 936 | + * Copyright 2014 Canonical Ltd. |
| 937 | + * |
| 938 | + * This program is free software; you can redistribute it and/or |
| 939 | + * modify it under the terms of the GNU General Public License as |
| 940 | + * published by the Free Software Foundation, version 2 of the |
| 941 | + * License. |
| 942 | + */ |
| 943 | +#ifndef __AA_AF_UNIX_H |
| 944 | + |
| 945 | +#include <net/af_unix.h> |
| 946 | + |
| 947 | +#include "label.h" |
| 948 | +//#include "include/net.h" |
| 949 | + |
| 950 | +#define unix_addr_len(L) ((L) - sizeof(sa_family_t)) |
| 951 | +#define unix_abstract_name_len(L) (unix_addr_len(L) - 1) |
| 952 | +#define unix_abstract_len(U) (unix_abstract_name_len((U)->addr->len)) |
| 953 | +#define addr_unix_abstract_name(B) ((B)[0] == 0) |
| 954 | +#define addr_unix_anonymous(U) (addr_unix_len(U) <= 0) |
| 955 | +#define addr_unix_abstract(U) (!addr_unix_anonymous(U) && addr_unix_abstract_name((U)->addr)) |
| 956 | +//#define unix_addr_fs(U) (!unix_addr_anonymous(U) && !unix_addr_abstract_name((U)->addr)) |
| 957 | + |
| 958 | +#define unix_addr(A) ((struct sockaddr_un *)(A)) |
| 959 | +#define unix_addr_anon(A, L) ((A) && unix_addr_len(L) <= 0) |
| 960 | +#define unix_addr_fs(A, L) (!unix_addr_anon(A, L) && !addr_unix_abstract_name(unix_addr(A)->sun_path)) |
| 961 | + |
| 962 | +#define UNIX_ANONYMOUS(U) (!unix_sk(U)->addr) |
| 963 | +/* from net/unix/af_unix.c */ |
| 964 | +#define UNIX_ABSTRACT(U) (!UNIX_ANONYMOUS(U) && \ |
| 965 | + unix_sk(U)->addr->hash < UNIX_HASH_SIZE) |
| 966 | +#define UNIX_FS(U) (!UNIX_ANONYMOUS(U) && unix_sk(U)->addr->name->sun_path[0]) |
| 967 | +#define unix_peer(sk) (unix_sk(sk)->peer) |
| 968 | +#define unix_connected(S) ((S)->state == SS_CONNECTED) |
| 969 | + |
| 970 | +static inline void print_unix_addr(struct sockaddr_un *A, int L) |
| 971 | +{ |
| 972 | + char *buf = (A) ? (char *) &(A)->sun_path : NULL; |
| 973 | + int len = unix_addr_len(L); |
| 974 | + if (!buf || len <= 0) |
| 975 | + printk(" <anonymous>"); |
| 976 | + else if (buf[0]) |
| 977 | + printk(" %s", buf); |
| 978 | + else |
| 979 | + /* abstract name len includes leading \0 */ |
| 980 | + printk(" %d @%.*s", len - 1, len - 1, buf+1); |
| 981 | +}; |
| 982 | + |
| 983 | +/* |
| 984 | + printk("%s: %s: f %d, t %d, p %d", __FUNCTION__, \ |
| 985 | + #SK , \ |
| 986 | +*/ |
| 987 | +#define print_unix_sk(SK) \ |
| 988 | +do { \ |
| 989 | + struct unix_sock *u = unix_sk(SK); \ |
| 990 | + printk("%s: f %d, t %d, p %d", #SK , \ |
| 991 | + (SK)->sk_family, (SK)->sk_type, (SK)->sk_protocol); \ |
| 992 | + if (u->addr) \ |
| 993 | + print_unix_addr(u->addr->name, u->addr->len); \ |
| 994 | + else \ |
| 995 | + print_unix_addr(NULL, sizeof(sa_family_t)); \ |
| 996 | + /* printk("\n");*/ \ |
| 997 | +} while (0) |
| 998 | + |
| 999 | +#define print_sk(SK) \ |
| 1000 | +do { \ |
| 1001 | + if (!(SK)) { \ |
| 1002 | + printk("%s: %s is null\n", __FUNCTION__, #SK); \ |
| 1003 | + } else if ((SK)->sk_family == PF_UNIX) { \ |
| 1004 | + print_unix_sk(SK); \ |
| 1005 | + printk("\n"); \ |
| 1006 | + } else { \ |
| 1007 | + printk("%s: %s: family %d\n", __FUNCTION__, #SK , \ |
| 1008 | + (SK)->sk_family); \ |
| 1009 | + } \ |
| 1010 | +} while (0) |
| 1011 | + |
| 1012 | +#define print_sock_addr(U) \ |
| 1013 | +do { \ |
| 1014 | + printk("%s:\n", __FUNCTION__); \ |
| 1015 | + printk(" sock %s:", sock_ctx && sock_ctx->label ? aa_label_printk(sock_ctx->label, GFP_ATOMIC); : "<null>"); print_sk(sock); \ |
| 1016 | + printk(" other %s:", other_ctx && other_ctx->label ? aa_label_printk(other_ctx->label, GFP_ATOMIC); : "<null>"); print_sk(other); \ |
| 1017 | + printk(" new %s", new_ctx && new_ctx->label ? aa_label_printk(new_ctx->label, GFP_ATOMIC); : "<null>"); print_sk(newsk); \ |
| 1018 | +} while (0) |
| 1019 | + |
| 1020 | + |
| 1021 | + |
| 1022 | + |
| 1023 | +int aa_unix_peer_perm(struct aa_label *label, const char *op, u32 request, |
| 1024 | + struct sock *sk, struct sock *peer_sk, |
| 1025 | + struct aa_label *peer_label); |
| 1026 | +int aa_unix_label_sk_perm(struct aa_label *label, const char *op, u32 request, |
| 1027 | + struct sock *sk); |
| 1028 | +int aa_unix_sock_perm(const char *op, u32 request, struct socket *sock); |
| 1029 | +int aa_unix_create_perm(struct aa_label *label, int family, int type, |
| 1030 | + int protocol); |
| 1031 | +int aa_unix_bind_perm(struct socket *sock, struct sockaddr *address, |
| 1032 | + int addrlen); |
| 1033 | +int aa_unix_connect_perm(struct socket *sock, struct sockaddr *address, |
| 1034 | + int addrlen); |
| 1035 | +int aa_unix_listen_perm(struct socket *sock, int backlog); |
| 1036 | +int aa_unix_accept_perm(struct socket *sock, struct socket *newsock); |
| 1037 | +int aa_unix_msg_perm(const char *op, u32 request, struct socket *sock, |
| 1038 | + struct msghdr *msg, int size); |
| 1039 | +int aa_unix_opt_perm(const char *op, u32 request, struct socket *sock, int level, |
| 1040 | + int optname); |
| 1041 | +int aa_unix_file_perm(struct aa_label *label, const char *op, u32 request, |
| 1042 | + struct socket *sock); |
| 1043 | + |
| 1044 | +#endif /* __AA_AF_UNIX_H */ |
| 1045 | diff --git a/security/apparmor/include/audit.h b/security/apparmor/include/audit.h |
| 1046 | index 4ac09511..aeacd722 100644 |
| 1047 | --- a/security/apparmor/include/audit.h |
| 1048 | +++ b/security/apparmor/include/audit.h |
| 1049 | @@ -126,7 +126,17 @@ struct apparmor_audit_data { |
| 1050 | const char *target; |
| 1051 | kuid_t ouid; |
| 1052 | } fs; |
| 1053 | + struct { |
| 1054 | + int type, protocol; |
| 1055 | + struct sock *peer_sk; |
| 1056 | + void *addr; |
| 1057 | + int addrlen; |
| 1058 | + } net; |
| 1059 | int signal; |
| 1060 | + struct { |
| 1061 | + int rlim; |
| 1062 | + unsigned long max; |
| 1063 | + } rlim; |
| 1064 | }; |
| 1065 | }; |
| 1066 | struct { |
| 1067 | @@ -134,10 +144,6 @@ struct apparmor_audit_data { |
| 1068 | const char *ns; |
| 1069 | long pos; |
| 1070 | } iface; |
| 1071 | - struct { |
| 1072 | - int rlim; |
| 1073 | - unsigned long max; |
| 1074 | - } rlim; |
| 1075 | struct { |
| 1076 | const char *src_name; |
| 1077 | const char *type; |
| 1078 | diff --git a/security/apparmor/include/crypto.h b/security/apparmor/include/crypto.h |
| 1079 | index c1469f8d..4d927dc1 100644 |
| 1080 | --- a/security/apparmor/include/crypto.h |
| 1081 | +++ b/security/apparmor/include/crypto.h |
| 1082 | @@ -18,6 +18,8 @@ |
| 1083 | |
| 1084 | #ifdef CONFIG_SECURITY_APPARMOR_HASH |
| 1085 | unsigned int aa_hash_size(void); |
| 1086 | +void aa_snprint_hashstr(char *out, unsigned char *hash, unsigned int hsize); |
| 1087 | +char *aa_asprint_hashstr(unsigned char *hash, unsigned int hsize, gfp_t gfp); |
| 1088 | char *aa_calc_hash(void *data, size_t len); |
| 1089 | int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start, |
| 1090 | size_t len); |
| 1091 | @@ -36,6 +38,15 @@ static inline unsigned int aa_hash_size(void) |
| 1092 | { |
| 1093 | return 0; |
| 1094 | } |
| 1095 | + |
| 1096 | +void aa_snprint_hashstr(char *out, unsigned char *hash, unsigned int hsize) |
| 1097 | +{ |
| 1098 | +} |
| 1099 | + |
| 1100 | +char *aa_asprint_hashstr(unsigned char *hash, unsigned int hsize, gfp_t gfp); |
| 1101 | +{ |
| 1102 | + return NULL; |
| 1103 | +} |
| 1104 | #endif |
| 1105 | |
| 1106 | #endif /* __APPARMOR_CRYPTO_H */ |
| 1107 | diff --git a/security/apparmor/include/net.h b/security/apparmor/include/net.h |
| 1108 | new file mode 100644 |
| 1109 | index 00000000..0ae45240 |
| 1110 | --- /dev/null |
| 1111 | +++ b/security/apparmor/include/net.h |
| 1112 | @@ -0,0 +1,124 @@ |
| 1113 | +/* |
| 1114 | + * AppArmor security module |
| 1115 | + * |
| 1116 | + * This file contains AppArmor network mediation definitions. |
| 1117 | + * |
| 1118 | + * Copyright (C) 1998-2008 Novell/SUSE |
| 1119 | + * Copyright 2009-2017 Canonical Ltd. |
| 1120 | + * |
| 1121 | + * This program is free software; you can redistribute it and/or |
| 1122 | + * modify it under the terms of the GNU General Public License as |
| 1123 | + * published by the Free Software Foundation, version 2 of the |
| 1124 | + * License. |
| 1125 | + */ |
| 1126 | + |
| 1127 | +#ifndef __AA_NET_H |
| 1128 | +#define __AA_NET_H |
| 1129 | + |
| 1130 | +#include <net/sock.h> |
| 1131 | +#include <linux/path.h> |
| 1132 | + |
| 1133 | +#include "apparmorfs.h" |
| 1134 | +#include "label.h" |
| 1135 | +#include "perms.h" |
| 1136 | +#include "policy.h" |
| 1137 | + |
| 1138 | +#define AA_MAY_SEND AA_MAY_WRITE |
| 1139 | +#define AA_MAY_RECEIVE AA_MAY_READ |
| 1140 | + |
| 1141 | +#define AA_MAY_SHUTDOWN AA_MAY_DELETE |
| 1142 | + |
| 1143 | +#define AA_MAY_CONNECT AA_MAY_OPEN |
| 1144 | +#define AA_MAY_ACCEPT 0x00100000 |
| 1145 | + |
| 1146 | +#define AA_MAY_BIND 0x00200000 |
| 1147 | +#define AA_MAY_LISTEN 0x00400000 |
| 1148 | + |
| 1149 | +#define AA_MAY_SETOPT 0x01000000 |
| 1150 | +#define AA_MAY_GETOPT 0x02000000 |
| 1151 | + |
| 1152 | +#define NET_PERMS_MASK (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CREATE | \ |
| 1153 | + AA_MAY_SHUTDOWN | AA_MAY_BIND | AA_MAY_LISTEN | \ |
| 1154 | + AA_MAY_CONNECT | AA_MAY_ACCEPT | AA_MAY_SETATTR | \ |
| 1155 | + AA_MAY_GETATTR | AA_MAY_SETOPT | AA_MAY_GETOPT) |
| 1156 | + |
| 1157 | +#define NET_FS_PERMS (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CREATE | \ |
| 1158 | + AA_MAY_SHUTDOWN | AA_MAY_CONNECT | AA_MAY_RENAME |\ |
| 1159 | + AA_MAY_SETATTR | AA_MAY_GETATTR | AA_MAY_CHMOD | \ |
| 1160 | + AA_MAY_CHOWN | AA_MAY_CHGRP | AA_MAY_LOCK | \ |
| 1161 | + AA_MAY_MPROT) |
| 1162 | + |
| 1163 | +#define NET_PEER_MASK (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CONNECT | \ |
| 1164 | + AA_MAY_ACCEPT) |
| 1165 | +struct aa_sk_ctx { |
| 1166 | + struct aa_label *label; |
| 1167 | + struct aa_label *peer; |
| 1168 | + struct path path; |
| 1169 | +}; |
| 1170 | + |
| 1171 | +#define SK_CTX(X) ((X)->sk_security) |
| 1172 | +#define SOCK_ctx(X) SOCK_INODE(X)->i_security |
| 1173 | +#define DEFINE_AUDIT_NET(NAME, OP, SK, F, T, P) \ |
| 1174 | + struct lsm_network_audit NAME ## _net = { .sk = (SK), \ |
| 1175 | + .family = (F)}; \ |
| 1176 | + DEFINE_AUDIT_DATA(NAME, \ |
| 1177 | + ((SK) && (F) != AF_UNIX) ? LSM_AUDIT_DATA_NET : \ |
| 1178 | + LSM_AUDIT_DATA_NONE, \ |
| 1179 | + OP); \ |
| 1180 | + NAME.u.net = &(NAME ## _net); \ |
| 1181 | + aad(&NAME)->net.type = (T); \ |
| 1182 | + aad(&NAME)->net.protocol = (P) |
| 1183 | + |
| 1184 | +#define DEFINE_AUDIT_SK(NAME, OP, SK) \ |
| 1185 | + DEFINE_AUDIT_NET(NAME, OP, SK, (SK)->sk_family, (SK)->sk_type, \ |
| 1186 | + (SK)->sk_protocol) |
| 1187 | + |
| 1188 | +/* struct aa_net - network confinement data |
| 1189 | + * @allow: basic network families permissions |
| 1190 | + * @audit: which network permissions to force audit |
| 1191 | + * @quiet: which network permissions to quiet rejects |
| 1192 | + */ |
| 1193 | +struct aa_net { |
| 1194 | + u16 allow[AF_MAX]; |
| 1195 | + u16 audit[AF_MAX]; |
| 1196 | + u16 quiet[AF_MAX]; |
| 1197 | +}; |
| 1198 | + |
| 1199 | + |
| 1200 | +extern struct aa_sfs_entry aa_sfs_entry_network[]; |
| 1201 | + |
| 1202 | +void audit_net_cb(struct audit_buffer *ab, void *va); |
| 1203 | +int aa_profile_af_perm(struct aa_profile *profile, struct common_audit_data *sa, |
| 1204 | + u32 request, u16 family, int type); |
| 1205 | +static inline int aa_profile_af_sk_perm(struct aa_profile *profile, |
| 1206 | + struct common_audit_data *sa, |
| 1207 | + u32 request, |
| 1208 | + struct sock *sk) |
| 1209 | +{ |
| 1210 | + return aa_profile_af_perm(profile, sa, request, sk->sk_family, |
| 1211 | + sk->sk_type); |
| 1212 | +} |
| 1213 | + |
| 1214 | +int aa_sock_perm(const char *op, u32 request, struct socket *sock); |
| 1215 | +int aa_sock_create_perm(struct aa_label *label, int family, int type, |
| 1216 | + int protocol); |
| 1217 | +int aa_sock_bind_perm(struct socket *sock, struct sockaddr *address, |
| 1218 | + int addrlen); |
| 1219 | +int aa_sock_connect_perm(struct socket *sock, struct sockaddr *address, |
| 1220 | + int addrlen); |
| 1221 | +int aa_sock_listen_perm(struct socket *sock, int backlog); |
| 1222 | +int aa_sock_accept_perm(struct socket *sock, struct socket *newsock); |
| 1223 | +int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, |
| 1224 | + struct msghdr *msg, int size); |
| 1225 | +int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, int level, |
| 1226 | + int optname); |
| 1227 | +int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request, |
| 1228 | + struct socket *sock); |
| 1229 | + |
| 1230 | + |
| 1231 | +static inline void aa_free_net_rules(struct aa_net *new) |
| 1232 | +{ |
| 1233 | + /* NOP */ |
| 1234 | +} |
| 1235 | + |
| 1236 | +#endif /* __AA_NET_H */ |
| 1237 | diff --git a/security/apparmor/include/path.h b/security/apparmor/include/path.h |
| 1238 | index 05fb3305..26762db2 100644 |
| 1239 | --- a/security/apparmor/include/path.h |
| 1240 | +++ b/security/apparmor/include/path.h |
| 1241 | @@ -18,6 +18,7 @@ |
| 1242 | |
| 1243 | enum path_flags { |
| 1244 | PATH_IS_DIR = 0x1, /* path is a directory */ |
| 1245 | + PATH_SOCK_COND = 0x2, |
| 1246 | PATH_CONNECT_PATH = 0x4, /* connect disconnected paths to / */ |
| 1247 | PATH_CHROOT_REL = 0x8, /* do path lookup relative to chroot */ |
| 1248 | PATH_CHROOT_NSCONNECT = 0x10, /* connect paths that are at ns root */ |
| 1249 | diff --git a/security/apparmor/include/perms.h b/security/apparmor/include/perms.h |
| 1250 | index 2b27bb79..af04d5a7 100644 |
| 1251 | --- a/security/apparmor/include/perms.h |
| 1252 | +++ b/security/apparmor/include/perms.h |
| 1253 | @@ -135,9 +135,10 @@ extern struct aa_perms allperms; |
| 1254 | |
| 1255 | |
| 1256 | void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask); |
| 1257 | -void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask); |
| 1258 | +void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names, |
| 1259 | + u32 mask); |
| 1260 | void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs, |
| 1261 | - u32 chrsmask, const char **names, u32 namesmask); |
| 1262 | + u32 chrsmask, const char * const *names, u32 namesmask); |
| 1263 | void aa_apply_modes_to_perms(struct aa_profile *profile, |
| 1264 | struct aa_perms *perms); |
| 1265 | void aa_compute_perms(struct aa_dfa *dfa, unsigned int state, |
| 1266 | diff --git a/security/apparmor/include/policy.h b/security/apparmor/include/policy.h |
| 1267 | index 17fe41a9..26660a1a 100644 |
| 1268 | --- a/security/apparmor/include/policy.h |
| 1269 | +++ b/security/apparmor/include/policy.h |
| 1270 | @@ -30,6 +30,7 @@ |
| 1271 | #include "file.h" |
| 1272 | #include "lib.h" |
| 1273 | #include "label.h" |
| 1274 | +#include "net.h" |
| 1275 | #include "perms.h" |
| 1276 | #include "resource.h" |
| 1277 | |
| 1278 | @@ -111,6 +112,7 @@ struct aa_data { |
| 1279 | * @policy: general match rules governing policy |
| 1280 | * @file: The set of rules governing basic file access and domain transitions |
| 1281 | * @caps: capabilities for the profile |
| 1282 | + * @net: network controls for the profile |
| 1283 | * @rlimits: rlimits for the profile |
| 1284 | * |
| 1285 | * @dents: dentries for the profiles file entries in apparmorfs |
| 1286 | @@ -148,6 +150,7 @@ struct aa_profile { |
| 1287 | struct aa_policydb policy; |
| 1288 | struct aa_file_rules file; |
| 1289 | struct aa_caps caps; |
| 1290 | + struct aa_net net; |
| 1291 | struct aa_rlimit rlimits; |
| 1292 | |
| 1293 | struct aa_loaddata *rawdata; |
| 1294 | @@ -220,6 +223,16 @@ static inline unsigned int PROFILE_MEDIATES_SAFE(struct aa_profile *profile, |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | +static inline unsigned int PROFILE_MEDIATES_AF(struct aa_profile *profile, |
| 1299 | + u16 AF) { |
| 1300 | + unsigned int state = PROFILE_MEDIATES(profile, AA_CLASS_NET); |
| 1301 | + __be16 be_af = cpu_to_be16(AF); |
| 1302 | + |
| 1303 | + if (!state) |
| 1304 | + return 0; |
| 1305 | + return aa_dfa_match_len(profile->policy.dfa, state, (char *) &be_af, 2); |
| 1306 | +} |
| 1307 | + |
| 1308 | /** |
| 1309 | * aa_get_profile - increment refcount on profile @p |
| 1310 | * @p: profile (MAYBE NULL) |
| 1311 | diff --git a/security/apparmor/include/sig_names.h b/security/apparmor/include/sig_names.h |
| 1312 | index 92e62fe9..0d4395f2 100644 |
| 1313 | --- a/security/apparmor/include/sig_names.h |
| 1314 | +++ b/security/apparmor/include/sig_names.h |
| 1315 | @@ -23,9 +23,7 @@ static const int sig_map[MAXMAPPED_SIG] = { |
| 1316 | [SIGPIPE] = 13, |
| 1317 | [SIGALRM] = 14, |
| 1318 | [SIGTERM] = 15, |
| 1319 | -#ifdef SIGSTKFLT |
| 1320 | [SIGSTKFLT] = 16, /* -, 16, - */ |
| 1321 | -#endif |
| 1322 | [SIGCHLD] = 17, /* 20, 17, 18. SIGCHLD -, -, 18 */ |
| 1323 | [SIGCONT] = 18, /* 19, 18, 25 */ |
| 1324 | [SIGSTOP] = 19, /* 17, 19, 23 */ |
| 1325 | @@ -49,8 +47,7 @@ static const int sig_map[MAXMAPPED_SIG] = { |
| 1326 | #if defined(SIGLOST) && SIGPWR != SIGLOST /* sparc */ |
| 1327 | [SIGLOST] = 33, /* unused on Linux */ |
| 1328 | #endif |
| 1329 | -#if defined(SIGUNUSED) && \ |
| 1330 | - defined(SIGLOST) && defined(SIGSYS) && SIGLOST != SIGSYS |
| 1331 | +#if defined(SIGLOST) && defined(SIGSYS) && SIGLOST != SIGSYS |
| 1332 | [SIGUNUSED] = 34, /* -, 31, - */ |
| 1333 | #endif |
| 1334 | }; |
| 1335 | diff --git a/security/apparmor/ipc.c b/security/apparmor/ipc.c |
| 1336 | index 7ca0032e..66fb9ede 100644 |
| 1337 | --- a/security/apparmor/ipc.c |
| 1338 | +++ b/security/apparmor/ipc.c |
| 1339 | @@ -128,7 +128,7 @@ static inline int map_signal_num(int sig) |
| 1340 | return SIGUNKNOWN; |
| 1341 | else if (sig >= SIGRTMIN) |
| 1342 | return sig - SIGRTMIN + 128; /* rt sigs mapped to 128 */ |
| 1343 | - else if (sig < MAXMAPPED_SIG) |
| 1344 | + else if (sig <= MAXMAPPED_SIG) |
| 1345 | return sig_map[sig]; |
| 1346 | return SIGUNKNOWN; |
| 1347 | } |
| 1348 | @@ -163,7 +163,7 @@ static void audit_signal_cb(struct audit_buffer *ab, void *va) |
| 1349 | audit_signal_mask(ab, aad(sa)->denied); |
| 1350 | } |
| 1351 | } |
| 1352 | - if (aad(sa)->signal < MAXMAPPED_SIG) |
| 1353 | + if (aad(sa)->signal <= MAXMAPPED_SIG) |
| 1354 | audit_log_format(ab, " signal=%s", sig_names[aad(sa)->signal]); |
| 1355 | else |
| 1356 | audit_log_format(ab, " signal=rtmin+%d", |
| 1357 | diff --git a/security/apparmor/lib.c b/security/apparmor/lib.c |
| 1358 | index 08ca26bc..8818621b 100644 |
| 1359 | --- a/security/apparmor/lib.c |
| 1360 | +++ b/security/apparmor/lib.c |
| 1361 | @@ -211,7 +211,8 @@ void aa_perm_mask_to_str(char *str, const char *chrs, u32 mask) |
| 1362 | *str = '\0'; |
| 1363 | } |
| 1364 | |
| 1365 | -void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask) |
| 1366 | +void aa_audit_perm_names(struct audit_buffer *ab, const char * const *names, |
| 1367 | + u32 mask) |
| 1368 | { |
| 1369 | const char *fmt = "%s"; |
| 1370 | unsigned int i, perm = 1; |
| 1371 | @@ -229,7 +230,7 @@ void aa_audit_perm_names(struct audit_buffer *ab, const char **names, u32 mask) |
| 1372 | } |
| 1373 | |
| 1374 | void aa_audit_perm_mask(struct audit_buffer *ab, u32 mask, const char *chrs, |
| 1375 | - u32 chrsmask, const char **names, u32 namesmask) |
| 1376 | + u32 chrsmask, const char * const *names, u32 namesmask) |
| 1377 | { |
| 1378 | char str[33]; |
| 1379 | |
| 1380 | diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c |
| 1381 | index 1346ee5b..5533d2f1 100644 |
| 1382 | --- a/security/apparmor/lsm.c |
| 1383 | +++ b/security/apparmor/lsm.c |
| 1384 | @@ -26,6 +26,7 @@ |
| 1385 | #include <linux/kmemleak.h> |
| 1386 | #include <net/sock.h> |
| 1387 | |
| 1388 | +#include "include/af_unix.h" |
| 1389 | #include "include/apparmor.h" |
| 1390 | #include "include/apparmorfs.h" |
| 1391 | #include "include/audit.h" |
| 1392 | @@ -33,6 +34,7 @@ |
| 1393 | #include "include/context.h" |
| 1394 | #include "include/file.h" |
| 1395 | #include "include/ipc.h" |
| 1396 | +#include "include/net.h" |
| 1397 | #include "include/path.h" |
| 1398 | #include "include/label.h" |
| 1399 | #include "include/policy.h" |
| 1400 | @@ -736,6 +738,411 @@ static int apparmor_task_kill(struct task_struct *target, struct siginfo *info, |
| 1401 | return error; |
| 1402 | } |
| 1403 | |
| 1404 | +/** |
| 1405 | + * apparmor_sk_alloc_security - allocate and attach the sk_security field |
| 1406 | + */ |
| 1407 | +static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags) |
| 1408 | +{ |
| 1409 | + struct aa_sk_ctx *ctx; |
| 1410 | + |
| 1411 | + ctx = kzalloc(sizeof(*ctx), flags); |
| 1412 | + if (!ctx) |
| 1413 | + return -ENOMEM; |
| 1414 | + |
| 1415 | + SK_CTX(sk) = ctx; |
| 1416 | + |
| 1417 | + return 0; |
| 1418 | +} |
| 1419 | + |
| 1420 | +/** |
| 1421 | + * apparmor_sk_free_security - free the sk_security field |
| 1422 | + */ |
| 1423 | +static void apparmor_sk_free_security(struct sock *sk) |
| 1424 | +{ |
| 1425 | + struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1426 | + |
| 1427 | + SK_CTX(sk) = NULL; |
| 1428 | + aa_put_label(ctx->label); |
| 1429 | + aa_put_label(ctx->peer); |
| 1430 | + path_put(&ctx->path); |
| 1431 | + kfree(ctx); |
| 1432 | +} |
| 1433 | + |
| 1434 | +/** |
| 1435 | + * apparmor_clone_security - clone the sk_security field |
| 1436 | + */ |
| 1437 | +static void apparmor_sk_clone_security(const struct sock *sk, |
| 1438 | + struct sock *newsk) |
| 1439 | +{ |
| 1440 | + struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1441 | + struct aa_sk_ctx *new = SK_CTX(newsk); |
| 1442 | + |
| 1443 | + new->label = aa_get_label(ctx->label); |
| 1444 | + new->peer = aa_get_label(ctx->peer); |
| 1445 | + new->path = ctx->path; |
| 1446 | + path_get(&new->path); |
| 1447 | +} |
| 1448 | + |
| 1449 | +static struct path *UNIX_FS_CONN_PATH(struct sock *sk, struct sock *newsk) |
| 1450 | +{ |
| 1451 | + if (sk->sk_family == PF_UNIX && UNIX_FS(sk)) |
| 1452 | + return &unix_sk(sk)->path; |
| 1453 | + else if (newsk->sk_family == PF_UNIX && UNIX_FS(newsk)) |
| 1454 | + return &unix_sk(newsk)->path; |
| 1455 | + return NULL; |
| 1456 | +} |
| 1457 | + |
| 1458 | +/** |
| 1459 | + * apparmor_unix_stream_connect - check perms before making unix domain conn |
| 1460 | + * |
| 1461 | + * peer is locked when this hook is called |
| 1462 | + */ |
| 1463 | +static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk, |
| 1464 | + struct sock *newsk) |
| 1465 | +{ |
| 1466 | + struct aa_sk_ctx *sk_ctx = SK_CTX(sk); |
| 1467 | + struct aa_sk_ctx *peer_ctx = SK_CTX(peer_sk); |
| 1468 | + struct aa_sk_ctx *new_ctx = SK_CTX(newsk); |
| 1469 | + struct aa_label *label; |
| 1470 | + struct path *path; |
| 1471 | + int error; |
| 1472 | + |
| 1473 | + label = __begin_current_label_crit_section(); |
| 1474 | + error = aa_unix_peer_perm(label, OP_CONNECT, |
| 1475 | + (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE), |
| 1476 | + sk, peer_sk, NULL); |
| 1477 | + if (!UNIX_FS(peer_sk)) { |
| 1478 | + last_error(error, |
| 1479 | + aa_unix_peer_perm(peer_ctx->label, OP_CONNECT, |
| 1480 | + (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE), |
| 1481 | + peer_sk, sk, label)); |
| 1482 | + } |
| 1483 | + __end_current_label_crit_section(label); |
| 1484 | + |
| 1485 | + if (error) |
| 1486 | + return error; |
| 1487 | + |
| 1488 | + /* label newsk if it wasn't labeled in post_create. Normally this |
| 1489 | + * would be done in sock_graft, but because we are directly looking |
| 1490 | + * at the peer_sk to obtain peer_labeling for unix socks this |
| 1491 | + * does not work |
| 1492 | + */ |
| 1493 | + if (!new_ctx->label) |
| 1494 | + new_ctx->label = aa_get_label(peer_ctx->label); |
| 1495 | + |
| 1496 | + /* Cross reference the peer labels for SO_PEERSEC */ |
| 1497 | + if (new_ctx->peer) |
| 1498 | + aa_put_label(new_ctx->peer); |
| 1499 | + |
| 1500 | + if (sk_ctx->peer) |
| 1501 | + aa_put_label(sk_ctx->peer); |
| 1502 | + |
| 1503 | + new_ctx->peer = aa_get_label(sk_ctx->label); |
| 1504 | + sk_ctx->peer = aa_get_label(peer_ctx->label); |
| 1505 | + |
| 1506 | + path = UNIX_FS_CONN_PATH(sk, peer_sk); |
| 1507 | + if (path) { |
| 1508 | + new_ctx->path = *path; |
| 1509 | + sk_ctx->path = *path; |
| 1510 | + path_get(path); |
| 1511 | + path_get(path); |
| 1512 | + } |
| 1513 | + return 0; |
| 1514 | +} |
| 1515 | + |
| 1516 | +/** |
| 1517 | + * apparmor_unix_may_send - check perms before conn or sending unix dgrams |
| 1518 | + * |
| 1519 | + * other is locked when this hook is called |
| 1520 | + * |
| 1521 | + * dgram connect calls may_send, peer setup but path not copied????? |
| 1522 | + */ |
| 1523 | +static int apparmor_unix_may_send(struct socket *sock, struct socket *peer) |
| 1524 | +{ |
| 1525 | + struct aa_sk_ctx *peer_ctx = SK_CTX(peer->sk); |
| 1526 | + struct aa_label *label; |
| 1527 | + int error; |
| 1528 | + |
| 1529 | + label = __begin_current_label_crit_section(); |
| 1530 | + error = xcheck(aa_unix_peer_perm(label, OP_SENDMSG, AA_MAY_SEND, |
| 1531 | + sock->sk, peer->sk, NULL), |
| 1532 | + aa_unix_peer_perm(peer_ctx->label, OP_SENDMSG, |
| 1533 | + AA_MAY_RECEIVE, |
| 1534 | + peer->sk, sock->sk, label)); |
| 1535 | + __end_current_label_crit_section(label); |
| 1536 | + |
| 1537 | + return error; |
| 1538 | +} |
| 1539 | + |
| 1540 | +/** |
| 1541 | + * apparmor_socket_create - check perms before creating a new socket |
| 1542 | + */ |
| 1543 | +static int apparmor_socket_create(int family, int type, int protocol, int kern) |
| 1544 | +{ |
| 1545 | + struct aa_label *label; |
| 1546 | + int error = 0; |
| 1547 | + |
| 1548 | + label = begin_current_label_crit_section(); |
| 1549 | + if (!(kern || unconfined(label))) |
| 1550 | + error = aa_sock_create_perm(label, family, type, protocol); |
| 1551 | + end_current_label_crit_section(label); |
| 1552 | + |
| 1553 | + return error; |
| 1554 | +} |
| 1555 | + |
| 1556 | +/** |
| 1557 | + * apparmor_socket_post_create - setup the per-socket security struct |
| 1558 | + * |
| 1559 | + * Note: |
| 1560 | + * - kernel sockets currently labeled unconfined but we may want to |
| 1561 | + * move to a special kernel label |
| 1562 | + * - socket may not have sk here if created with sock_create_lite or |
| 1563 | + * sock_alloc. These should be accept cases which will be handled in |
| 1564 | + * sock_graft. |
| 1565 | + */ |
| 1566 | +static int apparmor_socket_post_create(struct socket *sock, int family, |
| 1567 | + int type, int protocol, int kern) |
| 1568 | +{ |
| 1569 | + struct aa_label *label; |
| 1570 | + |
| 1571 | + if (kern) { |
| 1572 | + struct aa_ns *ns = aa_get_current_ns(); |
| 1573 | + |
| 1574 | + label = aa_get_label(ns_unconfined(ns)); |
| 1575 | + aa_put_ns(ns); |
| 1576 | + } else |
| 1577 | + label = aa_get_current_label(); |
| 1578 | + |
| 1579 | + if (sock->sk) { |
| 1580 | + struct aa_sk_ctx *ctx = SK_CTX(sock->sk); |
| 1581 | + |
| 1582 | + aa_put_label(ctx->label); |
| 1583 | + ctx->label = aa_get_label(label); |
| 1584 | + } |
| 1585 | + aa_put_label(label); |
| 1586 | + |
| 1587 | + return 0; |
| 1588 | +} |
| 1589 | + |
| 1590 | +/** |
| 1591 | + * apparmor_socket_bind - check perms before bind addr to socket |
| 1592 | + */ |
| 1593 | +static int apparmor_socket_bind(struct socket *sock, |
| 1594 | + struct sockaddr *address, int addrlen) |
| 1595 | +{ |
| 1596 | + return aa_sock_bind_perm(sock, address, addrlen); |
| 1597 | +} |
| 1598 | + |
| 1599 | +/** |
| 1600 | + * apparmor_socket_connect - check perms before connecting @sock to @address |
| 1601 | + */ |
| 1602 | +static int apparmor_socket_connect(struct socket *sock, |
| 1603 | + struct sockaddr *address, int addrlen) |
| 1604 | +{ |
| 1605 | + return aa_sock_connect_perm(sock, address, addrlen); |
| 1606 | +} |
| 1607 | + |
| 1608 | +/** |
| 1609 | + * apparmor_socket_list - check perms before allowing listen |
| 1610 | + */ |
| 1611 | +static int apparmor_socket_listen(struct socket *sock, int backlog) |
| 1612 | +{ |
| 1613 | + return aa_sock_listen_perm(sock, backlog); |
| 1614 | +} |
| 1615 | + |
| 1616 | +/** |
| 1617 | + * apparmor_socket_accept - check perms before accepting a new connection. |
| 1618 | + * |
| 1619 | + * Note: while @newsock is created and has some information, the accept |
| 1620 | + * has not been done. |
| 1621 | + */ |
| 1622 | +static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) |
| 1623 | +{ |
| 1624 | + return aa_sock_accept_perm(sock, newsock); |
| 1625 | +} |
| 1626 | + |
| 1627 | +/** |
| 1628 | + * apparmor_socket_sendmsg - check perms before sending msg to another socket |
| 1629 | + */ |
| 1630 | +static int apparmor_socket_sendmsg(struct socket *sock, |
| 1631 | + struct msghdr *msg, int size) |
| 1632 | +{ |
| 1633 | + return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); |
| 1634 | +} |
| 1635 | + |
| 1636 | +/** |
| 1637 | + * apparmor_socket_recvmsg - check perms before receiving a message |
| 1638 | + */ |
| 1639 | +static int apparmor_socket_recvmsg(struct socket *sock, |
| 1640 | + struct msghdr *msg, int size, int flags) |
| 1641 | +{ |
| 1642 | + return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); |
| 1643 | +} |
| 1644 | + |
| 1645 | +/** |
| 1646 | + * apparmor_socket_getsockname - check perms before getting the local address |
| 1647 | + */ |
| 1648 | +static int apparmor_socket_getsockname(struct socket *sock) |
| 1649 | +{ |
| 1650 | + return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); |
| 1651 | +} |
| 1652 | + |
| 1653 | +/** |
| 1654 | + * apparmor_socket_getpeername - check perms before getting remote address |
| 1655 | + */ |
| 1656 | +static int apparmor_socket_getpeername(struct socket *sock) |
| 1657 | +{ |
| 1658 | + return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); |
| 1659 | +} |
| 1660 | + |
| 1661 | +/** |
| 1662 | + * apparmor_getsockopt - check perms before getting socket options |
| 1663 | + */ |
| 1664 | +static int apparmor_socket_getsockopt(struct socket *sock, int level, |
| 1665 | + int optname) |
| 1666 | +{ |
| 1667 | + return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, |
| 1668 | + level, optname); |
| 1669 | +} |
| 1670 | + |
| 1671 | +/** |
| 1672 | + * apparmor_setsockopt - check perms before setting socket options |
| 1673 | + */ |
| 1674 | +static int apparmor_socket_setsockopt(struct socket *sock, int level, |
| 1675 | + int optname) |
| 1676 | +{ |
| 1677 | + return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, |
| 1678 | + level, optname); |
| 1679 | +} |
| 1680 | + |
| 1681 | +/** |
| 1682 | + * apparmor_socket_shutdown - check perms before shutting down @sock conn |
| 1683 | + */ |
| 1684 | +static int apparmor_socket_shutdown(struct socket *sock, int how) |
| 1685 | +{ |
| 1686 | + return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); |
| 1687 | +} |
| 1688 | + |
| 1689 | +/** |
| 1690 | + * apparmor_socket_sock_recv_skb - check perms before associating skb to sk |
| 1691 | + * |
| 1692 | + * Note: can not sleep may be called with locks held |
| 1693 | + * |
| 1694 | + * dont want protocol specific in __skb_recv_datagram() |
| 1695 | + * to deny an incoming connection socket_sock_rcv_skb() |
| 1696 | + */ |
| 1697 | +static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 1698 | +{ |
| 1699 | + return 0; |
| 1700 | +} |
| 1701 | + |
| 1702 | + |
| 1703 | +static struct aa_label *sk_peer_label(struct sock *sk) |
| 1704 | +{ |
| 1705 | + struct sock *peer_sk; |
| 1706 | + struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1707 | + |
| 1708 | + if (ctx->peer) |
| 1709 | + return ctx->peer; |
| 1710 | + |
| 1711 | + if (sk->sk_family != PF_UNIX) |
| 1712 | + return ERR_PTR(-ENOPROTOOPT); |
| 1713 | + |
| 1714 | + /* check for sockpair peering which does not go through |
| 1715 | + * security_unix_stream_connect |
| 1716 | + */ |
| 1717 | + peer_sk = unix_peer(sk); |
| 1718 | + if (peer_sk) { |
| 1719 | + ctx = SK_CTX(peer_sk); |
| 1720 | + if (ctx->label) |
| 1721 | + return ctx->label; |
| 1722 | + } |
| 1723 | + |
| 1724 | + return ERR_PTR(-ENOPROTOOPT); |
| 1725 | +} |
| 1726 | + |
| 1727 | +/** |
| 1728 | + * apparmor_socket_getpeersec_stream - get security context of peer |
| 1729 | + * |
| 1730 | + * Note: for tcp only valid if using ipsec or cipso on lan |
| 1731 | + */ |
| 1732 | +static int apparmor_socket_getpeersec_stream(struct socket *sock, |
| 1733 | + char __user *optval, |
| 1734 | + int __user *optlen, |
| 1735 | + unsigned int len) |
| 1736 | +{ |
| 1737 | + char *name; |
| 1738 | + int slen, error = 0; |
| 1739 | + struct aa_label *label; |
| 1740 | + struct aa_label *peer; |
| 1741 | + |
| 1742 | + label = begin_current_label_crit_section(); |
| 1743 | + peer = sk_peer_label(sock->sk); |
| 1744 | + if (IS_ERR(peer)) { |
| 1745 | + error = PTR_ERR(peer); |
| 1746 | + goto done; |
| 1747 | + } |
| 1748 | + slen = aa_label_asxprint(&name, labels_ns(label), peer, |
| 1749 | + FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | |
| 1750 | + FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); |
| 1751 | + /* don't include terminating \0 in slen, it breaks some apps */ |
| 1752 | + if (slen < 0) { |
| 1753 | + error = -ENOMEM; |
| 1754 | + } else { |
| 1755 | + if (slen > len) { |
| 1756 | + error = -ERANGE; |
| 1757 | + } else if (copy_to_user(optval, name, slen)) { |
| 1758 | + error = -EFAULT; |
| 1759 | + goto out; |
| 1760 | + } |
| 1761 | + if (put_user(slen, optlen)) |
| 1762 | + error = -EFAULT; |
| 1763 | +out: |
| 1764 | + kfree(name); |
| 1765 | + |
| 1766 | + } |
| 1767 | + |
| 1768 | +done: |
| 1769 | + end_current_label_crit_section(label); |
| 1770 | + |
| 1771 | + return error; |
| 1772 | +} |
| 1773 | + |
| 1774 | +/** |
| 1775 | + * apparmor_socket_getpeersec_dgram - get security label of packet |
| 1776 | + * @sock: the peer socket |
| 1777 | + * @skb: packet data |
| 1778 | + * @secid: pointer to where to put the secid of the packet |
| 1779 | + * |
| 1780 | + * Sets the netlabel socket state on sk from parent |
| 1781 | + */ |
| 1782 | +static int apparmor_socket_getpeersec_dgram(struct socket *sock, |
| 1783 | + struct sk_buff *skb, u32 *secid) |
| 1784 | + |
| 1785 | +{ |
| 1786 | + /* TODO: requires secid support */ |
| 1787 | + return -ENOPROTOOPT; |
| 1788 | +} |
| 1789 | + |
| 1790 | +/** |
| 1791 | + * apparmor_sock_graft - Initialize newly created socket |
| 1792 | + * @sk: child sock |
| 1793 | + * @parent: parent socket |
| 1794 | + * |
| 1795 | + * Note: could set off of SOCK_CTX(parent) but need to track inode and we can |
| 1796 | + * just set sk security information off of current creating process label |
| 1797 | + * Labeling of sk for accept case - probably should be sock based |
| 1798 | + * instead of task, because of the case where an implicitly labeled |
| 1799 | + * socket is shared by different tasks. |
| 1800 | + */ |
| 1801 | +static void apparmor_sock_graft(struct sock *sk, struct socket *parent) |
| 1802 | +{ |
| 1803 | + struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1804 | + |
| 1805 | + if (!ctx->label) |
| 1806 | + ctx->label = aa_get_current_label(); |
| 1807 | +} |
| 1808 | + |
| 1809 | static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
| 1810 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
| 1811 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), |
| 1812 | @@ -770,6 +1177,33 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
| 1813 | LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), |
| 1814 | LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), |
| 1815 | |
| 1816 | + LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), |
| 1817 | + LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), |
| 1818 | + LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), |
| 1819 | + |
| 1820 | + LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect), |
| 1821 | + LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send), |
| 1822 | + |
| 1823 | + LSM_HOOK_INIT(socket_create, apparmor_socket_create), |
| 1824 | + LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), |
| 1825 | + LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), |
| 1826 | + LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), |
| 1827 | + LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), |
| 1828 | + LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), |
| 1829 | + LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), |
| 1830 | + LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), |
| 1831 | + LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), |
| 1832 | + LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), |
| 1833 | + LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), |
| 1834 | + LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), |
| 1835 | + LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), |
| 1836 | + LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), |
| 1837 | + LSM_HOOK_INIT(socket_getpeersec_stream, |
| 1838 | + apparmor_socket_getpeersec_stream), |
| 1839 | + LSM_HOOK_INIT(socket_getpeersec_dgram, |
| 1840 | + apparmor_socket_getpeersec_dgram), |
| 1841 | + LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), |
| 1842 | + |
| 1843 | LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), |
| 1844 | LSM_HOOK_INIT(cred_free, apparmor_cred_free), |
| 1845 | LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), |
| 1846 | diff --git a/security/apparmor/net.c b/security/apparmor/net.c |
| 1847 | new file mode 100644 |
| 1848 | index 00000000..dd1953b0 |
| 1849 | --- /dev/null |
| 1850 | +++ b/security/apparmor/net.c |
| 1851 | @@ -0,0 +1,356 @@ |
| 1852 | +/* |
| 1853 | + * AppArmor security module |
| 1854 | + * |
| 1855 | + * This file contains AppArmor network mediation |
| 1856 | + * |
| 1857 | + * Copyright (C) 1998-2008 Novell/SUSE |
| 1858 | + * Copyright 2009-2017 Canonical Ltd. |
| 1859 | + * |
| 1860 | + * This program is free software; you can redistribute it and/or |
| 1861 | + * modify it under the terms of the GNU General Public License as |
| 1862 | + * published by the Free Software Foundation, version 2 of the |
| 1863 | + * License. |
| 1864 | + */ |
| 1865 | + |
| 1866 | +#include "include/af_unix.h" |
| 1867 | +#include "include/apparmor.h" |
| 1868 | +#include "include/audit.h" |
| 1869 | +#include "include/context.h" |
| 1870 | +#include "include/label.h" |
| 1871 | +#include "include/net.h" |
| 1872 | +#include "include/policy.h" |
| 1873 | + |
| 1874 | +#include "net_names.h" |
| 1875 | + |
| 1876 | + |
| 1877 | +struct aa_sfs_entry aa_sfs_entry_network[] = { |
| 1878 | + AA_SFS_FILE_STRING("af_mask", AA_SFS_AF_MASK), |
| 1879 | + AA_SFS_FILE_BOOLEAN("af_unix", 1), |
| 1880 | + { } |
| 1881 | +}; |
| 1882 | + |
| 1883 | +static const char * const net_mask_names[] = { |
| 1884 | + "unknown", |
| 1885 | + "send", |
| 1886 | + "receive", |
| 1887 | + "unknown", |
| 1888 | + |
| 1889 | + "create", |
| 1890 | + "shutdown", |
| 1891 | + "connect", |
| 1892 | + "unknown", |
| 1893 | + |
| 1894 | + "setattr", |
| 1895 | + "getattr", |
| 1896 | + "setcred", |
| 1897 | + "getcred", |
| 1898 | + |
| 1899 | + "chmod", |
| 1900 | + "chown", |
| 1901 | + "chgrp", |
| 1902 | + "lock", |
| 1903 | + |
| 1904 | + "mmap", |
| 1905 | + "mprot", |
| 1906 | + "unknown", |
| 1907 | + "unknown", |
| 1908 | + |
| 1909 | + "accept", |
| 1910 | + "bind", |
| 1911 | + "listen", |
| 1912 | + "unknown", |
| 1913 | + |
| 1914 | + "setopt", |
| 1915 | + "getopt", |
| 1916 | + "unknown", |
| 1917 | + "unknown", |
| 1918 | + |
| 1919 | + "unknown", |
| 1920 | + "unknown", |
| 1921 | + "unknown", |
| 1922 | + "unknown", |
| 1923 | +}; |
| 1924 | + |
| 1925 | +static void audit_unix_addr(struct audit_buffer *ab, const char *str, |
| 1926 | + struct sockaddr_un *addr, int addrlen) |
| 1927 | +{ |
| 1928 | + int len = unix_addr_len(addrlen); |
| 1929 | + |
| 1930 | + if (!addr || len <= 0) { |
| 1931 | + audit_log_format(ab, " %s=none", str); |
| 1932 | + } else if (addr->sun_path[0]) { |
| 1933 | + audit_log_format(ab, " %s=", str); |
| 1934 | + audit_log_untrustedstring(ab, addr->sun_path); |
| 1935 | + } else { |
| 1936 | + audit_log_format(ab, " %s=\"@", str); |
| 1937 | + if (audit_string_contains_control(&addr->sun_path[1], len - 1)) |
| 1938 | + audit_log_n_hex(ab, &addr->sun_path[1], len - 1); |
| 1939 | + else |
| 1940 | + audit_log_format(ab, "%.*s", len - 1, |
| 1941 | + &addr->sun_path[1]); |
| 1942 | + audit_log_format(ab, "\""); |
| 1943 | + } |
| 1944 | +} |
| 1945 | + |
| 1946 | +static void audit_unix_sk_addr(struct audit_buffer *ab, const char *str, |
| 1947 | + struct sock *sk) |
| 1948 | +{ |
| 1949 | + struct unix_sock *u = unix_sk(sk); |
| 1950 | + if (u && u->addr) |
| 1951 | + audit_unix_addr(ab, str, u->addr->name, u->addr->len); |
| 1952 | + else |
| 1953 | + audit_unix_addr(ab, str, NULL, 0); |
| 1954 | +} |
| 1955 | + |
| 1956 | +/* audit callback for net specific fields */ |
| 1957 | +void audit_net_cb(struct audit_buffer *ab, void *va) |
| 1958 | +{ |
| 1959 | + struct common_audit_data *sa = va; |
| 1960 | + |
| 1961 | + audit_log_format(ab, " family="); |
| 1962 | + if (address_family_names[sa->u.net->family]) |
| 1963 | + audit_log_string(ab, address_family_names[sa->u.net->family]); |
| 1964 | + else |
| 1965 | + audit_log_format(ab, "\"unknown(%d)\"", sa->u.net->family); |
| 1966 | + audit_log_format(ab, " sock_type="); |
| 1967 | + if (sock_type_names[aad(sa)->net.type]) |
| 1968 | + audit_log_string(ab, sock_type_names[aad(sa)->net.type]); |
| 1969 | + else |
| 1970 | + audit_log_format(ab, "\"unknown(%d)\"", aad(sa)->net.type); |
| 1971 | + audit_log_format(ab, " protocol=%d", aad(sa)->net.protocol); |
| 1972 | + |
| 1973 | + if (aad(sa)->request & NET_PERMS_MASK) { |
| 1974 | + audit_log_format(ab, " requested_mask="); |
| 1975 | + aa_audit_perm_mask(ab, aad(sa)->request, NULL, 0, |
| 1976 | + net_mask_names, NET_PERMS_MASK); |
| 1977 | + |
| 1978 | + if (aad(sa)->denied & NET_PERMS_MASK) { |
| 1979 | + audit_log_format(ab, " denied_mask="); |
| 1980 | + aa_audit_perm_mask(ab, aad(sa)->denied, NULL, 0, |
| 1981 | + net_mask_names, NET_PERMS_MASK); |
| 1982 | + } |
| 1983 | + } |
| 1984 | + if (sa->u.net->family == AF_UNIX) { |
| 1985 | + if ((aad(sa)->request & ~NET_PEER_MASK) && aad(sa)->net.addr) |
| 1986 | + audit_unix_addr(ab, "addr", |
| 1987 | + unix_addr(aad(sa)->net.addr), |
| 1988 | + aad(sa)->net.addrlen); |
| 1989 | + else |
| 1990 | + audit_unix_sk_addr(ab, "addr", sa->u.net->sk); |
| 1991 | + if (aad(sa)->request & NET_PEER_MASK) { |
| 1992 | + if (aad(sa)->net.addr) |
| 1993 | + audit_unix_addr(ab, "peer_addr", |
| 1994 | + unix_addr(aad(sa)->net.addr), |
| 1995 | + aad(sa)->net.addrlen); |
| 1996 | + else |
| 1997 | + audit_unix_sk_addr(ab, "peer_addr", |
| 1998 | + aad(sa)->net.peer_sk); |
| 1999 | + } |
| 2000 | + } |
| 2001 | + if (aad(sa)->peer) { |
| 2002 | + audit_log_format(ab, " peer="); |
| 2003 | + aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer, |
| 2004 | + FLAGS_NONE, GFP_ATOMIC); |
| 2005 | + } |
| 2006 | +} |
| 2007 | + |
| 2008 | + |
| 2009 | +/* Generic af perm */ |
| 2010 | +int aa_profile_af_perm(struct aa_profile *profile, struct common_audit_data *sa, |
| 2011 | + u32 request, u16 family, int type) |
| 2012 | +{ |
| 2013 | + struct aa_perms perms = { }; |
| 2014 | + |
| 2015 | + AA_BUG(family >= AF_MAX); |
| 2016 | + AA_BUG(type < 0 || type >= SOCK_MAX); |
| 2017 | + |
| 2018 | + if (profile_unconfined(profile)) |
| 2019 | + return 0; |
| 2020 | + |
| 2021 | + perms.allow = (profile->net.allow[family] & (1 << type)) ? |
| 2022 | + ALL_PERMS_MASK : 0; |
| 2023 | + perms.audit = (profile->net.audit[family] & (1 << type)) ? |
| 2024 | + ALL_PERMS_MASK : 0; |
| 2025 | + perms.quiet = (profile->net.quiet[family] & (1 << type)) ? |
| 2026 | + ALL_PERMS_MASK : 0; |
| 2027 | + aa_apply_modes_to_perms(profile, &perms); |
| 2028 | + |
| 2029 | + return aa_check_perms(profile, &perms, request, sa, audit_net_cb); |
| 2030 | +} |
| 2031 | + |
| 2032 | +int aa_af_perm(struct aa_label *label, const char *op, u32 request, u16 family, |
| 2033 | + int type, int protocol) |
| 2034 | +{ |
| 2035 | + struct aa_profile *profile; |
| 2036 | + DEFINE_AUDIT_NET(sa, op, NULL, family, type, protocol); |
| 2037 | + |
| 2038 | + return fn_for_each_confined(label, profile, |
| 2039 | + aa_profile_af_perm(profile, &sa, request, family, |
| 2040 | + type)); |
| 2041 | +} |
| 2042 | + |
| 2043 | +static int aa_label_sk_perm(struct aa_label *label, const char *op, u32 request, |
| 2044 | + struct sock *sk) |
| 2045 | +{ |
| 2046 | + struct aa_profile *profile; |
| 2047 | + DEFINE_AUDIT_SK(sa, op, sk); |
| 2048 | + |
| 2049 | + AA_BUG(!label); |
| 2050 | + AA_BUG(!sk); |
| 2051 | + |
| 2052 | + if (unconfined(label)) |
| 2053 | + return 0; |
| 2054 | + |
| 2055 | + return fn_for_each_confined(label, profile, |
| 2056 | + aa_profile_af_sk_perm(profile, &sa, request, sk)); |
| 2057 | +} |
| 2058 | + |
| 2059 | +int aa_sk_perm(const char *op, u32 request, struct sock *sk) |
| 2060 | +{ |
| 2061 | + struct aa_label *label; |
| 2062 | + int error; |
| 2063 | + |
| 2064 | + AA_BUG(!sk); |
| 2065 | + AA_BUG(in_interrupt()); |
| 2066 | + |
| 2067 | + /* TODO: switch to begin_current_label ???? */ |
| 2068 | + label = begin_current_label_crit_section(); |
| 2069 | + error = aa_label_sk_perm(label, op, request, sk); |
| 2070 | + end_current_label_crit_section(label); |
| 2071 | + |
| 2072 | + return error; |
| 2073 | +} |
| 2074 | + |
| 2075 | +#define af_select(FAMILY, FN, DEF_FN) \ |
| 2076 | +({ \ |
| 2077 | + int __e; \ |
| 2078 | + switch ((FAMILY)) { \ |
| 2079 | + case AF_UNIX: \ |
| 2080 | + __e = aa_unix_ ## FN; \ |
| 2081 | + break; \ |
| 2082 | + default: \ |
| 2083 | + __e = DEF_FN; \ |
| 2084 | + } \ |
| 2085 | + __e; \ |
| 2086 | +}) |
| 2087 | + |
| 2088 | +/* TODO: push into lsm.c ???? */ |
| 2089 | + |
| 2090 | +/* revaliation, get/set attr, shutdown */ |
| 2091 | +int aa_sock_perm(const char *op, u32 request, struct socket *sock) |
| 2092 | +{ |
| 2093 | + AA_BUG(!sock); |
| 2094 | + AA_BUG(!sock->sk); |
| 2095 | + AA_BUG(in_interrupt()); |
| 2096 | + |
| 2097 | + return af_select(sock->sk->sk_family, |
| 2098 | + sock_perm(op, request, sock), |
| 2099 | + aa_sk_perm(op, request, sock->sk)); |
| 2100 | +} |
| 2101 | + |
| 2102 | +int aa_sock_create_perm(struct aa_label *label, int family, int type, |
| 2103 | + int protocol) |
| 2104 | +{ |
| 2105 | + AA_BUG(!label); |
| 2106 | + /* TODO: .... */ |
| 2107 | + AA_BUG(in_interrupt()); |
| 2108 | + |
| 2109 | + return af_select(family, |
| 2110 | + create_perm(label, family, type, protocol), |
| 2111 | + aa_af_perm(label, OP_CREATE, AA_MAY_CREATE, family, |
| 2112 | + type, protocol)); |
| 2113 | +} |
| 2114 | + |
| 2115 | +int aa_sock_bind_perm(struct socket *sock, struct sockaddr *address, |
| 2116 | + int addrlen) |
| 2117 | +{ |
| 2118 | + AA_BUG(!sock); |
| 2119 | + AA_BUG(!sock->sk); |
| 2120 | + AA_BUG(!address); |
| 2121 | + /* TODO: .... */ |
| 2122 | + AA_BUG(in_interrupt()); |
| 2123 | + |
| 2124 | + return af_select(sock->sk->sk_family, |
| 2125 | + bind_perm(sock, address, addrlen), |
| 2126 | + aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); |
| 2127 | +} |
| 2128 | + |
| 2129 | +int aa_sock_connect_perm(struct socket *sock, struct sockaddr *address, |
| 2130 | + int addrlen) |
| 2131 | +{ |
| 2132 | + AA_BUG(!sock); |
| 2133 | + AA_BUG(!sock->sk); |
| 2134 | + AA_BUG(!address); |
| 2135 | + /* TODO: .... */ |
| 2136 | + AA_BUG(in_interrupt()); |
| 2137 | + |
| 2138 | + return af_select(sock->sk->sk_family, |
| 2139 | + connect_perm(sock, address, addrlen), |
| 2140 | + aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); |
| 2141 | +} |
| 2142 | + |
| 2143 | +int aa_sock_listen_perm(struct socket *sock, int backlog) |
| 2144 | +{ |
| 2145 | + AA_BUG(!sock); |
| 2146 | + AA_BUG(!sock->sk); |
| 2147 | + /* TODO: .... */ |
| 2148 | + AA_BUG(in_interrupt()); |
| 2149 | + |
| 2150 | + return af_select(sock->sk->sk_family, |
| 2151 | + listen_perm(sock, backlog), |
| 2152 | + aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); |
| 2153 | +} |
| 2154 | + |
| 2155 | +/* ability of sock to connect, not peer address binding */ |
| 2156 | +int aa_sock_accept_perm(struct socket *sock, struct socket *newsock) |
| 2157 | +{ |
| 2158 | + AA_BUG(!sock); |
| 2159 | + AA_BUG(!sock->sk); |
| 2160 | + AA_BUG(!newsock); |
| 2161 | + /* TODO: .... */ |
| 2162 | + AA_BUG(in_interrupt()); |
| 2163 | + |
| 2164 | + return af_select(sock->sk->sk_family, |
| 2165 | + accept_perm(sock, newsock), |
| 2166 | + aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); |
| 2167 | +} |
| 2168 | + |
| 2169 | +/* sendmsg, recvmsg */ |
| 2170 | +int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, |
| 2171 | + struct msghdr *msg, int size) |
| 2172 | +{ |
| 2173 | + AA_BUG(!sock); |
| 2174 | + AA_BUG(!sock->sk); |
| 2175 | + AA_BUG(!msg); |
| 2176 | + /* TODO: .... */ |
| 2177 | + AA_BUG(in_interrupt()); |
| 2178 | + |
| 2179 | + return af_select(sock->sk->sk_family, |
| 2180 | + msg_perm(op, request, sock, msg, size), |
| 2181 | + aa_sk_perm(op, request, sock->sk)); |
| 2182 | +} |
| 2183 | + |
| 2184 | +/* revaliation, get/set attr, opt */ |
| 2185 | +int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, int level, |
| 2186 | + int optname) |
| 2187 | +{ |
| 2188 | + AA_BUG(!sock); |
| 2189 | + AA_BUG(!sock->sk); |
| 2190 | + AA_BUG(in_interrupt()); |
| 2191 | + |
| 2192 | + return af_select(sock->sk->sk_family, |
| 2193 | + opt_perm(op, request, sock, level, optname), |
| 2194 | + aa_sk_perm(op, request, sock->sk)); |
| 2195 | +} |
| 2196 | + |
| 2197 | +int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request, |
| 2198 | + struct socket *sock) |
| 2199 | +{ |
| 2200 | + AA_BUG(!label); |
| 2201 | + AA_BUG(!sock); |
| 2202 | + AA_BUG(!sock->sk); |
| 2203 | + |
| 2204 | + return af_select(sock->sk->sk_family, |
| 2205 | + file_perm(label, op, request, sock), |
| 2206 | + aa_label_sk_perm(label, op, request, sock->sk)); |
| 2207 | +} |
| 2208 | diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c |
| 2209 | index 4ede87c3..5a2aec35 100644 |
| 2210 | --- a/security/apparmor/policy_unpack.c |
| 2211 | +++ b/security/apparmor/policy_unpack.c |
| 2212 | @@ -275,6 +275,19 @@ static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name) |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
| 2216 | +static bool unpack_u16(struct aa_ext *e, u16 *data, const char *name) |
| 2217 | +{ |
| 2218 | + if (unpack_nameX(e, AA_U16, name)) { |
| 2219 | + if (!inbounds(e, sizeof(u16))) |
| 2220 | + return 0; |
| 2221 | + if (data) |
| 2222 | + *data = le16_to_cpu(get_unaligned((__le16 *) e->pos)); |
| 2223 | + e->pos += sizeof(u16); |
| 2224 | + return 1; |
| 2225 | + } |
| 2226 | + return 0; |
| 2227 | +} |
| 2228 | + |
| 2229 | static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name) |
| 2230 | { |
| 2231 | if (unpack_nameX(e, AA_U32, name)) { |
| 2232 | @@ -584,7 +597,7 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) |
| 2233 | struct aa_profile *profile = NULL; |
| 2234 | const char *tmpname, *tmpns = NULL, *name = NULL; |
| 2235 | const char *info = "failed to unpack profile"; |
| 2236 | - size_t ns_len; |
| 2237 | + size_t size = 0, ns_len; |
| 2238 | struct rhashtable_params params = { 0 }; |
| 2239 | char *key = NULL; |
| 2240 | struct aa_data *data; |
| 2241 | @@ -717,6 +730,38 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) |
| 2242 | goto fail; |
| 2243 | } |
| 2244 | |
| 2245 | + size = unpack_array(e, "net_allowed_af"); |
| 2246 | + if (size) { |
| 2247 | + |
| 2248 | + for (i = 0; i < size; i++) { |
| 2249 | + /* discard extraneous rules that this kernel will |
| 2250 | + * never request |
| 2251 | + */ |
| 2252 | + if (i >= AF_MAX) { |
| 2253 | + u16 tmp; |
| 2254 | + |
| 2255 | + if (!unpack_u16(e, &tmp, NULL) || |
| 2256 | + !unpack_u16(e, &tmp, NULL) || |
| 2257 | + !unpack_u16(e, &tmp, NULL)) |
| 2258 | + goto fail; |
| 2259 | + continue; |
| 2260 | + } |
| 2261 | + if (!unpack_u16(e, &profile->net.allow[i], NULL)) |
| 2262 | + goto fail; |
| 2263 | + if (!unpack_u16(e, &profile->net.audit[i], NULL)) |
| 2264 | + goto fail; |
| 2265 | + if (!unpack_u16(e, &profile->net.quiet[i], NULL)) |
| 2266 | + goto fail; |
| 2267 | + } |
| 2268 | + if (!unpack_nameX(e, AA_ARRAYEND, NULL)) |
| 2269 | + goto fail; |
| 2270 | + } |
| 2271 | + if (VERSION_LT(e->version, v7)) { |
| 2272 | + /* pre v7 policy always allowed these */ |
| 2273 | + profile->net.allow[AF_UNIX] = 0xffff; |
| 2274 | + profile->net.allow[AF_NETLINK] = 0xffff; |
| 2275 | + } |
| 2276 | + |
| 2277 | if (unpack_nameX(e, AA_STRUCT, "policydb")) { |
| 2278 | /* generic policy dfa - optional and may be NULL */ |
| 2279 | info = "failed to unpack policydb"; |
| 2280 | -- |
| 2281 | 2.15.1 |
| 2282 | |
linux-current/files/security/0001-apparmor-Merge-items-still-missing-from-upstream.patch
linux-current/files/security/0001-apparmor-Merge-items-still-missing-from-upstream.patch