resource.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor resource mediation and attachment
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2010 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/audit.h>
  15. #include <linux/security.h>
  16. #include "include/audit.h"
  17. #include "include/cred.h"
  18. #include "include/resource.h"
  19. #include "include/policy.h"
  20. /*
  21. * Table of rlimit names: we generate it from resource.h.
  22. */
  23. #include "rlim_names.h"
  24. struct aa_sfs_entry aa_sfs_entry_rlimit[] = {
  25. AA_SFS_FILE_STRING("mask", AA_SFS_RLIMIT_MASK),
  26. { }
  27. };
  28. /* audit callback for resource specific fields */
  29. static void audit_cb(struct audit_buffer *ab, void *va)
  30. {
  31. struct common_audit_data *sa = va;
  32. audit_log_format(ab, " rlimit=%s value=%lu",
  33. rlim_names[aad(sa)->rlim.rlim], aad(sa)->rlim.max);
  34. if (aad(sa)->peer) {
  35. audit_log_format(ab, " peer=");
  36. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  37. FLAGS_NONE, GFP_ATOMIC);
  38. }
  39. }
  40. /**
  41. * audit_resource - audit setting resource limit
  42. * @profile: profile being enforced (NOT NULL)
  43. * @resource: rlimit being auditing
  44. * @value: value being set
  45. * @error: error value
  46. *
  47. * Returns: 0 or sa->error else other error code on failure
  48. */
  49. static int audit_resource(struct aa_profile *profile, unsigned int resource,
  50. unsigned long value, struct aa_label *peer,
  51. const char *info, int error)
  52. {
  53. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETRLIMIT);
  54. aad(&sa)->rlim.rlim = resource;
  55. aad(&sa)->rlim.max = value;
  56. aad(&sa)->peer = peer;
  57. aad(&sa)->info = info;
  58. aad(&sa)->error = error;
  59. return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb);
  60. }
  61. /**
  62. * aa_map_resouce - map compiled policy resource to internal #
  63. * @resource: flattened policy resource number
  64. *
  65. * Returns: resource # for the current architecture.
  66. *
  67. * rlimit resource can vary based on architecture, map the compiled policy
  68. * resource # to the internal representation for the architecture.
  69. */
  70. int aa_map_resource(int resource)
  71. {
  72. return rlim_map[resource];
  73. }
  74. static int profile_setrlimit(struct aa_profile *profile, unsigned int resource,
  75. struct rlimit *new_rlim)
  76. {
  77. int e = 0;
  78. if (profile->rlimits.mask & (1 << resource) && new_rlim->rlim_max >
  79. profile->rlimits.limits[resource].rlim_max)
  80. e = -EACCES;
  81. return audit_resource(profile, resource, new_rlim->rlim_max, NULL, NULL,
  82. e);
  83. }
  84. /**
  85. * aa_task_setrlimit - test permission to set an rlimit
  86. * @label - label confining the task (NOT NULL)
  87. * @task - task the resource is being set on
  88. * @resource - the resource being set
  89. * @new_rlim - the new resource limit (NOT NULL)
  90. *
  91. * Control raising the processes hard limit.
  92. *
  93. * Returns: 0 or error code if setting resource failed
  94. */
  95. int aa_task_setrlimit(struct aa_label *label, struct task_struct *task,
  96. unsigned int resource, struct rlimit *new_rlim)
  97. {
  98. struct aa_profile *profile;
  99. struct aa_label *peer;
  100. int error = 0;
  101. rcu_read_lock();
  102. peer = aa_get_newest_cred_label(__task_cred(task));
  103. rcu_read_unlock();
  104. /* TODO: extend resource control to handle other (non current)
  105. * profiles. AppArmor rules currently have the implicit assumption
  106. * that the task is setting the resource of a task confined with
  107. * the same profile or that the task setting the resource of another
  108. * task has CAP_SYS_RESOURCE.
  109. */
  110. if (label != peer &&
  111. aa_capable(label, CAP_SYS_RESOURCE, CAP_OPT_NOAUDIT) != 0)
  112. error = fn_for_each(label, profile,
  113. audit_resource(profile, resource,
  114. new_rlim->rlim_max, peer,
  115. "cap_sys_resource", -EACCES));
  116. else
  117. error = fn_for_each_confined(label, profile,
  118. profile_setrlimit(profile, resource, new_rlim));
  119. aa_put_label(peer);
  120. return error;
  121. }
  122. /**
  123. * __aa_transition_rlimits - apply new profile rlimits
  124. * @old_l: old label on task (NOT NULL)
  125. * @new_l: new label with rlimits to apply (NOT NULL)
  126. */
  127. void __aa_transition_rlimits(struct aa_label *old_l, struct aa_label *new_l)
  128. {
  129. unsigned int mask = 0;
  130. struct rlimit *rlim, *initrlim;
  131. struct aa_profile *old, *new;
  132. struct label_it i;
  133. old = labels_profile(old_l);
  134. new = labels_profile(new_l);
  135. /* for any rlimits the profile controlled, reset the soft limit
  136. * to the lesser of the tasks hard limit and the init tasks soft limit
  137. */
  138. label_for_each_confined(i, old_l, old) {
  139. if (old->rlimits.mask) {
  140. int j;
  141. for (j = 0, mask = 1; j < RLIM_NLIMITS; j++,
  142. mask <<= 1) {
  143. if (old->rlimits.mask & mask) {
  144. rlim = current->signal->rlim + j;
  145. initrlim = init_task.signal->rlim + j;
  146. rlim->rlim_cur = min(rlim->rlim_max,
  147. initrlim->rlim_cur);
  148. }
  149. }
  150. }
  151. }
  152. /* set any new hard limits as dictated by the new profile */
  153. label_for_each_confined(i, new_l, new) {
  154. int j;
  155. if (!new->rlimits.mask)
  156. continue;
  157. for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, mask <<= 1) {
  158. if (!(new->rlimits.mask & mask))
  159. continue;
  160. rlim = current->signal->rlim + j;
  161. rlim->rlim_max = min(rlim->rlim_max,
  162. new->rlimits.limits[j].rlim_max);
  163. /* soft limit should not exceed hard limit */
  164. rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max);
  165. }
  166. }
  167. }