Skip to contentSkip to conversation

security/keys: remove possessor verify after key

RFC PATCH Jarkko Sakkinen 3 participants 10 messages May 29, 2020 – Sep 2, 2020 Keys/keyrings

lore
Share & tools
lore
#0 lore raw
Quoted 70 lines · Alexey Krasikov

During the test I discovered that not all access rights restrictions working correct. Examples:

Remove possessor write right:

$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
47831201: alswrv-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith
$ keyctl update $KEYID dohn
$ keyctl print $KEYID
dohn
$ keyctl setperm $KEYID 0x3b000000
$ keyctl describe $KEYID
47831201: als-rv------------------  1000  1000 user: john
$ keyctl update $KEYID dohn
keyctl_update: Permission denied
$ keyctl print $KEYID
dohn

That OK. So, next
Remove possessor set attributes right:

$ keyctl setperm $KEYID 0x1f000000
$ keyctl describe $KEYID
47831201: -lswrv------------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3f000000
keyctl_update: Permission denied

That OK. So, next
Remove possessor read right:

$ keyctl unlink $KEYID
1 links removed
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

How's that? I removed the right to read, but the possessor continues to read it!

I opened the read function code and saw the following:

```
  /* see if we can read it directly */
  ret = key_permission(key_ref, KEY_NEED_READ); /* OK, we returned error */
                                                /* read access */
  if (ret = 0) /* skip that */
    goto can_read_key;
  if (ret != -EACCES) /* skip that */
    goto key_put_out;

  /* we can't; see if it's searchable from this process's keyrings
   * - we automatically take account of the fact that it may be
   *   dangling off an instantiation key
   */
  if (!is_key_possessed(key_ref)) { /* If we possessor = can read */
    ret = -EACCES;
    goto key_put_out;
  }

  /* the key is probably readable - now try to read it */
can_read_key:
```
Quoted 11 lines

Why can we read the key, if we don't have rights for doing this?

Alexey Krasikov (1):
security/keys: remove possessor verify after key permission check

 security/keys/keyctl.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
Signature · 1 line
2.17.1

Thank you for the legit examples and effort in general. Looking into this with time on Monday. Today I have to get unfortunately SGX patch set out.

/Jarkko

#1 lore raw
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

#2 lore raw
In reply to Jarkko Sakkinen
Quoted 21 lines · Alexey Krasikov
Quoted 20 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

Quoted 7 lines
Quoted 1 line

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

#3 lore raw
In reply to Jarkko Sakkinen
Quoted 42 lines · Alexey Krasikov
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Alexey Krasikov
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

Quoted 7 lines
$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

Quoted 8 lines
Quoted 1 line

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a different version of Linux.

Quoted 7 lines

I get to reproduce the problem on two systems:

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

thanks,

greg k-h

#4 lore raw
In reply to Jarkko Sakkinen
Quoted 45 lines · Greg KH
Quoted 44 lines · Alexey Krasikov
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Alexey Krasikov
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

Quoted 7 lines
$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

Quoted 8 lines
Quoted 1 line

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a different version of Linux.

I get to reproduce the problem on two systems:

Quoted 8 lines
Quoted 5 lines

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

Alexey,

A shell script containing the keyctl command chain with some output, and then your version of the output when running the script would be also very useful for better comparison.

/Jarkko

#5 lore raw
In reply to Jarkko Sakkinen
Quoted 52 lines · Jarkko Sakkinen
Quoted 44 lines · Greg KH
Quoted 43 lines · Alexey Krasikov
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Alexey Krasikov
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

Quoted 7 lines
$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

Quoted 8 lines
Quoted 1 line

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a different version of Linux.

I get to reproduce the problem on two systems:

Quoted 7 lines
Quoted 5 lines

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

Quoted 7 lines

Alexey,

A shell script containing the keyctl command chain with some output, and then your version of the output when running the script would be also very useful for better comparison.

/Jarkko

Ok. I have the following script:

----------------------------------------------------------------------------
#!/usr/bin/sh

uname -r

KEYID=$(keyctl add user john smith @u)
keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID
----------------------------------------------------------------------------

I installed the Manjaro distribution, updated the kernel to version 5.7.0-3, and when running this script, I got the following output:

****************************************************************************
5.7.0-3-MANJARO
914983072: alswrv-----v------------  1000  1000 user: john 914983072: alsw-v------------------  1000  1000 user: john smith
****************************************************************************

In fairness, I note that none of these distro-specific kernels does not change the work logic of the Linux keyring.

/Alexey

#6 lore raw
In reply to Jarkko Sakkinen
Quoted 66 lines · Alexey Krasikov
Quoted 52 lines · Jarkko Sakkinen
Quoted 44 lines · Greg KH
Quoted 43 lines · Alexey Krasikov
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Alexey Krasikov
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

Quoted 7 lines
$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

Quoted 8 lines
Quoted 1 line

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a different version of Linux.

I get to reproduce the problem on two systems:

Quoted 7 lines
Quoted 5 lines

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

Quoted 7 lines

Alexey,

A shell script containing the keyctl command chain with some output, and then your version of the output when running the script would be also very useful for better comparison.

/Jarkko

Ok. I have the following script:

----------------------------------------------------------------------------
#!/usr/bin/sh

Quoted 7 lines

uname -r

KEYID=$(keyctl add user john smith @u)
keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

pam_keyinit.so should create user keyring when the login session is created. If the user space stack is working correclty, you should not end up to be the possessor for the user keyring.

However, I can simulate your environment with the session keyring:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

And yes I do get:

564302411: alswrv-----v------------  1000  1000 user: john
564302411: alsw-v------------------  1000  1000 user: john

smith

Here's another sequence that also removes setattr:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x08000000
keyctl describe $KEYID
keyctl print $KEYID

700153280: alswrv-----v------------ 1000 1000 user: john
keyctl_describe_alloc: Permission denied
smith

David, this look at least with a quick sight somewhat weird: my possessor permissions are only "search", so why does reading the payload succeed?

/Jarkko

#7 lore raw
In reply to Jarkko Sakkinen
Quoted 105 lines · Jarkko Sakkinen
Quoted 65 lines · Alexey Krasikov
Quoted 52 lines · Jarkko Sakkinen
Quoted 44 lines · Greg KH
Quoted 43 lines · Alexey Krasikov
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Alexey Krasikov
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

Quoted 7 lines
$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

Quoted 8 lines
Quoted 1 line

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a different version of Linux.

I get to reproduce the problem on two systems:

Quoted 7 lines
Quoted 5 lines

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

Quoted 7 lines

Alexey,

A shell script containing the keyctl command chain with some output, and then your version of the output when running the script would be also very useful for better comparison.

/Jarkko

Ok. I have the following script:

----------------------------------------------------------------------------
#!/usr/bin/sh

Quoted 7 lines

uname -r

KEYID=$(keyctl add user john smith @u)
keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

pam_keyinit.so should create user keyring when the login session is created. If the user space stack is working correclty, you should not end up to be the possessor for the user keyring.

However, I can simulate your environment with the session keyring:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

And yes I do get:

564302411: alswrv-----v------------  1000  1000 user: john
564302411: alsw-v------------------  1000  1000 user: john

smith

Here's another sequence that also removes setattr:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x08000000
keyctl describe $KEYID
keyctl print $KEYID

700153280: alswrv-----v------------ 1000 1000 user: john
keyctl_describe_alloc: Permission denied
smith

Quoted 5 lines

David, this look at least with a quick sight somewhat weird: my possessor permissions are only "search", so why does reading the payload succeed?

/Jarkko

ping

/Alexey

#8 lore raw
In reply to Jarkko Sakkinen
Quoted 111 lines · Alexey Krasikov
Quoted 110 lines · Jarkko Sakkinen
Quoted 70 lines · Alexey Krasikov
Quoted 55 lines · Jarkko Sakkinen
Quoted 47 lines · Greg KH
Quoted 46 lines · Alexey Krasikov
Quoted 32 lines · Jarkko Sakkinen
Quoted 19 lines · Alexey Krasikov
Quoted 18 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied
Quoted 8 lines
Quoted 3 lines

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter.
It contains random dumps of keyctl output.

Quoted 5 lines

Maybe a better idea would be to write a test script that demonstrates
the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a
different version of Linux.

I get to reproduce the problem on two systems:

Quoted 7 lines
Quoted 5 lines

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

Quoted 8 lines

Alexey,

A shell script containing the keyctl command chain with some output, and
then your version of the output when running the script would be also very useful for better comparison.

/Jarkko

Ok. I have the following script:

----------------------------------------------------------------------------

#!/usr/bin/sh

Quoted 7 lines

uname -r

KEYID=$(keyctl add user john smith @u)
keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

pam_keyinit.so should create user keyring when the login session is created. If the user space stack is working correclty, you should not end up to be the possessor for the user keyring.

However, I can simulate your environment with the session keyring:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

And yes I do get:

564302411: alswrv-----v------------  1000  1000 user: john 564302411: alsw-v------------------  1000  1000 user: john smith

Here's another sequence that also removes setattr:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x08000000
keyctl describe $KEYID
keyctl print $KEYID

700153280: alswrv-----v------------  1000  1000 user: john
keyctl_describe_alloc: Permission denied
smith

Quoted 8 lines
Quoted 5 lines

David, this look at least with a quick sight somewhat weird: my possessor permissions are only "search", so why does reading the payload succeed?

/Jarkko

ping

/Alexey

ping, ping!!!

Hello everybody!

David, have you watched my correspondence with Jarkko? Do you have any ideas about this strange behavior of keyctl?

/Alexey

#9 lore raw
In reply to Jarkko Sakkinen
Quoted 105 lines · Jarkko Sakkinen
Quoted 65 lines · Alexey Krasikov
Quoted 52 lines · Jarkko Sakkinen
Quoted 44 lines · Greg KH
Quoted 43 lines · Alexey Krasikov
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Alexey Krasikov
Quoted 14 lines · Jarkko Sakkinen
Quoted 8 lines · Alexey Krasikov
$ KEYID=$(keyctl add user john smith @u)
$ keyctl describe $KEYID
5927639: alswrv-----v------------  1000  1000 user: john
$ keyctl setperm $KEYID 0x3d000000
$ keyctl describe $KEYID
5927639: alsw-v-----v------------  1000  1000 user: john
$ keyctl print $KEYID
smith

A keyring default permissions are 0x3f3f0000.
A key default permissions are 0x3f010000.

Because of this:

Quoted 7 lines
$ KEYID=$(keyctl add user john smith @u)
$ keyctl setperm $KEYID 0x3d000000
keyctl_setperm: Permission denied

Are you sure that your example is correct?

/Jarkko

Yes, this example works correctly.

Why do you think, that the current keyring and key rights

Quoted 8 lines
Quoted 1 line

shoukd not allow this to be done?

I'm just saying that I cannot figure out your point in the cover letter. It contains random dumps of keyctl output.

Maybe a better idea would be to write a test script that demonstrates the issue?

/Jarkko

+ [email protected]

Possible you may not be able to reproduce the problem because you have a different version of Linux.

I get to reproduce the problem on two systems:

Quoted 7 lines
Quoted 5 lines

Linux 4.14.74-28+yc11.91

and

Linux ubuntu 4.15.0-106-generic

Both of those are distro-specific kernels, can you reproduce this on 5.8-rc2 or 5.7 as released from kernel.org?

Quoted 7 lines

Alexey,

A shell script containing the keyctl command chain with some output, and then your version of the output when running the script would be also very useful for better comparison.

/Jarkko

Ok. I have the following script:

----------------------------------------------------------------------------
#!/usr/bin/sh

Quoted 7 lines

uname -r

KEYID=$(keyctl add user john smith @u)
keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

pam_keyinit.so should create user keyring when the login session is created. If the user space stack is working correclty, you should not end up to be the possessor for the user keyring.

However, I can simulate your environment with the session keyring:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x3d000000
keyctl describe $KEYID
keyctl print $KEYID

And yes I do get:

564302411: alswrv-----v------------  1000  1000 user: john
564302411: alsw-v------------------  1000  1000 user: john

smith

Here's another sequence that also removes setattr:

KEYID=keyctl add user john smith @s

keyctl describe $KEYID
keyctl setperm $KEYID 0x08000000
keyctl describe $KEYID
keyctl print $KEYID

700153280: alswrv-----v------------ 1000 1000 user: john
keyctl_describe_alloc: Permission denied
smith

Quoted 5 lines

David, this look at least with a quick sight somewhat weird: my possessor permissions are only "search", so why does reading the payload succeed?

/Jarkko

ping

Signature · 3 lines
With best regards,
Alexey Krasikov,
+7-999-789-0-789