procattr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/cred.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_label *label, char **string)
  35. {
  36. struct aa_ns *ns = labels_ns(label);
  37. struct aa_ns *current_ns = aa_get_current_ns();
  38. int len;
  39. if (!aa_ns_visible(current_ns, ns, true)) {
  40. aa_put_ns(current_ns);
  41. return -EACCES;
  42. }
  43. len = aa_label_snxprint(NULL, 0, current_ns, label,
  44. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  45. FLAG_HIDDEN_UNCONFINED);
  46. AA_BUG(len < 0);
  47. *string = kmalloc(len + 2, GFP_KERNEL);
  48. if (!*string) {
  49. aa_put_ns(current_ns);
  50. return -ENOMEM;
  51. }
  52. len = aa_label_snxprint(*string, len + 2, current_ns, label,
  53. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  54. FLAG_HIDDEN_UNCONFINED);
  55. if (len < 0) {
  56. aa_put_ns(current_ns);
  57. return len;
  58. }
  59. (*string)[len] = '\n';
  60. (*string)[len + 1] = 0;
  61. aa_put_ns(current_ns);
  62. return len + 1;
  63. }
  64. /**
  65. * split_token_from_name - separate a string of form <token>^<name>
  66. * @op: operation being checked
  67. * @args: string to parse (NOT NULL)
  68. * @token: stores returned parsed token value (NOT NULL)
  69. *
  70. * Returns: start position of name after token else NULL on failure
  71. */
  72. static char *split_token_from_name(const char *op, char *args, u64 *token)
  73. {
  74. char *name;
  75. *token = simple_strtoull(args, &name, 16);
  76. if ((name == args) || *name != '^') {
  77. AA_ERROR("%s: Invalid input '%s'", op, args);
  78. return ERR_PTR(-EINVAL);
  79. }
  80. name++; /* skip ^ */
  81. if (!*name)
  82. name = NULL;
  83. return name;
  84. }
  85. /**
  86. * aa_setprocattr_chagnehat - handle procattr interface to change_hat
  87. * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
  88. * @size: size of the args
  89. * @flags: set of flags governing behavior
  90. *
  91. * Returns: %0 or error code if change_hat fails
  92. */
  93. int aa_setprocattr_changehat(char *args, size_t size, int flags)
  94. {
  95. char *hat;
  96. u64 token;
  97. const char *hats[16]; /* current hard limit on # of names */
  98. int count = 0;
  99. hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
  100. if (IS_ERR(hat))
  101. return PTR_ERR(hat);
  102. if (!hat && !token) {
  103. AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
  104. return -EINVAL;
  105. }
  106. if (hat) {
  107. /* set up hat name vector, args guaranteed null terminated
  108. * at args[size] by setprocattr.
  109. *
  110. * If there are multiple hat names in the buffer each is
  111. * separated by a \0. Ie. userspace writes them pre tokenized
  112. */
  113. char *end = args + size;
  114. for (count = 0; (hat < end) && count < 16; ++count) {
  115. char *next = hat + strlen(hat) + 1;
  116. hats[count] = hat;
  117. AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d hat '%s'\n"
  118. , __func__, current->pid, token, count, hat);
  119. hat = next;
  120. }
  121. } else
  122. AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n",
  123. __func__, current->pid, token, count, "<NULL>");
  124. return aa_change_hat(hats, count, token, flags);
  125. }