Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1733328 > unrolled thread

[BUG] security_release_secctx seems broken

Started byKonstantin Khlebnikov <khlebnikov@yandex-team.ru>
First post2017-09-16 20:20 +0200
Last post2017-09-20 13:50 +0200
Articles 5 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [BUG] security_release_secctx seems broken Konstantin Khlebnikov <khlebnikov@yandex-team.ru> - 2017-09-16 20:20 +0200
    Re: [BUG] security_release_secctx seems broken Casey Schaufler <casey@schaufler-ca.com> - 2017-09-18 22:30 +0200
      Re: [BUG] security_release_secctx seems broken Casey Schaufler <casey@schaufler-ca.com> - 2017-09-19 06:30 +0200
      [PATCH] fix security_release_secctx seems broken Casey Schaufler <casey@schaufler-ca.com> - 2017-09-19 18:40 +0200
        Re: [PATCH] fix security_release_secctx seems broken Konstantin Khlebnikov <khlebnikov@yandex-team.ru> - 2017-09-20 13:50 +0200

#1733328 — [BUG] security_release_secctx seems broken

FromKonstantin Khlebnikov <khlebnikov@yandex-team.ru>
Date2017-09-16 20:20 +0200
Subject[BUG] security_release_secctx seems broken
Message-ID<uqpR8-5hV-3@gated-at.bofh.it>
I've got this kmemleak splat

unreferenced object 0xffff880f687ff6a8 (size 32):
   comm "cp", pid 4279, jiffies 4295784487 (age 2866.296s)
   hex dump (first 32 bytes):
     01 00 00 02 02 00 08 00 00 00 00 00 00 00 00 00  ................
     00 00 00 00 00 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5  .....kkkkkkkkkk.
   backtrace:
     [<ffffffff8192d77a>] kmemleak_alloc+0x4a/0xa0
     [<ffffffff8125b721>] __kmalloc_track_caller+0x171/0x280
     [<ffffffff812125c7>] krealloc+0xa7/0xc0
     [<ffffffff812abb2d>] vfs_getxattr_alloc+0xbd/0x110
     [<ffffffff813ba789>] cap_inode_getsecurity+0x79/0x200
     [<ffffffff813be4d2>] security_inode_getsecurity+0x52/0x80
     [<ffffffff812aad1a>] xattr_getsecurity+0x3a/0xa0
     [<ffffffff812aadf4>] vfs_getxattr+0x74/0xa0
     [<ffffffff812ab27e>] getxattr+0x9e/0x170
     [<ffffffff812abd2a>] SyS_fgetxattr+0x5a/0x90
     [<ffffffff8193a67b>] entry_SYSCALL_64_fastpath+0x1e/0xae
     [<ffffffffffffffff>] 0xffffffffffffffff

allocated in  xattr_getsecurity() security_inode_getsecurity() -> cap_inode_getsecurity()
should be freed by security_release_secctx() but there is no release_secctx hook.

I don't know details about modern security models stack but it
seems security_release_secctx() have to know which layer owns
secdata to free it property: selinux just calls kfree,
capability_hooks should do the same, but other modules returns
non-freeable pointers. Currently security_release_secctx() calls
release_secctx for all models, this is obviously wrong.

[toc] | [next] | [standalone]


#1734456

FromCasey Schaufler <casey@schaufler-ca.com>
Date2017-09-18 22:30 +0200
Message-ID<uraQ1-2OE-5@gated-at.bofh.it>
In reply to#1733328
On 9/16/2017 11:18 AM, Konstantin Khlebnikov wrote:
> I've got this kmemleak splat
>
> unreferenced object 0xffff880f687ff6a8 (size 32):
>   comm "cp", pid 4279, jiffies 4295784487 (age 2866.296s)
>   hex dump (first 32 bytes):
>     01 00 00 02 02 00 08 00 00 00 00 00 00 00 00 00  ................
>     00 00 00 00 00 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5  .....kkkkkkkkkk.
>   backtrace:
>     [<ffffffff8192d77a>] kmemleak_alloc+0x4a/0xa0
>     [<ffffffff8125b721>] __kmalloc_track_caller+0x171/0x280
>     [<ffffffff812125c7>] krealloc+0xa7/0xc0
>     [<ffffffff812abb2d>] vfs_getxattr_alloc+0xbd/0x110
>     [<ffffffff813ba789>] cap_inode_getsecurity+0x79/0x200
>     [<ffffffff813be4d2>] security_inode_getsecurity+0x52/0x80
>     [<ffffffff812aad1a>] xattr_getsecurity+0x3a/0xa0
>     [<ffffffff812aadf4>] vfs_getxattr+0x74/0xa0
>     [<ffffffff812ab27e>] getxattr+0x9e/0x170
>     [<ffffffff812abd2a>] SyS_fgetxattr+0x5a/0x90
>     [<ffffffff8193a67b>] entry_SYSCALL_64_fastpath+0x1e/0xae
>     [<ffffffffffffffff>] 0xffffffffffffffff
>
> allocated in  xattr_getsecurity() security_inode_getsecurity() -> cap_inode_getsecurity()
> should be freed by security_release_secctx() but there is no release_secctx hook.

There is a bug here, but the fix suggested is not correct.
security_inode_getsecurity() provides the security attribute
with a specified name. In the past there would be exactly one
use for this call, which was to return the value of the
attribute that happened to match the security "context" of
the inode. Freeing the value with security_release_secctx()
works coincidentally. Because security_inode_getsecurity() is
called with the "true" value for the alloc parameter the correct
fix is:

	- fix smack_inode_getsecurity() to honor the alloc flag
	- change the security_release_secctx() to kfree here.

I can provide a patch in a day or so.

>
> I don't know details about modern security models stack but it
> seems security_release_secctx() have to know which layer owns
> secdata to free it property: selinux just calls kfree,
> capability_hooks should do the same, but other modules returns
> non-freeable pointers. Currently security_release_secctx() calls
> release_secctx for all models, this is obviously wrong.
>


.

[toc] | [prev] | [next] | [standalone]


#1734651

FromCasey Schaufler <casey@schaufler-ca.com>
Date2017-09-19 06:30 +0200
Message-ID<urikx-7Ng-1@gated-at.bofh.it>
In reply to#1734456
On 9/18/2017 1:25 PM, Casey Schaufler wrote:
> On 9/16/2017 11:18 AM, Konstantin Khlebnikov wrote:
>> I've got this kmemleak splat

Do you have a convenient test case? I'd like to 
verify my patch with your case if it is reasonable
to do so.

>>
>> unreferenced object 0xffff880f687ff6a8 (size 32):
>>   comm "cp", pid 4279, jiffies 4295784487 (age 2866.296s)
>>   hex dump (first 32 bytes):
>>     01 00 00 02 02 00 08 00 00 00 00 00 00 00 00 00  ................
>>     00 00 00 00 00 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5  .....kkkkkkkkkk.
>>   backtrace:
>>     [<ffffffff8192d77a>] kmemleak_alloc+0x4a/0xa0
>>     [<ffffffff8125b721>] __kmalloc_track_caller+0x171/0x280
>>     [<ffffffff812125c7>] krealloc+0xa7/0xc0
>>     [<ffffffff812abb2d>] vfs_getxattr_alloc+0xbd/0x110
>>     [<ffffffff813ba789>] cap_inode_getsecurity+0x79/0x200
>>     [<ffffffff813be4d2>] security_inode_getsecurity+0x52/0x80
>>     [<ffffffff812aad1a>] xattr_getsecurity+0x3a/0xa0
>>     [<ffffffff812aadf4>] vfs_getxattr+0x74/0xa0
>>     [<ffffffff812ab27e>] getxattr+0x9e/0x170
>>     [<ffffffff812abd2a>] SyS_fgetxattr+0x5a/0x90
>>     [<ffffffff8193a67b>] entry_SYSCALL_64_fastpath+0x1e/0xae
>>     [<ffffffffffffffff>] 0xffffffffffffffff
>>
>> allocated in  xattr_getsecurity() security_inode_getsecurity() -> cap_inode_getsecurity()
>> should be freed by security_release_secctx() but there is no release_secctx hook.
> There is a bug here, but the fix suggested is not correct.
> security_inode_getsecurity() provides the security attribute
> with a specified name. In the past there would be exactly one
> use for this call, which was to return the value of the
> attribute that happened to match the security "context" of
> the inode. Freeing the value with security_release_secctx()
> works coincidentally. Because security_inode_getsecurity() is
> called with the "true" value for the alloc parameter the correct
> fix is:
>
> 	- fix smack_inode_getsecurity() to honor the alloc flag
> 	- change the security_release_secctx() to kfree here.
>
> I can provide a patch in a day or so.
>
>> I don't know details about modern security models stack but it
>> seems security_release_secctx() have to know which layer owns
>> secdata to free it property: selinux just calls kfree,
>> capability_hooks should do the same, but other modules returns
>> non-freeable pointers. Currently security_release_secctx() calls
>> release_secctx for all models, this is obviously wrong.
>>
>
> .
>

[toc] | [prev] | [next] | [standalone]


#1735092 — [PATCH] fix security_release_secctx seems broken

FromCasey Schaufler <casey@schaufler-ca.com>
Date2017-09-19 18:40 +0200
Subject[PATCH] fix security_release_secctx seems broken
Message-ID<urtIZ-7LM-21@gated-at.bofh.it>
In reply to#1734456
Subject: [PATCH] fix security_release_secctx seems broken

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.

Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>

---
 fs/xattr.c                 |  2 +-
 security/smack/smack_lsm.c | 55 +++++++++++++++++++++-------------------------
 2 files changed, 26 insertions(+), 31 deletions(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index 4424f7fecf14..61cd28ba25f3 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -250,7 +250,7 @@ xattr_getsecurity(struct inode *inode, const char *name, void *value,
 	}
 	memcpy(value, buffer, len);
 out:
-	security_release_secctx(buffer, len);
+	kfree(buffer);
 out_noalloc:
 	return len;
 }
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 319add31b4a4..286171a16ed2 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -1473,7 +1473,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name)
  * @inode: the object
  * @name: attribute name
  * @buffer: where to put the result
- * @alloc: unused
+ * @alloc: duplicate memory
  *
  * Returns the size of the attribute or an error code
  */
@@ -1486,43 +1486,38 @@ static int smack_inode_getsecurity(struct inode *inode,
 	struct super_block *sbp;
 	struct inode *ip = (struct inode *)inode;
 	struct smack_known *isp;
-	int ilen;
-	int rc = 0;
 
-	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
+	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
 		isp = smk_of_inode(inode);
-		ilen = strlen(isp->smk_known);
-		*buffer = isp->smk_known;
-		return ilen;
-	}
+	else {
+		/*
+		 * The rest of the Smack xattrs are only on sockets.
+		 */
+		sbp = ip->i_sb;
+		if (sbp->s_magic != SOCKFS_MAGIC)
+			return -EOPNOTSUPP;
 
-	/*
-	 * The rest of the Smack xattrs are only on sockets.
-	 */
-	sbp = ip->i_sb;
-	if (sbp->s_magic != SOCKFS_MAGIC)
-		return -EOPNOTSUPP;
+		sock = SOCKET_I(ip);
+		if (sock == NULL || sock->sk == NULL)
+			return -EOPNOTSUPP;
 
-	sock = SOCKET_I(ip);
-	if (sock == NULL || sock->sk == NULL)
-		return -EOPNOTSUPP;
-
-	ssp = sock->sk->sk_security;
+		ssp = sock->sk->sk_security;
 
-	if (strcmp(name, XATTR_SMACK_IPIN) == 0)
-		isp = ssp->smk_in;
-	else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
-		isp = ssp->smk_out;
-	else
-		return -EOPNOTSUPP;
+		if (strcmp(name, XATTR_SMACK_IPIN) == 0)
+			isp = ssp->smk_in;
+		else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
+			isp = ssp->smk_out;
+		else
+			return -EOPNOTSUPP;
+	}
 
-	ilen = strlen(isp->smk_known);
-	if (rc == 0) {
-		*buffer = isp->smk_known;
-		rc = ilen;
+	if (alloc) {
+		*buffer = kstrdup(isp->smk_known, GFP_KERNEL);
+		if (*buffer == NULL)
+			return -ENOMEM;
 	}
 
-	return rc;
+	return strlen(isp->smk_known);
 }
 
 

[toc] | [prev] | [next] | [standalone]


#1735733 — Re: [PATCH] fix security_release_secctx seems broken

FromKonstantin Khlebnikov <khlebnikov@yandex-team.ru>
Date2017-09-20 13:50 +0200
SubjectRe: [PATCH] fix security_release_secctx seems broken
Message-ID<urLFT-2u1-1@gated-at.bofh.it>
In reply to#1735092
On 19.09.2017 19:39, Casey Schaufler wrote:
> Subject: [PATCH] fix security_release_secctx seems broken
> 
> 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.
> 
> Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>

Looks good for me.

Reviewed-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>


> 
> ---
>   fs/xattr.c                 |  2 +-
>   security/smack/smack_lsm.c | 55 +++++++++++++++++++++-------------------------
>   2 files changed, 26 insertions(+), 31 deletions(-)
> 
> diff --git a/fs/xattr.c b/fs/xattr.c
> index 4424f7fecf14..61cd28ba25f3 100644
> --- a/fs/xattr.c
> +++ b/fs/xattr.c
> @@ -250,7 +250,7 @@ xattr_getsecurity(struct inode *inode, const char *name, void *value,
>   	}
>   	memcpy(value, buffer, len);
>   out:
> -	security_release_secctx(buffer, len);
> +	kfree(buffer);
>   out_noalloc:
>   	return len;
>   }
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 319add31b4a4..286171a16ed2 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -1473,7 +1473,7 @@ static int smack_inode_removexattr(struct dentry *dentry, const char *name)
>    * @inode: the object
>    * @name: attribute name
>    * @buffer: where to put the result
> - * @alloc: unused
> + * @alloc: duplicate memory
>    *
>    * Returns the size of the attribute or an error code
>    */
> @@ -1486,43 +1486,38 @@ static int smack_inode_getsecurity(struct inode *inode,
>   	struct super_block *sbp;
>   	struct inode *ip = (struct inode *)inode;
>   	struct smack_known *isp;
> -	int ilen;
> -	int rc = 0;
>   
> -	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
> +	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
>   		isp = smk_of_inode(inode);
> -		ilen = strlen(isp->smk_known);
> -		*buffer = isp->smk_known;
> -		return ilen;
> -	}
> +	else {
> +		/*
> +		 * The rest of the Smack xattrs are only on sockets.
> +		 */
> +		sbp = ip->i_sb;
> +		if (sbp->s_magic != SOCKFS_MAGIC)
> +			return -EOPNOTSUPP;
>   
> -	/*
> -	 * The rest of the Smack xattrs are only on sockets.
> -	 */
> -	sbp = ip->i_sb;
> -	if (sbp->s_magic != SOCKFS_MAGIC)
> -		return -EOPNOTSUPP;
> +		sock = SOCKET_I(ip);
> +		if (sock == NULL || sock->sk == NULL)
> +			return -EOPNOTSUPP;
>   
> -	sock = SOCKET_I(ip);
> -	if (sock == NULL || sock->sk == NULL)
> -		return -EOPNOTSUPP;
> -
> -	ssp = sock->sk->sk_security;
> +		ssp = sock->sk->sk_security;
>   
> -	if (strcmp(name, XATTR_SMACK_IPIN) == 0)
> -		isp = ssp->smk_in;
> -	else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
> -		isp = ssp->smk_out;
> -	else
> -		return -EOPNOTSUPP;
> +		if (strcmp(name, XATTR_SMACK_IPIN) == 0)
> +			isp = ssp->smk_in;
> +		else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
> +			isp = ssp->smk_out;
> +		else
> +			return -EOPNOTSUPP;
> +	}
>   
> -	ilen = strlen(isp->smk_known);
> -	if (rc == 0) {
> -		*buffer = isp->smk_known;
> -		rc = ilen;
> +	if (alloc) {
> +		*buffer = kstrdup(isp->smk_known, GFP_KERNEL);
> +		if (*buffer == NULL)
> +			return -ENOMEM;
>   	}
>   
> -	return rc;
> +	return strlen(isp->smk_known);
>   }
>   
>   
> 

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web