procattr.c 4.6 KB

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