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