resource.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "include/audit.h"
  16. #include "include/context.h"
  17. #include "include/resource.h"
  18. #include "include/policy.h"
  19. /*
  20. * Table of rlimit names: we generate it from resource.h.
  21. */
  22. #include "rlim_names.h"
  23. struct aa_fs_entry aa_fs_entry_rlimit[] = {
  24. AA_FS_FILE_STRING("mask", AA_FS_RLIMIT_MASK),
  25. { }
  26. };
  27. /* audit callback for resource specific fields */
  28. static void audit_cb(struct audit_buffer *ab, void *va)
  29. {
  30. struct common_audit_data *sa = va;
  31. audit_log_format(ab, " rlimit=%s value=%lu",
  32. rlim_names[aad(sa)->rlim.rlim], aad(sa)->rlim.max);
  33. }
  34. /**
  35. * audit_resource - audit setting resource limit
  36. * @profile: profile being enforced (NOT NULL)
  37. * @resoure: rlimit being auditing
  38. * @value: value being set
  39. * @error: error value
  40. *
  41. * Returns: 0 or sa->error else other error code on failure
  42. */
  43. static int audit_resource(struct aa_profile *profile, unsigned int resource,
  44. unsigned long value, int error)
  45. {
  46. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETRLIMIT);
  47. aad(&sa)->rlim.rlim = resource;
  48. aad(&sa)->rlim.max = value;
  49. aad(&sa)->error = error;
  50. return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb);
  51. }
  52. /**
  53. * aa_map_resouce - map compiled policy resource to internal #
  54. * @resource: flattened policy resource number
  55. *
  56. * Returns: resource # for the current architecture.
  57. *
  58. * rlimit resource can vary based on architecture, map the compiled policy
  59. * resource # to the internal representation for the architecture.
  60. */
  61. int aa_map_resource(int resource)
  62. {
  63. return rlim_map[resource];
  64. }
  65. /**
  66. * aa_task_setrlimit - test permission to set an rlimit
  67. * @profile - profile confining the task (NOT NULL)
  68. * @task - task the resource is being set on
  69. * @resource - the resource being set
  70. * @new_rlim - the new resource limit (NOT NULL)
  71. *
  72. * Control raising the processes hard limit.
  73. *
  74. * Returns: 0 or error code if setting resource failed
  75. */
  76. int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *task,
  77. unsigned int resource, struct rlimit *new_rlim)
  78. {
  79. struct aa_profile *task_profile;
  80. int error = 0;
  81. rcu_read_lock();
  82. task_profile = aa_get_profile(aa_cred_profile(__task_cred(task)));
  83. rcu_read_unlock();
  84. /* TODO: extend resource control to handle other (non current)
  85. * profiles. AppArmor rules currently have the implicit assumption
  86. * that the task is setting the resource of a task confined with
  87. * the same profile or that the task setting the resource of another
  88. * task has CAP_SYS_RESOURCE.
  89. */
  90. if ((profile != task_profile &&
  91. aa_capable(profile, CAP_SYS_RESOURCE, 1)) ||
  92. (profile->rlimits.mask & (1 << resource) &&
  93. new_rlim->rlim_max > profile->rlimits.limits[resource].rlim_max))
  94. error = -EACCES;
  95. aa_put_profile(task_profile);
  96. return audit_resource(profile, resource, new_rlim->rlim_max, error);
  97. }
  98. /**
  99. * __aa_transition_rlimits - apply new profile rlimits
  100. * @old: old profile on task (NOT NULL)
  101. * @new: new profile with rlimits to apply (NOT NULL)
  102. */
  103. void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new)
  104. {
  105. unsigned int mask = 0;
  106. struct rlimit *rlim, *initrlim;
  107. int i;
  108. /* for any rlimits the profile controlled reset the soft limit
  109. * to the less of the tasks hard limit and the init tasks soft limit
  110. */
  111. if (old->rlimits.mask) {
  112. for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {
  113. if (old->rlimits.mask & mask) {
  114. rlim = current->signal->rlim + i;
  115. initrlim = init_task.signal->rlim + i;
  116. rlim->rlim_cur = min(rlim->rlim_max,
  117. initrlim->rlim_cur);
  118. }
  119. }
  120. }
  121. /* set any new hard limits as dictated by the new profile */
  122. if (!new->rlimits.mask)
  123. return;
  124. for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {
  125. if (!(new->rlimits.mask & mask))
  126. continue;
  127. rlim = current->signal->rlim + i;
  128. rlim->rlim_max = min(rlim->rlim_max,
  129. new->rlimits.limits[i].rlim_max);
  130. /* soft limit should not exceed hard limit */
  131. rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max);
  132. }
  133. }