cred: Do not default to init_cred in prepare_kernel_cred()
Subsystems (9)
PATCH 1/1 cred: Do not default to init_cred in prepare_kernel_cred()
A common exploit pattern for ROP attacks is to abuse prepare_kernel_cred() in order to construct escalated privileges[1]. Instead of providing a short-hand argument (NULL) to the "daemon" argument to indicate using init_cred as the base cred, require that "daemon" is always set to an actual task. Replace all existing callers that were passing NULL with &init_task.
Future attacks will need to have sufficiently powerful read/write primitives to have found an appropriately privileged task and written it to the ROP stack as an argument to succeed, which is similarly difficult to the prior effort needed to escalate privileges before struct cred
existed: locate the current cred and overwrite the uid member.
This has the added benefit of meaning that prepare_kernel_cred() can no longer exceed the privileges of the init task, which may have changed from the original init_cred (e.g. dropping capabilities from the bounding set).
[1] https://google.com/search?q=commit_creds(prepare_kernel_cred(0))
--- drivers/base/firmware_loader/main.c | 2 +- fs/cifs/cifs_spnego.c | 2 +- fs/cifs/cifsacl.c | 2 +- fs/ksmbd/smb_common.c | 2 +- fs/nfs/flexfilelayout/flexfilelayout.c | 4 ++-- fs/nfs/nfs4idmap.c | 2 +- fs/nfsd/nfs4callback.c | 2 +- kernel/cred.c | 15 +++++++-------- net/dns_resolver/dns_key.c | 2 +- 9 files changed, 16 insertions(+), 17 deletions(-)
drivers/base/firmware_loader/main.c +1 -1
@@ -821,7 +821,7 @@ _request_firmware(const struct firmware **firmware_p, const char *name,* called by a driver when serving an unrelated request from userland, we use * the kernel credentials to read the file. */ - kern_cred =prepare_kernel_cred (NULL ); + kern_cred =prepare_kernel_cred (&init_task);if (!kern_cred) { ret = -ENOMEM ; goto out;
fs/cifs/cifs_spnego.c +1 -1
@@ -189,7 +189,7 @@ init_cifs_spnego(void)* spnego upcalls. */ - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;
fs/cifs/cifsacl.c +1 -1
@@ -465,7 +465,7 @@ init_cifs_idmap(void)* this is used to prevent malicious redirections from being installed * with add_key(). */ - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;
fs/ksmbd/smb_common.c +1 -1
@@ -623,7 +623,7 @@ int ksmbd_override_fsids(struct ksmbd_work *work)if (share->force_gid !=KSMBD_SHARE_INVALID_GID ) gid = share->force_gid; - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;
fs/nfs/flexfilelayout/flexfilelayout.c +2 -2
@@ -493,10 +493,10 @@ ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, gid =make_kgid (&init_user_ns, id);if (gfp_flags & __GFP_FS) - kcred =prepare_kernel_cred (NULL ); + kcred =prepare_kernel_cred (&init_task);else {unsigned int nofs_flags =memalloc_nofs_save (); - kcred =prepare_kernel_cred (NULL ); + kcred =prepare_kernel_cred (&init_task);memalloc_nofs_restore (nofs_flags); } rc = -ENOMEM ;
fs/nfs/nfs4idmap.c +1 -1
@@ -203,7 +203,7 @@ int nfs_idmap_init(void)printk (KERN_NOTICE "NFS: Registering the %s key type\n" , key_type_id_resolver.name); - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;
fs/nfsd/nfs4callback.c +1 -1
@@ -870,7 +870,7 @@ static const struct cred *get_backchannel_cred(struct nfs4_client *clp, struct r }else {struct cred *kcred; - kcred =prepare_kernel_cred (NULL ); + kcred =prepare_kernel_cred (&init_task);if (!kcred)return NULL ;
kernel/cred.c +7 -8
@@ -701,9 +701,9 @@ void __init cred_init(void)* override a task's own credentials so that work can be done on behalf of that * task that requires a different subjective context. * -* @daemon is used to provide a base for the security record, but can be NULL. -* If @daemon is supplied, then the security data will be derived from that; -* otherwise they'll be set to 0 and no groups, full capabilities and no keys. +* @daemon is used to provide a base cred, with the security data derived from +* that; if this is "&init_task", they'll be set to 0, no groups, full +* capabilities, and no keys. * * The caller may change these controls afterwards if desired. *
@@ -714,17 +714,16 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)const struct cred *old;struct cred *new; +if (WARN_ON_ONCE (!daemon)) +return NULL ; + new =kmem_cache_alloc (cred_jar,GFP_KERNEL );if (!new)return NULL ;kdebug ("prepare_kernel_cred() alloc %p" , new); -if (daemon) - old =get_task_cred (daemon); -else - old =get_cred (&init_cred); - + old =get_task_cred (daemon);validate_creds (old); *new = *old;
net/dns_resolver/dns_key.c +1 -1
@@ -337,7 +337,7 @@ static int __init init_dns_resolver(void)* this is used to prevent malicious redirections from being installed * with add_key(). */ - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;
Signature · 1 line
2.34.1
Quoted 39 lines · Kees Cook
A common exploit pattern for ROP attacks is to abuse
prepare_kernel_cred()in order to construct escalated privileges[1]. Instead of providing a short-hand argument (NULL) to the "daemon" argument to indicate usinginit_credas the base cred, require that "daemon" is always set to an actual task. Replace all existing callers that were passing NULL with&init_task.Future attacks will need to have sufficiently powerful read/write primitives to have found an appropriately privileged task and written it to the ROP stack as an argument to succeed, which is similarly difficult to the prior effort needed to escalate privileges before struct cred
existed: locate the current cred and overwrite the uid member.This has the added benefit of meaning that
prepare_kernel_cred()can no longer exceed the privileges of the init task, which may have changed from the originalinit_cred(e.g. dropping capabilities from the bounding set).[1] https://google.com/search?q=commit_creds(prepare_kernel_cred(0))
Cc "Eric W. Biederman" <[email protected]>Cc David Howells <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Russ Weight <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Steve French <[email protected]>Cc Paulo Alcantara <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Namjae Jeon <[email protected]>Cc Sergey Senozhatsky <[email protected]>Cc Trond Myklebust <[email protected]>Cc Anna Schumaker <[email protected]>Cc Chuck Lever <[email protected]>Cc Jeff Layton <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>
Quoted 8 lines
Cc Paolo Abeni <[email protected]>Cc "Michal Koutný" <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Kees Cook <[email protected]>
Quoted 47 lines · Kees Cook
A common exploit pattern for ROP attacks is to abuse
prepare_kernel_cred()in order to construct escalated privileges[1]. Instead of providing a short-hand argument (NULL) to the "daemon" argument to indicate usinginit_credas the base cred, require that "daemon" is always set to an actual task. Replace all existing callers that were passing NULL with&init_task.Future attacks will need to have sufficiently powerful read/write primitives to have found an appropriately privileged task and written it to the ROP stack as an argument to succeed, which is similarly difficult to the prior effort needed to escalate privileges before struct cred
existed: locate the current cred and overwrite the uid member.This has the added benefit of meaning that
prepare_kernel_cred()can no longer exceed the privileges of the init task, which may have changed from the originalinit_cred(e.g. dropping capabilities from the bounding set).[1] https://google.com/search?q=commit_creds(prepare_kernel_cred(0))
Cc "Eric W. Biederman" <[email protected]>Cc David Howells <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Russ Weight <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Steve French <[email protected]>Cc Paulo Alcantara <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Namjae Jeon <[email protected]>Cc Sergey Senozhatsky <[email protected]>Cc Trond Myklebust <[email protected]>Cc Anna Schumaker <[email protected]>Cc Chuck Lever <[email protected]>Cc Jeff Layton <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Cc "Michal Koutný" <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Kees Cook <[email protected]>
Quoted 11 lines
--- drivers/base/firmware_loader/main.c | 2 +- fs/cifs/cifs_spnego.c | 2 +- fs/cifs/cifsacl.c | 2 +- fs/ksmbd/smb_common.c | 2 +- fs/nfs/flexfilelayout/flexfilelayout.c | 4 ++-- fs/nfs/nfs4idmap.c | 2 +- fs/nfsd/nfs4callback.c | 2 +- kernel/cred.c | 15 +++++++-------- net/dns_resolver/dns_key.c | 2 +- 9 files changed, 16 insertions(+), 17 deletions(-)
Quoted 13 lines · Kees Cook
A common exploit pattern for ROP attacks is to abuse
prepare_kernel_cred()in order to construct escalated privileges[1]. Instead of providing a short-hand argument (NULL) to the "daemon" argument to indicate usinginit_credas the base cred, require that "daemon" is always set to an actual task. Replace all existing callers that were passing NULL with&init_task.Future attacks will need to have sufficiently powerful read/write primitives to have found an appropriately privileged task and written it to the ROP stack as an argument to succeed, which is similarly difficult to the prior effort needed to escalate privileges before struct cred
existed: locate the current cred and overwrite the uid member.
Quoted 3 lines
This has the added benefit of meaning that
prepare_kernel_cred()can no longer exceed the privileges of the init task, which may have changed from the originalinit_cred(e.g. dropping capabilities from the bounding set).
Quoted 39 lines · Kees Cook
A common exploit pattern for ROP attacks is to abuse
prepare_kernel_cred()in order to construct escalated privileges[1]. Instead of providing a short-hand argument (NULL) to the "daemon" argument to indicate usinginit_credas the base cred, require that "daemon" is always set to an actual task. Replace all existing callers that were passing NULL with&init_task.Future attacks will need to have sufficiently powerful read/write primitives to have found an appropriately privileged task and written it to the ROP stack as an argument to succeed, which is similarly difficult to the prior effort needed to escalate privileges before struct cred
existed: locate the current cred and overwrite the uid member.This has the added benefit of meaning that
prepare_kernel_cred()can no longer exceed the privileges of the init task, which may have changed from the originalinit_cred(e.g. dropping capabilities from the bounding set).[1] https://google.com/search?q=commit_creds(prepare_kernel_cred(0))
Cc "Eric W. Biederman" <[email protected]>Cc David Howells <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Russ Weight <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Steve French <[email protected]>Cc Paulo Alcantara <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Namjae Jeon <[email protected]>Cc Sergey Senozhatsky <[email protected]>Cc Trond Myklebust <[email protected]>Cc Anna Schumaker <[email protected]>Cc Chuck Lever <[email protected]>Cc Jeff Layton <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>
Quoted 8 lines
Cc Paolo Abeni <[email protected]>Cc "Michal Koutný" <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Kees Cook <[email protected]>
Luis
Quoted 198 lines · Kees Cook
A common exploit pattern for ROP attacks is to abuse
prepare_kernel_cred()in order to construct escalated privileges[1]. Instead of providing a short-hand argument (NULL) to the "daemon" argument to indicate usinginit_credas the base cred, require that "daemon" is always set to an actual task. Replace all existing callers that were passing NULL with&init_task.Future attacks will need to have sufficiently powerful read/write primitives to have found an appropriately privileged task and written it to the ROP stack as an argument to succeed, which is similarly difficult to the prior effort needed to escalate privileges before struct cred
existed: locate the current cred and overwrite the uid member.This has the added benefit of meaning that
prepare_kernel_cred()can no longer exceed the privileges of the init task, which may have changed from the originalinit_cred(e.g. dropping capabilities from the bounding set).[1] https://google.com/search?q=commit_creds(prepare_kernel_cred(0))
Cc "Eric W. Biederman" <[email protected]>Cc David Howells <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Russ Weight <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Steve French <[email protected]>Cc Paulo Alcantara <[email protected]>Cc Ronnie Sahlberg <[email protected]>Cc Shyam Prasad N <[email protected]>Cc Tom Talpey <[email protected]>Cc Namjae Jeon <[email protected]>Cc Sergey Senozhatsky <[email protected]>Cc Trond Myklebust <[email protected]>Cc Anna Schumaker <[email protected]>Cc Chuck Lever <[email protected]>Cc Jeff Layton <[email protected]>Cc "David S. Miller" <[email protected]>Cc Eric Dumazet <[email protected]>Cc Jakub Kicinski <[email protected]>Cc Paolo Abeni <[email protected]>Cc "Michal Koutný" <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Kees Cook <[email protected]>--- drivers/base/firmware_loader/main.c | 2 +- fs/cifs/cifs_spnego.c | 2 +- fs/cifs/cifsacl.c | 2 +- fs/ksmbd/smb_common.c | 2 +- fs/nfs/flexfilelayout/flexfilelayout.c | 4 ++-- fs/nfs/nfs4idmap.c | 2 +- fs/nfsd/nfs4callback.c | 2 +- kernel/cred.c | 15 +++++++-------- net/dns_resolver/dns_key.c | 2 +- 9 files changed, 16 insertions(+), 17 deletions(-)drivers/base/firmware_loader/main.c +1
-1@@ -821,7 +821,7 @@ _request_firmware(const struct firmware **firmware_p, const char *name,* called by a driver when serving an unrelated request from userland, we use * the kernel credentials to read the file. */ - kern_cred =prepare_kernel_cred (NULL ); + kern_cred =prepare_kernel_cred (&init_task);if (!kern_cred) { ret = -ENOMEM ; goto out;fs/cifs/cifs_spnego.c +1
-1@@ -189,7 +189,7 @@ init_cifs_spnego(void)* spnego upcalls. */ - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;fs/cifs/cifsacl.c +1
-1@@ -465,7 +465,7 @@ init_cifs_idmap(void)* this is used to prevent malicious redirections from being installed * with add_key(). */ - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;fs/ksmbd/smb_common.c +1
-1@@ -623,7 +623,7 @@ int ksmbd_override_fsids(struct ksmbd_work *work)if (share->force_gid !=KSMBD_SHARE_INVALID_GID ) gid = share->force_gid; - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;fs/nfs/flexfilelayout/flexfilelayout.c +2
-2@@ -493,10 +493,10 @@ ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, gid =make_kgid (&init_user_ns, id);if (gfp_flags & __GFP_FS) - kcred =prepare_kernel_cred (NULL ); + kcred =prepare_kernel_cred (&init_task);else {unsigned int nofs_flags =memalloc_nofs_save (); - kcred =prepare_kernel_cred (NULL ); + kcred =prepare_kernel_cred (&init_task);memalloc_nofs_restore (nofs_flags); } rc = -ENOMEM ;fs/nfs/nfs4idmap.c +1
-1@@ -203,7 +203,7 @@ int nfs_idmap_init(void)printk (KERN_NOTICE "NFS: Registering the %s key type\n" , key_type_id_resolver.name); - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;fs/nfsd/nfs4callback.c +1
-1@@ -870,7 +870,7 @@ static const struct cred *get_backchannel_cred(struct nfs4_client *clp, struct r }else {struct cred *kcred; - kcred =prepare_kernel_cred (NULL ); + kcred =prepare_kernel_cred (&init_task);if (!kcred)return NULL ;kernel/cred.c +7
-8@@ -701,9 +701,9 @@ void __init cred_init(void)* override a task's own credentials so that work can be done on behalf of that * task that requires a different subjective context. * -* @daemon is used to provide a base for the security record, but can be NULL. -* If @daemon is supplied, then the security data will be derived from that; -* otherwise they'll be set to 0 and no groups, full capabilities and no keys. +* @daemon is used to provide a base cred, with the security data derived from +* that; if this is "&init_task", they'll be set to 0, no groups, full +* capabilities, and no keys. * * The caller may change these controls afterwards if desired. * @@ -714,17 +714,16 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)const struct cred *old;struct cred *new; +if (WARN_ON_ONCE (!daemon)) +return NULL ; + new =kmem_cache_alloc (cred_jar,GFP_KERNEL );if (!new)return NULL ;kdebug ("prepare_kernel_cred() alloc %p" , new); -if (daemon) - old =get_task_cred (daemon); -else - old =get_cred (&init_cred); - + old =get_task_cred (daemon);validate_creds (old); *new = *old;net/dns_resolver/dns_key.c +0
-0
net/dns_resolver/dns_key.c static int __init init_dns_resolver(void) · line 343
Quoted 8 lines
net/dns_resolver/dns_key.c +1
-1@@ -337,7 +337,7 @@ static int __init init_dns_resolver(void)* this is used to prevent malicious redirections from being installed * with add_key(). */ - cred =prepare_kernel_cred (NULL ); + cred =prepare_kernel_cred (&init_task);if (!cred)return -ENOMEM ;
- Russ