Skip to content

Commit 0ca554b

Browse files
committed
apparmor: add kvzalloc to handle zeroing for kvmalloc
Signed-off-by: John Johansen <john.johansen@canonical.com> Acked-by: Steve Beattie <sbeattie@ubuntu.com>
1 parent 3cfcc19 commit 0ca554b

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎security/apparmor/include/apparmor.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef __APPARMOR_H
1616
#define __APPARMOR_H
1717

18+
#include <linux/slab.h>
1819
#include <linux/fs.h>
1920

2021
#include "match.h"
@@ -64,9 +65,18 @@ extern int apparmor_initialized __initdata;
6465
/* fn's in lib */
6566
char *aa_split_fqname(char *args, char **ns_name);
6667
void aa_info_message(const char *str);
67-
void *kvmalloc(size_t size);
68+
void *__aa_kvmalloc(size_t size, gfp_t flags);
6869
void kvfree(void *buffer);
6970

71+
static inline void *kvmalloc(size_t size)
72+
{
73+
return __aa_kvmalloc(size, 0);
74+
}
75+
76+
static inline void *kvzalloc(size_t size)
77+
{
78+
return __aa_kvmalloc(size, __GFP_ZERO);
79+
}
7080

7181
/**
7282
* aa_strneq - compare null terminated @str to a non null terminated substring

‎security/apparmor/lib.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ void aa_info_message(const char *str)
7575
}
7676

7777
/**
78-
* kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
79-
* @size: size of allocation
78+
* __aa_kvmalloc - do allocation preferring kmalloc but falling back to vmalloc
79+
* @size: how many bytes of memory are required
80+
* @flags: the type of memory to allocate (see kmalloc).
8081
*
8182
* Return: allocated buffer or NULL if failed
8283
*
8384
* It is possible that policy being loaded from the user is larger than
8485
* what can be allocated by kmalloc, in those cases fall back to vmalloc.
8586
*/
86-
void *kvmalloc(size_t size)
87+
void *__aa_kvmalloc(size_t size, gfp_t flags)
8788
{
8889
void *buffer = NULL;
8990

@@ -92,14 +93,17 @@ void *kvmalloc(size_t size)
9293

9394
/* do not attempt kmalloc if we need more than 16 pages at once */
9495
if (size <= (16*PAGE_SIZE))
95-
buffer = kmalloc(size, GFP_NOIO | __GFP_NOWARN);
96+
buffer = kmalloc(size, flags | GFP_NOIO | __GFP_NOWARN);
9697
if (!buffer) {
9798
/* see kvfree for why size must be at least work_struct size
9899
* when allocated via vmalloc
99100
*/
100101
if (size < sizeof(struct work_struct))
101102
size = sizeof(struct work_struct);
102-
buffer = vmalloc(size);
103+
if (flags & __GFP_ZERO)
104+
buffer = vzalloc(size);
105+
else
106+
buffer = vmalloc(size);
103107
}
104108
return buffer;
105109
}

‎security/apparmor/match.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* Returns: pointer to table else NULL on failure
3232
*
33-
* NOTE: must be freed by kvfree (not kmalloc)
33+
* NOTE: must be freed by kvfree (not kfree)
3434
*/
3535
static struct table_header *unpack_table(char *blob, size_t bsize)
3636
{
@@ -57,7 +57,7 @@ static struct table_header *unpack_table(char *blob, size_t bsize)
5757
if (bsize < tsize)
5858
goto out;
5959

60-
table = kvmalloc(tsize);
60+
table = kvzalloc(tsize);
6161
if (table) {
6262
*table = th;
6363
if (th.td_flags == YYTD_DATA8)

0 commit comments

Comments
 (0)