net.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor network mediation definitions.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2017 Canonical Ltd.
  9. */
  10. #ifndef __AA_NET_H
  11. #define __AA_NET_H
  12. #include <net/sock.h>
  13. #include <linux/path.h>
  14. #include "apparmorfs.h"
  15. #include "label.h"
  16. #include "perms.h"
  17. #include "policy.h"
  18. #define AA_MAY_SEND AA_MAY_WRITE
  19. #define AA_MAY_RECEIVE AA_MAY_READ
  20. #define AA_MAY_SHUTDOWN AA_MAY_DELETE
  21. #define AA_MAY_CONNECT AA_MAY_OPEN
  22. #define AA_MAY_ACCEPT 0x00100000
  23. #define AA_MAY_BIND 0x00200000
  24. #define AA_MAY_LISTEN 0x00400000
  25. #define AA_MAY_SETOPT 0x01000000
  26. #define AA_MAY_GETOPT 0x02000000
  27. #define NET_PERMS_MASK (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CREATE | \
  28. AA_MAY_SHUTDOWN | AA_MAY_BIND | AA_MAY_LISTEN | \
  29. AA_MAY_CONNECT | AA_MAY_ACCEPT | AA_MAY_SETATTR | \
  30. AA_MAY_GETATTR | AA_MAY_SETOPT | AA_MAY_GETOPT)
  31. #define NET_FS_PERMS (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CREATE | \
  32. AA_MAY_SHUTDOWN | AA_MAY_CONNECT | AA_MAY_RENAME |\
  33. AA_MAY_SETATTR | AA_MAY_GETATTR | AA_MAY_CHMOD | \
  34. AA_MAY_CHOWN | AA_MAY_CHGRP | AA_MAY_LOCK | \
  35. AA_MAY_MPROT)
  36. #define NET_PEER_MASK (AA_MAY_SEND | AA_MAY_RECEIVE | AA_MAY_CONNECT | \
  37. AA_MAY_ACCEPT)
  38. struct aa_sk_ctx {
  39. struct aa_label __rcu *label;
  40. struct aa_label __rcu *peer;
  41. struct aa_label __rcu *peer_lastupdate; /* ptr cmp only, no deref */
  42. };
  43. static inline struct aa_sk_ctx *aa_sock(const struct sock *sk)
  44. {
  45. return sk->sk_security + apparmor_blob_sizes.lbs_sock;
  46. }
  47. #define DEFINE_AUDIT_NET(NAME, OP, CRED, SK, F, T, P) \
  48. struct lsm_network_audit NAME ## _net = { .sk = (SK), \
  49. .family = (F)}; \
  50. DEFINE_AUDIT_DATA(NAME, \
  51. ((SK) && (F) != AF_UNIX) ? LSM_AUDIT_DATA_NET : \
  52. LSM_AUDIT_DATA_NONE, \
  53. AA_CLASS_NET, \
  54. OP); \
  55. NAME.common.u.net = &(NAME ## _net); \
  56. NAME.subj_cred = (CRED); \
  57. NAME.net.type = (T); \
  58. NAME.net.protocol = (P)
  59. #define DEFINE_AUDIT_SK(NAME, OP, CRED, SK) \
  60. DEFINE_AUDIT_NET(NAME, OP, CRED, SK, (SK)->sk_family, (SK)->sk_type, \
  61. (SK)->sk_protocol)
  62. struct aa_secmark {
  63. u8 audit;
  64. u8 deny;
  65. u32 secid;
  66. char *label;
  67. };
  68. extern struct aa_sfs_entry aa_sfs_entry_network[];
  69. extern struct aa_sfs_entry aa_sfs_entry_networkv9[];
  70. int aa_do_perms(struct aa_profile *profile, struct aa_policydb *policy,
  71. aa_state_t state, u32 request, struct aa_perms *p,
  72. struct apparmor_audit_data *ad);
  73. /* passing in state returned by XXX_mediates_AF() */
  74. aa_state_t aa_match_to_prot(struct aa_policydb *policy, aa_state_t state,
  75. u32 request, u16 af, int type, int protocol,
  76. struct aa_perms **p, const char **info);
  77. void audit_net_cb(struct audit_buffer *ab, void *va);
  78. int aa_profile_af_perm(struct aa_profile *profile,
  79. struct apparmor_audit_data *ad,
  80. u32 request, u16 family, int type, int protocol);
  81. int aa_af_perm(const struct cred *subj_cred, struct aa_label *label,
  82. const char *op, u32 request, u16 family,
  83. int type, int protocol);
  84. static inline int aa_profile_af_sk_perm(struct aa_profile *profile,
  85. struct apparmor_audit_data *ad,
  86. u32 request,
  87. struct sock *sk)
  88. {
  89. return aa_profile_af_perm(profile, ad, request, sk->sk_family,
  90. sk->sk_type, sk->sk_protocol);
  91. }
  92. int aa_sk_perm(const char *op, u32 request, struct sock *sk);
  93. int aa_sock_file_perm(const struct cred *subj_cred, struct aa_label *label,
  94. const char *op, u32 request,
  95. struct file *file);
  96. int apparmor_secmark_check(struct aa_label *label, char *op, u32 request,
  97. u32 secid, const struct sock *sk);
  98. #endif /* __AA_NET_H */