lib.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. struct apparmor_audit_data aad = {0,};
  62. COMMON_AUDIT_DATA_INIT(&sa, NONE);
  63. sa.aad = &aad;
  64. aad.info = str;
  65. aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);
  66. }
  67. printk(KERN_INFO "AppArmor: %s\n", str);
  68. }
  69. /**
  70. * kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
  71. * @size: size of allocation
  72. *
  73. * Return: allocated buffer or NULL if failed
  74. *
  75. * It is possible that policy being loaded from the user is larger than
  76. * what can be allocated by kmalloc, in those cases fall back to vmalloc.
  77. */
  78. void *kvmalloc(size_t size)
  79. {
  80. void *buffer = NULL;
  81. if (size == 0)
  82. return NULL;
  83. /* do not attempt kmalloc if we need more than 16 pages at once */
  84. if (size <= (16*PAGE_SIZE))
  85. buffer = kmalloc(size, GFP_NOIO | __GFP_NOWARN);
  86. if (!buffer) {
  87. /* see kvfree for why size must be at least work_struct size
  88. * when allocated via vmalloc
  89. */
  90. if (size < sizeof(struct work_struct))
  91. size = sizeof(struct work_struct);
  92. buffer = vmalloc(size);
  93. }
  94. return buffer;
  95. }