Skip to content

Commit 237c31c

Browse files
committed
Merge tag 'apparmor-pr-2024-01-18' of git://git.kernel.org/pub/scm/linux/kernel/git/jj/linux-apparmor
Pull AppArmor updates from John Johansen: "This adds a single feature, switch the hash used to check policy from sha1 to sha256 There are fixes for two memory leaks, and refcount bug and a potential crash when a profile name is empty. Along with a couple minor code cleanups. Summary: Features - switch policy hash from sha1 to sha256 Bug Fixes - Fix refcount leak in task_kill - Fix leak of pdb objects and trans_table - avoid crash when parse profie name is empty Cleanups - add static to stack_msg and nulldfa - more kernel-doc cleanups" * tag 'apparmor-pr-2024-01-18' of git://git.kernel.org/pub/scm/linux/kernel/git/jj/linux-apparmor: apparmor: Fix memory leak in unpack_profile() apparmor: avoid crash when parsed profile name is empty apparmor: fix possible memory leak in unpack_trans_table apparmor: free the allocated pdb objects apparmor: Fix ref count leak in task_kill apparmor: cleanup network hook comments apparmor: add missing params to aa_may_ptrace kernel-doc comments apparmor: declare nulldfa as static apparmor: declare stack_msg as static apparmor: switch SECURITY_APPARMOR_HASH from sha1 to sha256
2 parents 556e2d1 + 8ead196 commit 237c31c

File tree

9 files changed

+54
-74
lines changed

9 files changed

+54
-74
lines changed

security/apparmor/Kconfig

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ config SECURITY_APPARMOR_INTROSPECT_POLICY
5757
cpu is paramount.
5858

5959
config SECURITY_APPARMOR_HASH
60-
bool "Enable introspection of sha1 hashes for loaded profiles"
60+
bool "Enable introspection of sha256 hashes for loaded profiles"
6161
depends on SECURITY_APPARMOR_INTROSPECT_POLICY
6262
select CRYPTO
63-
select CRYPTO_SHA1
63+
select CRYPTO_SHA256
6464
default y
6565
help
6666
This option selects whether introspection of loaded policy
@@ -74,10 +74,10 @@ config SECURITY_APPARMOR_HASH_DEFAULT
7474
depends on SECURITY_APPARMOR_HASH
7575
default y
7676
help
77-
This option selects whether sha1 hashing of loaded policy
78-
is enabled by default. The generation of sha1 hashes for
79-
loaded policy provide system administrators a quick way
80-
to verify that policy in the kernel matches what is expected,
77+
This option selects whether sha256 hashing of loaded policy
78+
is enabled by default. The generation of sha256 hashes for
79+
loaded policy provide system administrators a quick way to
80+
verify that policy in the kernel matches what is expected,
8181
however it can slow down policy load on some devices. In
8282
these cases policy hashing can be disabled by default and
8383
enabled only if needed.

security/apparmor/apparmorfs.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
14741474
rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
14751475

14761476
if (aa_g_hash_policy) {
1477-
dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
1477+
dent = aafs_create_file("sha256", S_IFREG | 0444, dir,
14781478
rawdata, &seq_rawdata_hash_fops);
14791479
if (IS_ERR(dent))
14801480
goto fail;
@@ -1643,11 +1643,11 @@ static const char *rawdata_get_link_base(struct dentry *dentry,
16431643
return target;
16441644
}
16451645

1646-
static const char *rawdata_get_link_sha1(struct dentry *dentry,
1646+
static const char *rawdata_get_link_sha256(struct dentry *dentry,
16471647
struct inode *inode,
16481648
struct delayed_call *done)
16491649
{
1650-
return rawdata_get_link_base(dentry, inode, done, "sha1");
1650+
return rawdata_get_link_base(dentry, inode, done, "sha256");
16511651
}
16521652

16531653
static const char *rawdata_get_link_abi(struct dentry *dentry,
@@ -1664,8 +1664,8 @@ static const char *rawdata_get_link_data(struct dentry *dentry,
16641664
return rawdata_get_link_base(dentry, inode, done, "raw_data");
16651665
}
16661666

1667-
static const struct inode_operations rawdata_link_sha1_iops = {
1668-
.get_link = rawdata_get_link_sha1,
1667+
static const struct inode_operations rawdata_link_sha256_iops = {
1668+
.get_link = rawdata_get_link_sha256,
16691669
};
16701670

16711671
static const struct inode_operations rawdata_link_abi_iops = {
@@ -1738,7 +1738,7 @@ int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
17381738
profile->dents[AAFS_PROF_ATTACH] = dent;
17391739

17401740
if (profile->hash) {
1741-
dent = create_profile_file(dir, "sha1", profile,
1741+
dent = create_profile_file(dir, "sha256", profile,
17421742
&seq_profile_hash_fops);
17431743
if (IS_ERR(dent))
17441744
goto fail;
@@ -1748,9 +1748,9 @@ int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
17481748
#ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
17491749
if (profile->rawdata) {
17501750
if (aa_g_hash_policy) {
1751-
dent = aafs_create("raw_sha1", S_IFLNK | 0444, dir,
1751+
dent = aafs_create("raw_sha256", S_IFLNK | 0444, dir,
17521752
profile->label.proxy, NULL, NULL,
1753-
&rawdata_link_sha1_iops);
1753+
&rawdata_link_sha256_iops);
17541754
if (IS_ERR(dent))
17551755
goto fail;
17561756
aa_get_proxy(profile->label.proxy);

security/apparmor/crypto.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ static int __init init_profile_hash(void)
106106
if (!apparmor_initialized)
107107
return 0;
108108

109-
tfm = crypto_alloc_shash("sha1", 0, 0);
109+
tfm = crypto_alloc_shash("sha256", 0, 0);
110110
if (IS_ERR(tfm)) {
111111
int error = PTR_ERR(tfm);
112-
AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
112+
AA_ERROR("failed to setup profile sha256 hashing: %d\n", error);
113113
return error;
114114
}
115115
apparmor_tfm = tfm;
116116
apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
117117

118-
aa_info_message("AppArmor sha1 policy hashing enabled");
118+
aa_info_message("AppArmor sha256 policy hashing enabled");
119119

120120
return 0;
121121
}

security/apparmor/domain.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ static int change_profile_perms_wrapper(const char *op, const char *name,
13111311
return error;
13121312
}
13131313

1314-
const char *stack_msg = "change_profile unprivileged unconfined converted to stacking";
1314+
static const char *stack_msg = "change_profile unprivileged unconfined converted to stacking";
13151315

13161316
/**
13171317
* aa_change_profile - perform a one-way profile transition

security/apparmor/lib.c

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void aa_free_str_table(struct aa_str_table *t)
4141
kfree_sensitive(t->table[i]);
4242
kfree_sensitive(t->table);
4343
t->table = NULL;
44+
t->size = 0;
4445
}
4546
}
4647

security/apparmor/lsm.c

+17-46
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,6 @@ static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo
10231023
cl = aa_get_newest_cred_label(cred);
10241024
error = aa_may_signal(cred, cl, tc, tl, sig);
10251025
aa_put_label(cl);
1026-
return error;
10271026
} else {
10281027
cl = __begin_current_label_crit_section();
10291028
error = aa_may_signal(current_cred(), cl, tc, tl, sig);
@@ -1056,9 +1055,6 @@ static int apparmor_userns_create(const struct cred *cred)
10561055
return error;
10571056
}
10581057

1059-
/**
1060-
* apparmor_sk_alloc_security - allocate and attach the sk_security field
1061-
*/
10621058
static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
10631059
{
10641060
struct aa_sk_ctx *ctx;
@@ -1072,9 +1068,6 @@ static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
10721068
return 0;
10731069
}
10741070

1075-
/**
1076-
* apparmor_sk_free_security - free the sk_security field
1077-
*/
10781071
static void apparmor_sk_free_security(struct sock *sk)
10791072
{
10801073
struct aa_sk_ctx *ctx = aa_sock(sk);
@@ -1087,6 +1080,8 @@ static void apparmor_sk_free_security(struct sock *sk)
10871080

10881081
/**
10891082
* apparmor_sk_clone_security - clone the sk_security field
1083+
* @sk: sock to have security cloned
1084+
* @newsk: sock getting clone
10901085
*/
10911086
static void apparmor_sk_clone_security(const struct sock *sk,
10921087
struct sock *newsk)
@@ -1103,9 +1098,6 @@ static void apparmor_sk_clone_security(const struct sock *sk,
11031098
new->peer = aa_get_label(ctx->peer);
11041099
}
11051100

1106-
/**
1107-
* apparmor_socket_create - check perms before creating a new socket
1108-
*/
11091101
static int apparmor_socket_create(int family, int type, int protocol, int kern)
11101102
{
11111103
struct aa_label *label;
@@ -1127,10 +1119,14 @@ static int apparmor_socket_create(int family, int type, int protocol, int kern)
11271119

11281120
/**
11291121
* apparmor_socket_post_create - setup the per-socket security struct
1122+
* @sock: socket that is being setup
1123+
* @family: family of socket being created
1124+
* @type: type of the socket
1125+
* @ptotocol: protocol of the socket
1126+
* @kern: socket is a special kernel socket
11301127
*
11311128
* Note:
1132-
* - kernel sockets currently labeled unconfined but we may want to
1133-
* move to a special kernel label
1129+
* - kernel sockets labeled kernel_t used to use unconfined
11341130
* - socket may not have sk here if created with sock_create_lite or
11351131
* sock_alloc. These should be accept cases which will be handled in
11361132
* sock_graft.
@@ -1156,9 +1152,6 @@ static int apparmor_socket_post_create(struct socket *sock, int family,
11561152
return 0;
11571153
}
11581154

1159-
/**
1160-
* apparmor_socket_bind - check perms before bind addr to socket
1161-
*/
11621155
static int apparmor_socket_bind(struct socket *sock,
11631156
struct sockaddr *address, int addrlen)
11641157
{
@@ -1172,9 +1165,6 @@ static int apparmor_socket_bind(struct socket *sock,
11721165
aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
11731166
}
11741167

1175-
/**
1176-
* apparmor_socket_connect - check perms before connecting @sock to @address
1177-
*/
11781168
static int apparmor_socket_connect(struct socket *sock,
11791169
struct sockaddr *address, int addrlen)
11801170
{
@@ -1188,9 +1178,6 @@ static int apparmor_socket_connect(struct socket *sock,
11881178
aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
11891179
}
11901180

1191-
/**
1192-
* apparmor_socket_listen - check perms before allowing listen
1193-
*/
11941181
static int apparmor_socket_listen(struct socket *sock, int backlog)
11951182
{
11961183
AA_BUG(!sock);
@@ -1202,9 +1189,7 @@ static int apparmor_socket_listen(struct socket *sock, int backlog)
12021189
aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
12031190
}
12041191

1205-
/**
1206-
* apparmor_socket_accept - check perms before accepting a new connection.
1207-
*
1192+
/*
12081193
* Note: while @newsock is created and has some information, the accept
12091194
* has not been done.
12101195
*/
@@ -1233,18 +1218,12 @@ static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
12331218
aa_sk_perm(op, request, sock->sk));
12341219
}
12351220

1236-
/**
1237-
* apparmor_socket_sendmsg - check perms before sending msg to another socket
1238-
*/
12391221
static int apparmor_socket_sendmsg(struct socket *sock,
12401222
struct msghdr *msg, int size)
12411223
{
12421224
return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
12431225
}
12441226

1245-
/**
1246-
* apparmor_socket_recvmsg - check perms before receiving a message
1247-
*/
12481227
static int apparmor_socket_recvmsg(struct socket *sock,
12491228
struct msghdr *msg, int size, int flags)
12501229
{
@@ -1263,17 +1242,11 @@ static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
12631242
aa_sk_perm(op, request, sock->sk));
12641243
}
12651244

1266-
/**
1267-
* apparmor_socket_getsockname - check perms before getting the local address
1268-
*/
12691245
static int apparmor_socket_getsockname(struct socket *sock)
12701246
{
12711247
return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
12721248
}
12731249

1274-
/**
1275-
* apparmor_socket_getpeername - check perms before getting remote address
1276-
*/
12771250
static int apparmor_socket_getpeername(struct socket *sock)
12781251
{
12791252
return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
@@ -1292,29 +1265,20 @@ static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
12921265
aa_sk_perm(op, request, sock->sk));
12931266
}
12941267

1295-
/**
1296-
* apparmor_socket_getsockopt - check perms before getting socket options
1297-
*/
12981268
static int apparmor_socket_getsockopt(struct socket *sock, int level,
12991269
int optname)
13001270
{
13011271
return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
13021272
level, optname);
13031273
}
13041274

1305-
/**
1306-
* apparmor_socket_setsockopt - check perms before setting socket options
1307-
*/
13081275
static int apparmor_socket_setsockopt(struct socket *sock, int level,
13091276
int optname)
13101277
{
13111278
return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
13121279
level, optname);
13131280
}
13141281

1315-
/**
1316-
* apparmor_socket_shutdown - check perms before shutting down @sock conn
1317-
*/
13181282
static int apparmor_socket_shutdown(struct socket *sock, int how)
13191283
{
13201284
return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
@@ -1323,6 +1287,8 @@ static int apparmor_socket_shutdown(struct socket *sock, int how)
13231287
#ifdef CONFIG_NETWORK_SECMARK
13241288
/**
13251289
* apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1290+
* @sk: sk to associate @skb with
1291+
* @skb: skb to check for perms
13261292
*
13271293
* Note: can not sleep may be called with locks held
13281294
*
@@ -1354,6 +1320,11 @@ static struct aa_label *sk_peer_label(struct sock *sk)
13541320

13551321
/**
13561322
* apparmor_socket_getpeersec_stream - get security context of peer
1323+
* @sock: socket that we are trying to get the peer context of
1324+
* @optval: output - buffer to copy peer name to
1325+
* @optlen: output - size of copied name in @optval
1326+
* @len: size of @optval buffer
1327+
* Returns: 0 on success, -errno of failure
13571328
*
13581329
* Note: for tcp only valid if using ipsec or cipso on lan
13591330
*/
@@ -2182,7 +2153,7 @@ __initcall(apparmor_nf_ip_init);
21822153
static char nulldfa_src[] = {
21832154
#include "nulldfa.in"
21842155
};
2185-
struct aa_dfa *nulldfa;
2156+
static struct aa_dfa *nulldfa;
21862157

21872158
static char stacksplitdfa_src[] = {
21882159
#include "stacksplitdfa.in"

security/apparmor/policy.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ const char *const aa_profile_mode_names[] = {
9999
};
100100

101101

102-
static void aa_free_pdb(struct aa_policydb *policy)
102+
static void aa_free_pdb(struct aa_policydb *pdb)
103103
{
104-
if (policy) {
105-
aa_put_dfa(policy->dfa);
106-
if (policy->perms)
107-
kvfree(policy->perms);
108-
aa_free_str_table(&policy->trans);
104+
if (pdb) {
105+
aa_put_dfa(pdb->dfa);
106+
if (pdb->perms)
107+
kvfree(pdb->perms);
108+
aa_free_str_table(&pdb->trans);
109+
kfree(pdb);
109110
}
110111
}
111112

security/apparmor/policy_unpack.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ static bool unpack_trans_table(struct aa_ext *e, struct aa_str_table *strs)
478478
if (!table)
479479
goto fail;
480480

481+
strs->table = table;
482+
strs->size = size;
481483
for (i = 0; i < size; i++) {
482484
char *str;
483485
int c, j, pos, size2 = aa_unpack_strdup(e, &str, NULL);
@@ -520,14 +522,11 @@ static bool unpack_trans_table(struct aa_ext *e, struct aa_str_table *strs)
520522
goto fail;
521523
if (!aa_unpack_nameX(e, AA_STRUCTEND, NULL))
522524
goto fail;
523-
524-
strs->table = table;
525-
strs->size = size;
526525
}
527526
return true;
528527

529528
fail:
530-
kfree_sensitive(table);
529+
aa_free_str_table(strs);
531530
e->pos = saved_pos;
532531
return false;
533532
}
@@ -833,6 +832,10 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
833832

834833
tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len);
835834
if (tmpns) {
835+
if (!tmpname) {
836+
info = "empty profile name";
837+
goto fail;
838+
}
836839
*ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL);
837840
if (!*ns_name) {
838841
info = "out of memory";
@@ -1022,8 +1025,10 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
10221025
}
10231026
} else if (rules->policy->dfa &&
10241027
rules->policy->start[AA_CLASS_FILE]) {
1028+
aa_put_pdb(rules->file);
10251029
rules->file = aa_get_pdb(rules->policy);
10261030
} else {
1031+
aa_put_pdb(rules->file);
10271032
rules->file = aa_get_pdb(nullpdb);
10281033
}
10291034
error = -EPROTO;

security/apparmor/task.c

+2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ static int profile_tracer_perm(const struct cred *cred,
278278

279279
/**
280280
* aa_may_ptrace - test if tracer task can trace the tracee
281+
* @tracer_cred: cred of task doing the tracing (NOT NULL)
281282
* @tracer: label of the task doing the tracing (NOT NULL)
283+
* @tracee_cred: cred of task to be traced
282284
* @tracee: task label to be traced
283285
* @request: permission request
284286
*

0 commit comments

Comments
 (0)