diff mbox

Smack: Fix memory leak in smack_inode_getsecctx

Message ID fa278910-5c99-11dc-c9cb-b88a97592e53@schaufler-ca.com (mailing list archive)
State New, archived
Headers show

Commit Message

Casey Schaufler June 1, 2018, 5:45 p.m. UTC
Fix memory leak in smack_inode_getsecctx

The implementation of smack_inode_getsecctx() made
incorrect assumptions about how Smack presents a security
context. Smack does not need to allocate memory to support
security contexts, so "releasing" a Smack context is a no-op.
The code made an unnecessary copy and returned that as a
context, which was never freed. The revised implementation
returns the context correctly.

Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
---
 security/smack/smack_lsm.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Casey Schaufler June 4, 2018, 9:01 p.m. UTC | #1
On 6/1/2018 10:45 AM, Casey Schaufler wrote:
> Fix memory leak in smack_inode_getsecctx
>
> The implementation of smack_inode_getsecctx() made
> incorrect assumptions about how Smack presents a security
> context. Smack does not need to allocate memory to support
> security contexts, so "releasing" a Smack context is a no-op.
> The code made an unnecessary copy and returned that as a
> context, which was never freed. The revised implementation
> returns the context correctly.
>
> Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>

Tejun, does this pass your tests?

> ---
>  security/smack/smack_lsm.c | 12 +++++-------
>  1 file changed, 5 insertions(+), 7 deletions(-)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 0b414836bebd..5e3beae334a8 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -1545,9 +1545,9 @@ static int smack_inode_listsecurity(struct inode *inode, char *buffer,
>   */
>  static void smack_inode_getsecid(struct inode *inode, u32 *secid)
>  {
> -	struct inode_smack *isp = inode->i_security;
> +	struct smack_known *skp = smk_of_inode(inode);
>  
> -	*secid = isp->smk_inode->smk_secid;
> +	*secid = skp->smk_secid;
>  }
>  
>  /*
> @@ -4538,12 +4538,10 @@ static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  
>  static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
>  {
> -	int len = 0;
> -	len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
> +	struct smack_known *skp = smk_of_inode(inode);
>  
> -	if (len < 0)
> -		return len;
> -	*ctxlen = len;
> +	*ctx = skp->smk_known;
> +	*ctxlen = strlen(skp->smk_known);
>  	return 0;
>  }
>  
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Tejun Heo June 4, 2018, 9:27 p.m. UTC | #2
On Mon, Jun 04, 2018 at 02:01:34PM -0700, Casey Schaufler wrote:
> On 6/1/2018 10:45 AM, Casey Schaufler wrote:
> > Fix memory leak in smack_inode_getsecctx
> >
> > The implementation of smack_inode_getsecctx() made
> > incorrect assumptions about how Smack presents a security
> > context. Smack does not need to allocate memory to support
> > security contexts, so "releasing" a Smack context is a no-op.
> > The code made an unnecessary copy and returned that as a
> > context, which was never freed. The revised implementation
> > returns the context correctly.
> >
> > Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
> 
> Tejun, does this pass your tests?

Oh, I'm not the one who reported.  Chandan?
CHANDAN VN June 5, 2018, 7:04 a.m. UTC | #3
>On Mon, Jun 04, 2018 at 02:01:34PM -0700, Casey Schaufler wrote:

>> On 6/1/2018 10:45 AM, Casey Schaufler wrote:

>> > Fix memory leak in smack_inode_getsecctx

>> >

>> > The implementation of smack_inode_getsecctx() made

>> > incorrect assumptions about how Smack presents a security

>> > context. Smack does not need to allocate memory to support

>> > security contexts, so "releasing" a Smack context is a no-op.

>> > The code made an unnecessary copy and returned that as a

>> > context, which was never freed. The revised implementation

>> > returns the context correctly.

>> >

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

>> 

>> Tejun, does this pass your tests?

 
>Oh, I'm not the one who reported.  Chandan?

Looks good to me. Leak not found.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Casey Schaufler June 5, 2018, 2:29 p.m. UTC | #4
On 6/5/2018 12:04 AM, CHANDAN VN wrote:
>  
>
>> On Mon, Jun 04, 2018 at 02:01:34PM -0700, Casey Schaufler wrote:
>>>  On 6/1/2018 10:45 AM, Casey Schaufler wrote:
>>>  > Fix memory leak in smack_inode_getsecctx
>>>  >
>>>  > The implementation of smack_inode_getsecctx() made
>>>  > incorrect assumptions about how Smack presents a security
>>>  > context. Smack does not need to allocate memory to support
>>>  > security contexts, so "releasing" a Smack context is a no-op.
>>>  > The code made an unnecessary copy and returned that as a
>>>  > context, which was never freed. The revised implementation
>>>  > returns the context correctly.
>>>  >
>>>  > Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
>>>  
>>>  Tejun, does this pass your tests?
>  
>> Oh, I'm not the one who reported.  Chandan?
> Looks good to me. Leak not found.

Thanks. Can I add your "Tested-by:" on the commit?

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
CHANDAN VN June 5, 2018, 2:46 p.m. UTC | #5
>On 6/5/2018 12:04 AM, CHANDAN VN wrote:

>>  

>>

>>> On Mon, Jun 04, 2018 at 02:01:34PM -0700, Casey Schaufler wrote:

>>>>  On 6/1/2018 10:45 AM, Casey Schaufler wrote:

>>>>  > Fix memory leak in smack_inode_getsecctx

>>>>  >

>>>>  > The implementation of smack_inode_getsecctx() made

>>>>  > incorrect assumptions about how Smack presents a security

>>>>  > context. Smack does not need to allocate memory to support

>>>>  > security contexts, so "releasing" a Smack context is a no-op.

>>>>  > The code made an unnecessary copy and returned that as a

>>>>  > context, which was never freed. The revised implementation

>>>>  > returns the context correctly.

>>>>  >

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

>>>>  

>>>>  Tejun, does this pass your tests?

>>  

>>> Oh, I'm not the one who reported.  Chandan?

>> Looks good to me. Leak not found.


>Thanks. Can I add your "Tested-by:" on the commit?


Sure. I think "Reported-by:" would also be ok.
 
 
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 0b414836bebd..5e3beae334a8 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -1545,9 +1545,9 @@  static int smack_inode_listsecurity(struct inode *inode, char *buffer,
  */
 static void smack_inode_getsecid(struct inode *inode, u32 *secid)
 {
-	struct inode_smack *isp = inode->i_security;
+	struct smack_known *skp = smk_of_inode(inode);
 
-	*secid = isp->smk_inode->smk_secid;
+	*secid = skp->smk_secid;
 }
 
 /*
@@ -4538,12 +4538,10 @@  static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
 
 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
 {
-	int len = 0;
-	len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
+	struct smack_known *skp = smk_of_inode(inode);
 
-	if (len < 0)
-		return len;
-	*ctxlen = len;
+	*ctx = skp->smk_known;
+	*ctxlen = strlen(skp->smk_known);
 	return 0;
 }