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