procattr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor /proc/<pid>/attr/ interface functions
  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 "include/apparmor.h"
  15. #include "include/context.h"
  16. #include "include/policy.h"
  17. #include "include/policy_ns.h"
  18. #include "include/domain.h"
  19. #include "include/procattr.h"
  20. /**
  21. * aa_getprocattr - Return the profile information for @profile
  22. * @profile: the profile to print profile info about (NOT NULL)
  23. * @string: Returns - string containing the profile info (NOT NULL)
  24. *
  25. * Returns: length of @string on success else error on failure
  26. *
  27. * Requires: profile != NULL
  28. *
  29. * Creates a string containing the namespace_name://profile_name for
  30. * @profile.
  31. *
  32. * Returns: size of string placed in @string else error code on failure
  33. */
  34. int aa_getprocattr(struct aa_profile *profile, char **string)
  35. {
  36. char *str;
  37. int len = 0, mode_len = 0, ns_len = 0, name_len;
  38. const char *mode_str = aa_profile_mode_names[profile->mode];
  39. const char *ns_name = NULL;
  40. struct aa_ns *ns = profile->ns;
  41. struct aa_ns *current_ns = __aa_current_profile()->ns;
  42. char *s;
  43. if (!aa_ns_visible(current_ns, ns, true))
  44. return -EACCES;
  45. ns_name = aa_ns_name(current_ns, ns, true);
  46. ns_len = strlen(ns_name);
  47. /* if the visible ns_name is > 0 increase size for : :// seperator */
  48. if (ns_len)
  49. ns_len += 4;
  50. /* unconfined profiles don't have a mode string appended */
  51. if (!unconfined(profile))
  52. mode_len = strlen(mode_str) + 3; /* + 3 for _() */
  53. name_len = strlen(profile->base.hname);
  54. len = mode_len + ns_len + name_len + 1; /* + 1 for \n */
  55. s = str = kmalloc(len + 1, GFP_KERNEL); /* + 1 \0 */
  56. if (!str)
  57. return -ENOMEM;
  58. if (ns_len) {
  59. /* skip over prefix current_ns->base.hname and separating // */
  60. sprintf(s, ":%s://", ns_name);
  61. s += ns_len;
  62. }
  63. if (unconfined(profile))
  64. /* mode string not being appended */
  65. sprintf(s, "%s\n", profile->base.hname);
  66. else
  67. sprintf(s, "%s (%s)\n", profile->base.hname, mode_str);
  68. *string = str;
  69. /* NOTE: len does not include \0 of string, not saved as part of file */
  70. return len;
  71. }
  72. /**
  73. * split_token_from_name - separate a string of form <token>^<name>
  74. * @op: operation being checked
  75. * @args: string to parse (NOT NULL)
  76. * @token: stores returned parsed token value (NOT NULL)
  77. *
  78. * Returns: start position of name after token else NULL on failure
  79. */
  80. static char *split_token_from_name(const char *op, char *args, u64 *token)
  81. {
  82. char *name;
  83. *token = simple_strtoull(args, &name, 16);
  84. if ((name == args) || *name != '^') {
  85. AA_ERROR("%s: Invalid input '%s'", op, args);
  86. return ERR_PTR(-EINVAL);
  87. }
  88. name++; /* skip ^ */
  89. if (!*name)
  90. name = NULL;
  91. return name;
  92. }
  93. /**
  94. * aa_setprocattr_chagnehat - handle procattr interface to change_hat
  95. * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
  96. * @size: size of the args
  97. * @test: true if this is a test of change_hat permissions
  98. *
  99. * Returns: %0 or error code if change_hat fails
  100. */
  101. int aa_setprocattr_changehat(char *args, size_t size, int test)
  102. {
  103. char *hat;
  104. u64 token;
  105. const char *hats[16]; /* current hard limit on # of names */
  106. int count = 0;
  107. hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
  108. if (IS_ERR(hat))
  109. return PTR_ERR(hat);
  110. if (!hat && !token) {
  111. AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
  112. return -EINVAL;
  113. }
  114. if (hat) {
  115. /* set up hat name vector, args guaranteed null terminated
  116. * at args[size] by setprocattr.
  117. *
  118. * If there are multiple hat names in the buffer each is
  119. * separated by a \0. Ie. userspace writes them pre tokenized
  120. */
  121. char *end = args + size;
  122. for (count = 0; (hat < end) && count < 16; ++count) {
  123. char *next = hat + strlen(hat) + 1;
  124. hats[count] = hat;
  125. hat = next;
  126. }
  127. }
  128. AA_DEBUG("%s: Magic 0x%llx Hat '%s'\n",
  129. __func__, token, hat ? hat : NULL);
  130. return aa_change_hat(hats, count, token, test);
  131. }
  132. /**
  133. * aa_setprocattr_changeprofile - handle procattr interface to changeprofile
  134. * @fqname: args received from writting to /proc/<pid>/attr/current (NOT NULL)
  135. * @onexec: true if change_profile should be delayed until exec
  136. * @test: true if this is a test of change_profile permissions
  137. *
  138. * Returns: %0 or error code if change_profile fails
  139. */
  140. int aa_setprocattr_changeprofile(char *fqname, bool onexec, int test)
  141. {
  142. char *name, *ns_name;
  143. name = aa_split_fqname(fqname, &ns_name);
  144. return aa_change_profile(ns_name, name, onexec, test);
  145. }