/*1* AppArmor security module2*3* This file contains basic common functions used in AppArmor4*5* Copyright (C) 1998-2008 Novell/SUSE6* Copyright 2009-2010 Canonical Ltd.7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License as10* published by the Free Software Foundation, version 2 of the11* License.12*/1314#include <linux/slab.h>15#include <linux/string.h>16#include <linux/vmalloc.h>1718#include "include/audit.h"192021/**22* aa_split_fqname - split a fqname into a profile and namespace name23* @fqname: a full qualified name in namespace profile format (NOT NULL)24* @ns_name: pointer to portion of the string containing the ns name (NOT NULL)25*26* Returns: profile name or NULL if one is not specified27*28* Split a namespace name from a profile name (see policy.c for naming29* description). If a portion of the name is missing it returns NULL for30* that portion.31*32* NOTE: may modify the @fqname string. The pointers returned point33* into the @fqname string.34*/35char *aa_split_fqname(char *fqname, char **ns_name)36{37char *name = strim(fqname);3839*ns_name = NULL;40if (name[0] == ':') {41char *split = strchr(&name[1], ':');42*ns_name = skip_spaces(&name[1]);43if (split) {44/* overwrite ':' with \0 */45*split = 0;46name = skip_spaces(split + 1);47} else48/* a ns name without a following profile is allowed */49name = NULL;50}51if (name && *name == 0)52name = NULL;5354return name;55}5657/**58* aa_info_message - log a none profile related status message59* @str: message to log60*/61void aa_info_message(const char *str)62{63if (audit_enabled) {64struct common_audit_data sa;65COMMON_AUDIT_DATA_INIT(&sa, NONE);66sa.aad.info = str;67aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, NULL);68}69printk(KERN_INFO "AppArmor: %s\n", str);70}7172/**73* kvmalloc - do allocation preferring kmalloc but falling back to vmalloc74* @size: size of allocation75*76* Return: allocated buffer or NULL if failed77*78* It is possible that policy being loaded from the user is larger than79* what can be allocated by kmalloc, in those cases fall back to vmalloc.80*/81void *kvmalloc(size_t size)82{83void *buffer = NULL;8485if (size == 0)86return NULL;8788/* do not attempt kmalloc if we need more than 16 pages at once */89if (size <= (16*PAGE_SIZE))90buffer = kmalloc(size, GFP_NOIO | __GFP_NOWARN);91if (!buffer) {92/* see kvfree for why size must be at least work_struct size93* when allocated via vmalloc94*/95if (size < sizeof(struct work_struct))96size = sizeof(struct work_struct);97buffer = vmalloc(size);98}99return buffer;100}101102/**103* do_vfree - workqueue routine for freeing vmalloced memory104* @work: data to be freed105*106* The work_struct is overlaid to the data being freed, as at the point107* the work is scheduled the data is no longer valid, be its freeing108* needs to be delayed until safe.109*/110static void do_vfree(struct work_struct *work)111{112vfree(work);113}114115/**116* kvfree - free an allocation do by kvmalloc117* @buffer: buffer to free (MAYBE_NULL)118*119* Free a buffer allocated by kvmalloc120*/121void kvfree(void *buffer)122{123if (is_vmalloc_addr(buffer)) {124/* Data is no longer valid so just use the allocated space125* as the work_struct126*/127struct work_struct *work = (struct work_struct *) buffer;128INIT_WORK(work, do_vfree);129schedule_work(work);130} else131kfree(buffer);132}133134135