lib.c 3.3 KB

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