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