keys: Pin request_key_auth payload in instantiate paths
Subsystems (2)
PATCH 1/1 keys: Pin request_key_auth payload in instantiate paths
keyctl_instantiate_key_common() reads request_key_auth from the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.
A request-key helper reproducer can trigger this race. One helper child blocks in KEYCTL_INSTANTIATE_IOV while the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stale request_key_auth payload in keyctl_instantiate_key_common().
Give request_key_auth payloads a refcount. Take a payload reference while authkey->sem stabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.
--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)
include/keys/request_key_auth-type.h +2 -0
@@ -9,12 +9,14 @@#define _KEYS_REQUEST_KEY_AUTH_TYPE_H#include <linux/key.h> +#include <linux/refcount.h> /* * Authorisation record for request_key(). */ struct request_key_auth {struct rcu_head rcu; +refcount_t usage;struct key *target_key;struct key *dest_keyring;const struct cred *cred;
security/keys/internal.h +2 -0
@@ -208,6 +208,8 @@ extern struct key *request_key_auth_new(struct key *target,const void *callout_info, size_t callout_len, struct key *dest_keyring); +struct request_key_auth *request_key_auth_get (struct key *authkey); +void request_key_auth_put (struct request_key_auth *rka);extern struct key *key_get_instantiation_authkey (key_serial_t target_id);
security/keys/keyctl.c +18 -6
@@ -1197,9 +1197,13 @@ static long keyctl_instantiate_key_common(key_serial_t id,if (!instkey) goto error; - rka = instkey->payload.data[0 ]; -if (rka->target_key->serial != id) + rka =request_key_auth_get (instkey); +if (!rka) { + ret = -EKEYREVOKED ; goto error; + } +if (rka->target_key->serial != id) + goto error_put_rka;/* pull the payload in if one was supplied */ payload =NULL ;
@@ -1208,7 +1212,7 @@ static long keyctl_instantiate_key_common(key_serial_t id, ret = -ENOMEM ; payload =kvmalloc (plen,GFP_KERNEL );if (!payload) - goto error; + goto error_put_rka; ret = -EFAULT ;if (!copy_from_iter_full (payload, plen, from))
@@ -1234,6 +1238,8 @@ static long keyctl_instantiate_key_common(key_serial_t id, error2:kvfree_sensitive (payload, plen); +error_put_rka: +request_key_auth_put (rka); error:return ret; }
@@ -1358,15 +1364,19 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,if (!instkey) goto error; - rka = instkey->payload.data[0 ]; -if (rka->target_key->serial != id) + rka =request_key_auth_get (instkey); +if (!rka) { + ret = -EKEYREVOKED ; goto error; + } +if (rka->target_key->serial != id) + goto error_put_rka;/* find the destination keyring if present (which must also be * writable) */ ret =get_instantiation_keyring (ringid, rka, &dest_keyring);if (ret <0 ) - goto error; + goto error_put_rka;/* instantiate the key and link it into a keyring */ ret = key_reject_and_link(rka->target_key, timeout, error,
@@ -1379,6 +1389,8 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,if (ret ==0 )keyctl_change_reqkey_auth (NULL ); +error_put_rka: +request_key_auth_put (rka); error:return ret; }
security/keys/request_key_auth.c +31 -2
@@ -23,6 +23,7 @@ static void request_key_auth_describe(const struct key *, struct seq_file *);static void request_key_auth_revoke (struct key *);static void request_key_auth_destroy (struct key *);static long request_key_auth_read (const struct key *,char *,size_t ); +static void request_key_auth_rcu_disposal (struct rcu_head *);/* * The request-key authorisation key type definition.
@@ -115,6 +116,31 @@ static void free_request_key_auth(struct request_key_auth *rka)kfree (rka); } +/* +* Take a reference to the request-key authorisation payload so callers can +* drop authkey->sem before doing operations that may sleep. +*/ +struct request_key_auth *request_key_auth_get (struct key *authkey) +{ +struct request_key_auth *rka; + +down_read (&authkey->sem); + rka =dereference_key_locked (authkey); +if (rka && !test_bit (KEY_FLAG_REVOKED , &authkey->flags)) +refcount_inc (&rka->usage); +else + rka =NULL ; +up_read (&authkey->sem); + +return rka; +} + +void request_key_auth_put (struct request_key_auth *rka) +{ +if (rka &&refcount_dec_and_test (&rka->usage)) +call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +} +/* * Dispose of the request_key_auth record under RCU conditions */
@@ -136,8 +162,10 @@ static void request_key_auth_revoke(struct key *key)struct request_key_auth *rka =dereference_key_locked (key);kenter ("{%d}" , key->serial); +if (!rka) +return ;rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); }/*
@@ -150,7 +178,7 @@ static void request_key_auth_destroy(struct key *key)kenter ("{%d}" , key->serial);if (rka) {rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); } }
@@ -174,6 +202,7 @@ struct key *request_key_auth_new(struct key *target, const char *op, rka =kzalloc_obj (*rka);if (!rka) goto error; +refcount_set (&rka->usage,1 ); rka->callout_info =kmemdup (callout_info, callout_len,GFP_KERNEL );if (!rka->callout_info) goto error_free_rka;
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerQuoted 203 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whileauthkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)include/keys/request_key_auth-type.h +2
-0@@ -9,12 +9,14 @@#define _KEYS_REQUEST_KEY_AUTH_TYPE_H#include <linux/key.h> +#include <linux/refcount.h> /* * Authorisation record for request_key(). */ struct request_key_auth {struct rcu_head rcu; +refcount_t usage;struct key *target_key;struct key *dest_keyring;const struct cred *cred;security/keys/internal.h +2
-0@@ -208,6 +208,8 @@ extern struct key *request_key_auth_new(struct key *target,const void *callout_info, size_t callout_len, struct key *dest_keyring); +struct request_key_auth *request_key_auth_get (struct key *authkey); +void request_key_auth_put (struct request_key_auth *rka);extern struct key *key_get_instantiation_authkey (key_serial_t target_id);security/keys/keyctl.c +18
-6@@ -1197,9 +1197,13 @@ static long keyctl_instantiate_key_common(key_serial_t id,if (!instkey) goto error; - rka = instkey->payload.data[0 ]; -if (rka->target_key->serial != id) + rka =request_key_auth_get (instkey); +if (!rka) { + ret = -EKEYREVOKED ; goto error; + } +if (rka->target_key->serial != id) + goto error_put_rka;/* pull the payload in if one was supplied */ payload =NULL ;@@ -1208,7 +1212,7 @@ static long keyctl_instantiate_key_common(key_serial_t id, ret = -ENOMEM ; payload =kvmalloc (plen,GFP_KERNEL );if (!payload) - goto error; + goto error_put_rka; ret = -EFAULT ;if (!copy_from_iter_full (payload, plen, from))@@ -1234,6 +1238,8 @@ static long keyctl_instantiate_key_common(key_serial_t id, error2:kvfree_sensitive (payload, plen); +error_put_rka: +request_key_auth_put (rka); error:return ret; }@@ -1358,15 +1364,19 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,if (!instkey) goto error; - rka = instkey->payload.data[0 ]; -if (rka->target_key->serial != id) + rka =request_key_auth_get (instkey); +if (!rka) { + ret = -EKEYREVOKED ; goto error; + } +if (rka->target_key->serial != id) + goto error_put_rka;/* find the destination keyring if present (which must also be * writable) */ ret =get_instantiation_keyring (ringid, rka, &dest_keyring);if (ret <0 ) - goto error; + goto error_put_rka;/* instantiate the key and link it into a keyring */ ret = key_reject_and_link(rka->target_key, timeout, error,@@ -1379,6 +1389,8 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,if (ret ==0 )keyctl_change_reqkey_auth (NULL ); +error_put_rka: +request_key_auth_put (rka); error:return ret; }security/keys/request_key_auth.c +31
-2@@ -23,6 +23,7 @@ static void request_key_auth_describe(const struct key *, struct seq_file *);static void request_key_auth_revoke (struct key *);static void request_key_auth_destroy (struct key *);static long request_key_auth_read (const struct key *,char *,size_t ); +static void request_key_auth_rcu_disposal (struct rcu_head *);/* * The request-key authorisation key type definition. @@ -115,6 +116,31 @@ static void free_request_key_auth(struct request_key_auth *rka)kfree (rka); } +/* +* Take a reference to the request-key authorisation payload so callers can +* drop authkey->sem before doing operations that may sleep. +*/ +struct request_key_auth *request_key_auth_get (struct key *authkey) +{ +struct request_key_auth *rka; + +down_read (&authkey->sem); + rka =dereference_key_locked (authkey); +if (rka && !test_bit (KEY_FLAG_REVOKED , &authkey->flags)) +refcount_inc (&rka->usage); +else + rka =NULL ; +up_read (&authkey->sem); + +return rka; +} + +void request_key_auth_put (struct request_key_auth *rka) +{ +if (rka &&refcount_dec_and_test (&rka->usage)) +call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +} +/* * Dispose of the request_key_auth record under RCU conditions */ @@ -136,8 +162,10 @@ static void request_key_auth_revoke(struct key *key)struct request_key_auth *rka =dereference_key_locked (key);kenter ("{%d}" , key->serial); +if (!rka) +return ;rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); }/* @@ -150,7 +178,7 @@ static void request_key_auth_destroy(struct key *key)kenter ("{%d}" , key->serial);if (rka) {rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); } }@@ -174,6 +202,7 @@ struct key *request_key_auth_new(struct key *target, const char *op, rka =kzalloc_obj (*rka);if (!rka) goto error; +refcount_set (&rka->usage,1 ); rka->callout_info =kmemdup (callout_info, callout_len,GFP_KERNEL );if (!rka->callout_info) goto error_free_rka;Signature · 1 line
2.47.3
I get the approach and for it makes sense but I'd really like to hear David's feedback on this before moving forward.
In the meanwhile, were you able to do a script or similar as a reproducer?
BR, Jarkko
A message that is not in the archive (sent off-list or not yet received): CA+TOyfhe=0Ty-FQDZy-9_LWJ6dgakzyjog5rNfQ2NdCc5X+dFQ@mail.gmail.com
Jarkko Sakkinen
maintainerQuoted 11 lines · eee sss
Hi Jarkko,
Yes, I have a reproducer available as a tar.gz file.
I am not attaching it directly here because I am not sure what the preferred way is to share a tar.gz reproducer in this mail context, especially with mailing lists on Cc. Please let me know whether you would prefer a direct attachment, a private upload location, encrypted transfer, or another channel.
BR,
Shaomin
Drop it to me as private! I actually have now queue of total three reproducers but I'll get to this sometime next week :-) Lots of stuff apparently happening in sec.
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · eee sss
Hi Jarkko,
Yes, I have a reproducer available as a tar.gz file.
I am not attaching it directly here because I am not sure what the preferred way is to share a tar.gz reproducer in this mail context, especially with mailing lists on Cc. Please let me know whether you would prefer a direct attachment, a private upload location, encrypted transfer, or another channel.
Quoted 6 lines
Quoted 2 lines
BR,
ShaominDrop it to me as private! I actually have now queue of total three reproducers but I'll get to this sometime next week :-) Lots of stuff apparently happening in sec.
polite request: it is advicable to use plain text in. It's not just me whining but also sometimes some ML servers simply drop HTML mail i.e. only non-ML recipients will get it. Just something to be aware of that's all.
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.
Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference while
Please, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 7 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>
Quoted 6 lines
--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
That does necessarily mean same as NAK but we need much more clarity here.
You should start off not mentioning 'reproducer'. If it does not exist in public, it does not exist. It's not good for anything here.
Then, put a concurrency scenario with A/B sidecars.
Some comments below.
include/keys/request_key_auth-type.h line 12
Quoted 10 lines
include/keys/request_key_auth-type.h +1
-0@@ -9,12 +9,14 @@#define _KEYS_REQUEST_KEY_AUTH_TYPE_H#include <linux/key.h> +#include <linux/refcount.h>
BTW, did you test compilation w/o adding this? Just potential low-hanging fruit diff to remove.
Quoted 30 lines
include/keys/request_key_auth-type.h +1
-0/* * Authorisation record for request_key(). */ struct request_key_auth {struct rcu_head rcu; +refcount_t usage;struct key *target_key;struct key *dest_keyring;const struct cred *cred;security/keys/internal.h +2
-0@@ -208,6 +208,8 @@ extern struct key *request_key_auth_new(struct key *target,const void *callout_info, size_t callout_len, struct key *dest_keyring); +struct request_key_auth *request_key_auth_get (struct key *authkey); +void request_key_auth_put (struct request_key_auth *rka);extern struct key *key_get_instantiation_authkey (key_serial_t target_id);security/keys/keyctl.c +0
-0@@ -1197,9 +1197,13 @@ static long keyctl_instantiate_key_common(key_serial_t id,if (!instkey) goto error;
security/keys/keyctl.c static long keyctl_instantiate_key_common(key_serial_t id, · line 1206
Quoted 10 lines
security/keys/keyctl.c +6
-2- rka = instkey->payload.data[0 ]; -if (rka->target_key->serial != id) + rka =request_key_auth_get (instkey); +if (!rka) { + ret = -EKEYREVOKED ; goto error; + } +if (rka->target_key->serial != id) + goto error_put_rka;
This label should not be introduced and generally please do everything not add extra fat to diff. This is already fat for a claiming to be bug fix.
Quoted 77 lines
security/keys/keyctl.c +12
-4/* pull the payload in if one was supplied */ payload =NULL ;@@ -1208,7 +1212,7 @@ static long keyctl_instantiate_key_common(key_serial_t id, ret = -ENOMEM ; payload =kvmalloc (plen,GFP_KERNEL );if (!payload) - goto error; + goto error_put_rka; ret = -EFAULT ;if (!copy_from_iter_full (payload, plen, from))@@ -1234,6 +1238,8 @@ static long keyctl_instantiate_key_common(key_serial_t id, error2:kvfree_sensitive (payload, plen); +error_put_rka: +request_key_auth_put (rka); error:return ret; }@@ -1358,15 +1364,19 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,if (!instkey) goto error; - rka = instkey->payload.data[0 ]; -if (rka->target_key->serial != id) + rka =request_key_auth_get (instkey); +if (!rka) { + ret = -EKEYREVOKED ; goto error; + } +if (rka->target_key->serial != id) + goto error_put_rka;/* find the destination keyring if present (which must also be * writable) */ ret =get_instantiation_keyring (ringid, rka, &dest_keyring);if (ret <0 ) - goto error; + goto error_put_rka;/* instantiate the key and link it into a keyring */ ret = key_reject_and_link(rka->target_key, timeout, error,@@ -1379,6 +1389,8 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,if (ret ==0 )keyctl_change_reqkey_auth (NULL ); +error_put_rka: +request_key_auth_put (rka); error:return ret; }security/keys/request_key_auth.c +9
-0@@ -23,6 +23,7 @@ static void request_key_auth_describe(const struct key *, struct seq_file *);static void request_key_auth_revoke (struct key *);static void request_key_auth_destroy (struct key *);static long request_key_auth_read (const struct key *,char *,size_t ); +static void request_key_auth_rcu_disposal (struct rcu_head *);/* * The request-key authorisation key type definition. @@ -115,6 +116,31 @@ static void free_request_key_auth(struct request_key_auth *rka)kfree (rka); } +/* +* Take a reference to the request-key authorisation payload so callers can +* drop authkey->sem before doing operations that may sleep. +*/ +struct request_key_auth *request_key_auth_get (struct key *authkey) +{ +struct request_key_auth *rka; +
security/keys/request_key_auth.c static void free_request_key_auth(struct request_key_auth *rka) · line 136
Quoted 10 lines
security/keys/request_key_auth.c +10
-0+down_read (&authkey->sem); + rka =dereference_key_locked (authkey); +if (rka && !test_bit (KEY_FLAG_REVOKED , &authkey->flags)) +refcount_inc (&rka->usage); +else + rka =NULL ; +up_read (&authkey->sem); + +return rka; +}
Instead add fast-path return here. Then request_key_auth can be called called in keyctl_instantiate_key_common and all that extra cruft in this patch won't be needed.
Quoted 41 lines
security/keys/request_key_auth.c +12
-2+ +void request_key_auth_put (struct request_key_auth *rka) +{ +if (rka &&refcount_dec_and_test (&rka->usage)) +call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +} +/* * Dispose of the request_key_auth record under RCU conditions */ @@ -136,8 +162,10 @@ static void request_key_auth_revoke(struct key *key)struct request_key_auth *rka =dereference_key_locked (key);kenter ("{%d}" , key->serial); +if (!rka) +return ;rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); }/* @@ -150,7 +178,7 @@ static void request_key_auth_destroy(struct key *key)kenter ("{%d}" , key->serial);if (rka) {rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); } }@@ -174,6 +202,7 @@ struct key *request_key_auth_new(struct key *target, const char *op, rka =kzalloc_obj (*rka);if (!rka) goto error; +refcount_set (&rka->usage,1 ); rka->callout_info =kmemdup (callout_info, callout_len,GFP_KERNEL );if (!rka->callout_info) goto error_free_rka;Signature · 1 line
2.47.3
Looking forward for v2. These requests are for being able to make reasonable review for the actual logical change. Now there's that and "decorations".
Thanks!
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 31 lines · Jarkko Sakkinen
Quoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whilePlease, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 7 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>Quoted 6 lines
--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)
Quoted 3 lines
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
Oops, sorry typo. "not that well documented reproducer" :-)
But it is cool we just then need to draw the picture.
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 32 lines · Jarkko Sakkinen
Quoted 31 lines · Jarkko Sakkinen
Quoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whilePlease, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 13 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)
Quoted 7 lines
Quoted 3 lines
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
Oops, sorry typo. "not that well documented reproducer" :-)
But it is cool we just then need to draw the picture.
I think I got this:
A: request_key() B: KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PF
helper completed
detach and free rka
destroy auth key
wake up
use rka->target_key
**USE-AFTER-FREE**So nothing really complicated here, is there?
`
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 55 lines · Jarkko Sakkinen
Quoted 32 lines · Jarkko Sakkinen
Quoted 31 lines · Jarkko Sakkinen
Quoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whilePlease, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 13 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)Quoted 7 lines
Quoted 3 lines
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
Oops, sorry typo. "not that well documented reproducer" :-)
But it is cool we just then need to draw the picture.
I think I got this:
A:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PF
helper completed
detach and free rka
destroy auth key
Quoted 5 lines
wake up use rka->target_key **USE-AFTER-FREE**So nothing really complicated here, is there?
Send v2 with the code changes that I proposed as we want to the change as ergonomic as possible.
Use this as the commit message:
keys: Pin request_key_auth payload in instantiate paths
A: request_key() B: KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PF
helper completed
detach and free rka
destroy auth key
wake up
use rka->target_key
**USE-AFTER-FREE**
Give request_key_auth payloads a refcount. Take a payload reference while authkey->sem stabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.
[jarkko: Replaced the first two paragraphs of text with a concurrency scenario.]
And it includes also the remark at the end.
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 91 lines · Jarkko Sakkinen
Quoted 55 lines · Jarkko Sakkinen
Quoted 32 lines · Jarkko Sakkinen
Quoted 31 lines · Jarkko Sakkinen
Quoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whilePlease, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 13 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)Quoted 7 lines
Quoted 3 lines
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
Oops, sorry typo. "not that well documented reproducer" :-)
But it is cool we just then need to draw the picture.
I think I got this:
A:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PF
helper completed
detach and free rka
destroy auth keyQuoted 5 lines
wake up use rka->target_key **USE-AFTER-FREE**So nothing really complicated here, is there?
Send v2 with the code changes that I proposed as we want to the change as ergonomic as possible.
Use this as the commit message:
keys: Pin
request_key_authpayload in instantiate pathsA:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PFhelper completed
detach and free rka
destroy auth key
wake up
userka->target_key
**USE-AFTER-FREE**Give
request_key_authpayloads a refcount. Take a payload reference whileauthkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.
Quoted 5 lines
[jarkko: Replaced the first two paragraphs of text with a concurrency scenario.]
And it includes also the remark at the end.
BR, Jarkko
Nothing heard so I pushed:
It has the commit message change.
BR, Jarkko
Thanks, Jarkko. Sorry for the delayed response.
I checked the commit in for-next-keys. The updated commit message and the cleanup look good to me.
Best,
Shaomin
Quoted 105 lines · Jarkko Sakkinen
Quoted 91 lines · Jarkko Sakkinen
Quoted 59 lines · Jarkko Sakkinen
Quoted 32 lines · Jarkko Sakkinen
Quoted 31 lines · Jarkko Sakkinen
Quoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whilePlease, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 13 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>---
include/keys/request_key_auth-type.h| 2 ++security/keys/internal.h| 2 ++security/keys/keyctl.c| 24 +++++++++++++++-----security/keys/request_key_auth.c| 33 ++++++++++++++++++++++++++--4 files changed, 53 insertions(+), 8 deletions(-)Quoted 7 lines
Quoted 3 lines
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
Oops, sorry typo. "not that well documented reproducer" :-)
But it is cool we just then need to draw the picture.
I think I got this:
A:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PF
helper completed
detach and free rka
destroy auth key
wake up
userka->target_key
**USE-AFTER-FREE**Quoted 1 line
So nothing really complicated here, is there?
Send v2 with the code changes that I proposed as we want to the change as ergonomic as possible.
Use this as the commit message:
keys: Pin
request_key_authpayload in instantiate pathsA:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PFhelper completed
detach and free rka
destroy auth key
wake up
userka->target_key
**USE-AFTER-FREE**Give
request_key_authpayloads a refcount. Take a payload reference whileauthkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Quoted 5 lines
[jarkko: Replaced the first two paragraphs of text with a concurrency scenario.]
And it includes also the remark at the end.
BR, Jarkko
Nothing heard so I pushed:
It has the commit message change.
BR, Jarkko
Jarkko Sakkinen
maintainerAwesome, no worries, great to hear, and thank you for responding.
And great collaboration :-)
BR, Jarkko
Quoted 114 lines · eee sss
Thanks, Jarkko. Sorry for the delayed response.
I checked the commit in for-next-keys. The updated commit message and the cleanup look good to me.
Best,
ShaominQuoted 105 lines · Jarkko Sakkinen
Quoted 91 lines · Jarkko Sakkinen
Quoted 59 lines · Jarkko Sakkinen
Quoted 32 lines · Jarkko Sakkinen
Quoted 31 lines · Jarkko Sakkinen
Quoted 6 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.Quoted 6 lines
A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whilePlease, name concrete things accurately. I.e. 'usage' in this case. If you have a name, use it instead of obfuscating generalizations.
Quoted 13 lines
authkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>---
include/keys/request_key_auth-type.h| 2 ++security/keys/internal.h| 2 ++security/keys/keyctl.c| 24 +++++++++++++++-----security/keys/request_key_auth.c| 33 ++++++++++++++++++++++++++--4 files changed, 53 insertions(+), 8 deletions(-)Quoted 7 lines
Quoted 3 lines
So first, couple of things.
I'm not going to test not that well documented involving OOT driver.
Oops, sorry typo. "not that well documented reproducer" :-)
But it is cool we just then need to draw the picture.
I think I got this:
A:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PF
helper completed
detach and free rka
destroy auth key
wake up
userka->target_key
**USE-AFTER-FREE**Quoted 1 line
So nothing really complicated here, is there?
Send v2 with the code changes that I proposed as we want to the change as ergonomic as possible.
Use this as the commit message:
keys: Pin
request_key_authpayload in instantiate pathsA:
request_key()B:KEYCTL_INSTANTIATE_IOV
---------------- -------------------------
create auth key
store rka in auth key
wait for helper
get auth key
load rka from auth key
copy user payload
sleep on #PFhelper completed
detach and free rka
destroy auth key
wake up
userka->target_key
**USE-AFTER-FREE**Give
request_key_authpayloads a refcount. Take a payload reference whileauthkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Quoted 5 lines
[jarkko: Replaced the first two paragraphs of text with a concurrency scenario.]
And it includes also the remark at the end.
BR, Jarkko
Nothing heard so I pushed:
It has the commit message change.
BR, Jarkko
Hi Shaomin,
Quoted 45 lines · Shaomin Chen
keyctl_instantiate_key_common()readsrequest_key_authfrom the assumed auth key before copying an instantiation payload from userspace. The copy can fault and sleep. If the request completes and revokes the auth key in that window, the auth payload can be detached and freed before the instantiate path uses it again.A request-key helper reproducer can trigger this race. One helper child blocks in
KEYCTL_INSTANTIATE_IOVwhile the original helper instantiates the requested key and returns. KASAN then reports a use-after-free from the stalerequest_key_authpayload inkeyctl_instantiate_key_common().Give
request_key_authpayloads a refcount. Take a payload reference whileauthkey->semstabilizes the payload and revocation state. Hold that reference across the instantiate and reject paths. Drop the auth key owning reference from revoke and destroy.Reported-by Shaomin Chen <[email protected]>Signed-off-by Shaomin Chen <[email protected]>--- include/keys/request_key_auth-type.h | 2 ++ security/keys/internal.h | 2 ++ security/keys/keyctl.c | 24 +++++++++++++++----- security/keys/request_key_auth.c | 33 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 8 deletions(-)include/keys/request_key_auth-type.h +2
-0@@ -9,12 +9,14 @@#define _KEYS_REQUEST_KEY_AUTH_TYPE_H#include <linux/key.h> +#include <linux/refcount.h> /* * Authorisation record for request_key(). */ struct request_key_auth {struct rcu_head rcu; +refcount_t usage;struct key *target_key;struct key *dest_keyring;const struct cred *cred;
[...snip]
Quoted 9 lines
security/keys/request_key_auth.c +1
-0@@ -23,6 +23,7 @@ static void request_key_auth_describe(const struct key *, struct seq_file *);static void request_key_auth_revoke (struct key *);static void request_key_auth_destroy (struct key *);static long request_key_auth_read (const struct key *,char *,size_t ); +static void request_key_auth_rcu_disposal (struct rcu_head *);
[...snip]
Quoted 7 lines
security/keys/request_key_auth.c +7
-0+ +void request_key_auth_put (struct request_key_auth *rka) +{ +if (rka &&refcount_dec_and_test (&rka->usage)) +call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +} +
[...snip]
Quoted 12 lines
/*
security/keys/request_key_auth.c +1
-1@@ -150,7 +178,7 @@ static void request_key_auth_destroy(struct key *key)kenter ("{%d}" , key->serial);if (rka) {rcu_assign_keypointer (key,NULL ); -call_rcu (&rka->rcu, request_key_auth_rcu_disposal); +request_key_auth_put (rka); } }
security/keys/request_key_auth.c struct key *request_key_auth_new(struct key *target, const char *op, · line 208
Quoted 7 lines
security/keys/request_key_auth.c +1
-0@@ -174,6 +202,7 @@ struct key *request_key_auth_new(struct key *target, const char *op, rka =kzalloc_obj (*rka);if (!rka) goto error; +refcount_set (&rka->usage,1 ); rka->callout_info =kmemdup (callout_info, callout_len,GFP_KERNEL );if (!rka->callout_info) goto error_free_rka;I have a question when looking at the request_key_auth_new() in v7.2 kernel.
I have a question when looking at the error handling path in the request_key_auth_new() code in v7.2 kernel:
struct key *request_key_auth_new(struct key *target, const char *op,
const void *callout_info, size_t callout_len, struct key *dest_keyring)
{
struct request_key_auth *rka, *irka;
[...snip]
refcount_set(&rka->usage, 1); // here set refcount to 1
[...snip]
/* construct the auth key */
ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
if (ret < 0)
goto error_put_authkey;
kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
return authkey;error_put_authkey:
key_put(authkey); // here the key_gc_work will call request_key_auth_destroy()error_free_rka:
free_request_key_auth(rka); // Why is not request_key_auth_put() being used here?
error:
kleave("= %d", ret);
return ERR_PTR(ret);
}The request_key_auth_destroy() also calls free_request_key_auth() finally:
request_key_auth_destroy() -> request_key_auth_put() ->
request_key_auth_rcu_disposal() -> free_request_key_auth()
Direct use free_request_key_auth() in the error_free_rka path may cause double free? Or I missed any information?
Thanks a lot!
Joey Lee