procattr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor /proc/<pid>/attr/ interface functions
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include "include/apparmor.h"
  11. #include "include/cred.h"
  12. #include "include/policy.h"
  13. #include "include/policy_ns.h"
  14. #include "include/domain.h"
  15. #include "include/procattr.h"
  16. /**
  17. * aa_getprocattr - Return the label information for @label
  18. * @label: the label to print label info about (NOT NULL)
  19. * @string: Returns - string containing the label info (NOT NULL)
  20. * @newline: indicates that a newline should be added
  21. *
  22. * Requires: label != NULL && string != NULL
  23. *
  24. * Creates a string containing the label information for @label.
  25. *
  26. * Returns: size of string placed in @string else error code on failure
  27. */
  28. int aa_getprocattr(struct aa_label *label, char **string, bool newline)
  29. {
  30. struct aa_ns *ns = labels_ns(label);
  31. struct aa_ns *current_ns = aa_get_current_ns();
  32. int len;
  33. if (!aa_ns_visible(current_ns, ns, true)) {
  34. aa_put_ns(current_ns);
  35. return -EACCES;
  36. }
  37. len = aa_label_snxprint(NULL, 0, current_ns, label,
  38. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  39. FLAG_HIDDEN_UNCONFINED);
  40. AA_BUG(len < 0);
  41. *string = kmalloc(len + 2, GFP_KERNEL);
  42. if (!*string) {
  43. aa_put_ns(current_ns);
  44. return -ENOMEM;
  45. }
  46. len = aa_label_snxprint(*string, len + 2, current_ns, label,
  47. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  48. FLAG_HIDDEN_UNCONFINED);
  49. if (len < 0) {
  50. aa_put_ns(current_ns);
  51. return len;
  52. }
  53. if (newline)
  54. (*string)[len++] = '\n';
  55. (*string)[len] = 0;
  56. aa_put_ns(current_ns);
  57. return len;
  58. }
  59. /**
  60. * split_token_from_name - separate a string of form <token>^<name>
  61. * @op: operation being checked
  62. * @args: string to parse (NOT NULL)
  63. * @token: stores returned parsed token value (NOT NULL)
  64. *
  65. * Returns: start position of name after token else NULL on failure
  66. */
  67. static char *split_token_from_name(const char *op, char *args, u64 *token)
  68. {
  69. char *name;
  70. *token = simple_strtoull(args, &name, 16);
  71. if ((name == args) || *name != '^') {
  72. AA_ERROR("%s: Invalid input '%s'", op, args);
  73. return ERR_PTR(-EINVAL);
  74. }
  75. name++; /* skip ^ */
  76. if (!*name)
  77. name = NULL;
  78. return name;
  79. }
  80. /**
  81. * aa_setprocattr_changehat - handle procattr interface to change_hat
  82. * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
  83. * @size: size of the args
  84. * @flags: set of flags governing behavior
  85. *
  86. * Returns: %0 or error code if change_hat fails
  87. */
  88. int aa_setprocattr_changehat(char *args, size_t size, int flags)
  89. {
  90. char *hat;
  91. u64 token;
  92. const char *hats[16]; /* current hard limit on # of names */
  93. int count = 0;
  94. hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
  95. if (IS_ERR(hat))
  96. return PTR_ERR(hat);
  97. if (!hat && !token) {
  98. AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
  99. return -EINVAL;
  100. }
  101. if (hat) {
  102. /* set up hat name vector, args guaranteed null terminated
  103. * at args[size] by setprocattr.
  104. *
  105. * If there are multiple hat names in the buffer each is
  106. * separated by a \0. Ie. userspace writes them pre tokenized
  107. */
  108. char *end = args + size;
  109. for (count = 0; (hat < end) && count < 16; ++count) {
  110. char *next = hat + strlen(hat) + 1;
  111. hats[count] = hat;
  112. AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d hat '%s'\n"
  113. , __func__, current->pid, token, count, hat);
  114. hat = next;
  115. }
  116. } else
  117. AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n",
  118. __func__, current->pid, token, count, "<NULL>");
  119. return aa_change_hat(hats, count, token, flags);
  120. }