Skip to content

Commit dd1f96a

Browse files
cschauflergregkh
authored andcommitted
lsm: fix smack_inode_removexattr and xattr_getsecurity memleak
commit 57e7ba04d422c3d41c8426380303ec9b7533ded9 upstream. security_inode_getsecurity() provides the text string value of a security attribute. It does not provide a "secctx". The code in xattr_getsecurity() that calls security_inode_getsecurity() and then calls security_release_secctx() happened to work because SElinux and Smack treat the attribute and the secctx the same way. It fails for cap_inode_getsecurity(), because that module has no secctx that ever needs releasing. It turns out that Smack is the one that's doing things wrong by not allocating memory when instructed to do so by the "alloc" parameter. The fix is simple enough. Change the security_release_secctx() to kfree() because it isn't a secctx being returned by security_inode_getsecurity(). Change Smack to allocate the string when told to do so. Note: this also fixes memory leaks for LSMs which implement inode_getsecurity but not release_secctx, such as capabilities. Signed-off-by: Casey Schaufler <casey@schaufler-ca.com> Reported-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Signed-off-by: James Morris <james.l.morris@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a44be3e commit dd1f96a

2 files changed

Lines changed: 26 additions & 31 deletions

File tree

fs/xattr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ xattr_getsecurity(struct inode *inode, const char *name, void *value,
163163
}
164164
memcpy(value, buffer, len);
165165
out:
166-
security_release_secctx(buffer, len);
166+
kfree(buffer);
167167
out_noalloc:
168168
return len;
169169
}

security/smack/smack_lsm.c

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name)
14591459
* @inode: the object
14601460
* @name: attribute name
14611461
* @buffer: where to put the result
1462-
* @alloc: unused
1462+
* @alloc: duplicate memory
14631463
*
14641464
* Returns the size of the attribute or an error code
14651465
*/
@@ -1472,43 +1472,38 @@ static int smack_inode_getsecurity(const struct inode *inode,
14721472
struct super_block *sbp;
14731473
struct inode *ip = (struct inode *)inode;
14741474
struct smack_known *isp;
1475-
int ilen;
1476-
int rc = 0;
14771475

1478-
if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1476+
if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
14791477
isp = smk_of_inode(inode);
1480-
ilen = strlen(isp->smk_known);
1481-
*buffer = isp->smk_known;
1482-
return ilen;
1483-
}
1478+
else {
1479+
/*
1480+
* The rest of the Smack xattrs are only on sockets.
1481+
*/
1482+
sbp = ip->i_sb;
1483+
if (sbp->s_magic != SOCKFS_MAGIC)
1484+
return -EOPNOTSUPP;
14841485

1485-
/*
1486-
* The rest of the Smack xattrs are only on sockets.
1487-
*/
1488-
sbp = ip->i_sb;
1489-
if (sbp->s_magic != SOCKFS_MAGIC)
1490-
return -EOPNOTSUPP;
1486+
sock = SOCKET_I(ip);
1487+
if (sock == NULL || sock->sk == NULL)
1488+
return -EOPNOTSUPP;
14911489

1492-
sock = SOCKET_I(ip);
1493-
if (sock == NULL || sock->sk == NULL)
1494-
return -EOPNOTSUPP;
1495-
1496-
ssp = sock->sk->sk_security;
1490+
ssp = sock->sk->sk_security;
14971491

1498-
if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1499-
isp = ssp->smk_in;
1500-
else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1501-
isp = ssp->smk_out;
1502-
else
1503-
return -EOPNOTSUPP;
1492+
if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1493+
isp = ssp->smk_in;
1494+
else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1495+
isp = ssp->smk_out;
1496+
else
1497+
return -EOPNOTSUPP;
1498+
}
15041499

1505-
ilen = strlen(isp->smk_known);
1506-
if (rc == 0) {
1507-
*buffer = isp->smk_known;
1508-
rc = ilen;
1500+
if (alloc) {
1501+
*buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1502+
if (*buffer == NULL)
1503+
return -ENOMEM;
15091504
}
15101505

1511-
return rc;
1506+
return strlen(isp->smk_known);
15121507
}
15131508

15141509

0 commit comments

Comments
 (0)