ipc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor ipc mediation
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2017 Canonical Ltd.
  9. */
  10. #include <linux/gfp.h>
  11. #include "include/audit.h"
  12. #include "include/capability.h"
  13. #include "include/cred.h"
  14. #include "include/policy.h"
  15. #include "include/ipc.h"
  16. #include "include/sig_names.h"
  17. static inline int map_signal_num(int sig)
  18. {
  19. if (sig > SIGRTMAX)
  20. return SIGUNKNOWN;
  21. else if (sig >= SIGRTMIN)
  22. return sig - SIGRTMIN + SIGRT_BASE;
  23. else if (sig < MAXMAPPED_SIG)
  24. return sig_map[sig];
  25. return SIGUNKNOWN;
  26. }
  27. /**
  28. * audit_signal_mask - convert mask to permission string
  29. * @mask: permission mask to convert
  30. *
  31. * Returns: pointer to static string
  32. */
  33. static const char *audit_signal_mask(u32 mask)
  34. {
  35. if (mask & MAY_READ)
  36. return "receive";
  37. if (mask & MAY_WRITE)
  38. return "send";
  39. return "";
  40. }
  41. /**
  42. * audit_signal_cb() - call back for signal specific audit fields
  43. * @ab: audit_buffer (NOT NULL)
  44. * @va: audit struct to audit values of (NOT NULL)
  45. */
  46. static void audit_signal_cb(struct audit_buffer *ab, void *va)
  47. {
  48. struct common_audit_data *sa = va;
  49. struct apparmor_audit_data *ad = aad(sa);
  50. if (ad->request & AA_SIGNAL_PERM_MASK) {
  51. audit_log_format(ab, " requested_mask=\"%s\"",
  52. audit_signal_mask(ad->request));
  53. if (ad->denied & AA_SIGNAL_PERM_MASK) {
  54. audit_log_format(ab, " denied_mask=\"%s\"",
  55. audit_signal_mask(ad->denied));
  56. }
  57. }
  58. if (ad->signal == SIGUNKNOWN)
  59. audit_log_format(ab, "signal=unknown(%d)",
  60. ad->unmappedsig);
  61. else if (ad->signal < MAXMAPPED_SIGNAME)
  62. audit_log_format(ab, " signal=%s", sig_names[ad->signal]);
  63. else
  64. audit_log_format(ab, " signal=rtmin+%d",
  65. ad->signal - SIGRT_BASE);
  66. audit_log_format(ab, " peer=");
  67. aa_label_xaudit(ab, labels_ns(ad->subj_label), ad->peer,
  68. FLAGS_NONE, GFP_ATOMIC);
  69. }
  70. static int profile_signal_perm(const struct cred *cred,
  71. struct aa_profile *profile,
  72. struct aa_label *peer, u32 request,
  73. struct apparmor_audit_data *ad)
  74. {
  75. struct aa_ruleset *rules = profile->label.rules[0];
  76. struct aa_perms perms;
  77. aa_state_t state;
  78. if (profile_unconfined(profile))
  79. return 0;
  80. ad->subj_cred = cred;
  81. ad->peer = peer;
  82. /* TODO: secondary cache check <profile, profile, perm> */
  83. state = RULE_MEDIATES(rules, AA_CLASS_SIGNAL);
  84. if (!state)
  85. return 0;
  86. state = aa_dfa_next(rules->policy->dfa, state, ad->signal);
  87. aa_label_match(profile, rules, peer, state, false, request, &perms);
  88. aa_apply_modes_to_perms(profile, &perms);
  89. return aa_check_perms(profile, &perms, request, ad, audit_signal_cb);
  90. }
  91. int aa_may_signal(const struct cred *subj_cred, struct aa_label *sender,
  92. const struct cred *target_cred, struct aa_label *target,
  93. int sig)
  94. {
  95. struct aa_profile *profile;
  96. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_SIGNAL, OP_SIGNAL);
  97. ad.signal = map_signal_num(sig);
  98. ad.unmappedsig = sig;
  99. return xcheck_labels(sender, target, profile,
  100. profile_signal_perm(subj_cred, profile, target,
  101. MAY_WRITE, &ad),
  102. profile_signal_perm(target_cred, profile, sender,
  103. MAY_READ, &ad));
  104. }