lib.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains basic common functions used in AppArmor
  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 <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/vmalloc.h>
  18. #include "include/audit.h"
  19. #include "include/apparmor.h"
  20. /**
  21. * aa_split_fqname - split a fqname into a profile and namespace name
  22. * @fqname: a full qualified name in namespace profile format (NOT NULL)
  23. * @ns_name: pointer to portion of the string containing the ns name (NOT NULL)
  24. *
  25. * Returns: profile name or NULL if one is not specified
  26. *
  27. * Split a namespace name from a profile name (see policy.c for naming
  28. * description). If a portion of the name is missing it returns NULL for
  29. * that portion.
  30. *
  31. * NOTE: may modify the @fqname string. The pointers returned point
  32. * into the @fqname string.
  33. */
  34. char *aa_split_fqname(char *fqname, char **ns_name)
  35. {
  36. char *name = strim(fqname);
  37. *ns_name = NULL;
  38. if (name[0] == ':') {
  39. char *split = strchr(&name[1], ':');
  40. *ns_name = skip_spaces(&name[1]);
  41. if (split) {
  42. /* overwrite ':' with \0 */
  43. *split = 0;
  44. name = skip_spaces(split + 1);
  45. } else
  46. /* a ns name without a following profile is allowed */
  47. name = NULL;
  48. }
  49. if (name && *name == 0)
  50. name = NULL;
  51. return name;
  52. }
  53. /**
  54. * aa_info_message - log a none profile related status message
  55. * @str: message to log
  56. */
  57. void aa_info_message(const char *str)
  58. {
  59. if (audit_enabled) {
  60. struct common_audit_data sa;
  61. COMMON_AUDIT_DATA_INIT(&sa, NONE);
  62. sa.aad.info = str;
  63. aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
  64. }
  65. printk(KERN_INFO "AppArmor: %s\n", str);
  66. }
  67. /**
  68. * kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
  69. * @size: size of allocation
  70. *
  71. * Return: allocated buffer or NULL if failed
  72. *
  73. * It is possible that policy being loaded from the user is larger than
  74. * what can be allocated by kmalloc, in those cases fall back to vmalloc.
  75. */
  76. void *kvmalloc(size_t size)
  77. {
  78. void *buffer = NULL;
  79. if (size == 0)
  80. return NULL;
  81. /* do not attempt kmalloc if we need more than 16 pages at once */
  82. if (size <= (16*PAGE_SIZE))
  83. buffer = kmalloc(size, GFP_NOIO | __GFP_NOWARN);
  84. if (!buffer) {
  85. /* see kvfree for why size must be at least work_struct size
  86. * when allocated via vmalloc
  87. */
  88. if (size < sizeof(struct work_struct))
  89. size = sizeof(struct work_struct);
  90. buffer = vmalloc(size);
  91. }
  92. return buffer;
  93. }
  94. /**
  95. * do_vfree - workqueue routine for freeing vmalloced memory
  96. * @work: data to be freed
  97. *
  98. * The work_struct is overlaid to the data being freed, as at the point
  99. * the work is scheduled the data is no longer valid, be its freeing
  100. * needs to be delayed until safe.
  101. */
  102. static void do_vfree(struct work_struct *work)
  103. {
  104. vfree(work);
  105. }
  106. /**
  107. * kvfree - free an allocation do by kvmalloc
  108. * @buffer: buffer to free (MAYBE_NULL)
  109. *
  110. * Free a buffer allocated by kvmalloc
  111. */
  112. void kvfree(void *buffer)
  113. {
  114. if (is_vmalloc_addr(buffer)) {
  115. /* Data is no longer valid so just use the allocated space
  116. * as the work_struct
  117. */
  118. struct work_struct *work = (struct work_struct *) buffer;
  119. INIT_WORK(work, do_vfree);
  120. schedule_work(work);
  121. } else
  122. kfree(buffer);
  123. }