ipc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_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. if (aad(sa)->request & AA_SIGNAL_PERM_MASK) {
  50. audit_log_format(ab, " requested_mask=\"%s\"",
  51. audit_signal_mask(aad(sa)->request));
  52. if (aad(sa)->denied & AA_SIGNAL_PERM_MASK) {
  53. audit_log_format(ab, " denied_mask=\"%s\"",
  54. audit_signal_mask(aad(sa)->denied));
  55. }
  56. }
  57. if (aad(sa)->signal == SIGUNKNOWN)
  58. audit_log_format(ab, "signal=unknown(%d)",
  59. aad(sa)->unmappedsig);
  60. else if (aad(sa)->signal < MAXMAPPED_SIGNAME)
  61. audit_log_format(ab, " signal=%s", sig_names[aad(sa)->signal]);
  62. else
  63. audit_log_format(ab, " signal=rtmin+%d",
  64. aad(sa)->signal - SIGRT_BASE);
  65. audit_log_format(ab, " peer=");
  66. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  67. FLAGS_NONE, GFP_ATOMIC);
  68. }
  69. static int profile_signal_perm(struct aa_profile *profile,
  70. struct aa_label *peer, u32 request,
  71. struct common_audit_data *sa)
  72. {
  73. struct aa_perms perms;
  74. unsigned int state;
  75. if (profile_unconfined(profile) ||
  76. !PROFILE_MEDIATES(profile, AA_CLASS_SIGNAL))
  77. return 0;
  78. aad(sa)->peer = peer;
  79. /* TODO: secondary cache check <profile, profile, perm> */
  80. state = aa_dfa_next(profile->policy.dfa,
  81. profile->policy.start[AA_CLASS_SIGNAL],
  82. aad(sa)->signal);
  83. aa_label_match(profile, peer, state, false, request, &perms);
  84. aa_apply_modes_to_perms(profile, &perms);
  85. return aa_check_perms(profile, &perms, request, sa, audit_signal_cb);
  86. }
  87. int aa_may_signal(struct aa_label *sender, struct aa_label *target, int sig)
  88. {
  89. struct aa_profile *profile;
  90. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SIGNAL);
  91. aad(&sa)->signal = map_signal_num(sig);
  92. aad(&sa)->unmappedsig = sig;
  93. return xcheck_labels(sender, target, profile,
  94. profile_signal_perm(profile, target, MAY_WRITE, &sa),
  95. profile_signal_perm(profile, sender, MAY_READ, &sa));
  96. }