selinux: fix a race condition in security_read_policy()
PATCH 1/1 selinux: fix a race condition in security_read_policy()
In security_read_policy(), the policy length is computed using security_policydb_len(), which does a separate transaction, and then another transaction is done to write the policydb into a buffer of this length.
The bug is that the policy might be re-loaded in between the two transactions and so the length can be wrong. In case the new length is lower than the old length, the length is corrected at the end of the function. In case the new length is higher than the old one, an error is returned.
Fix it by doing everything in a single transaction and getting the length directly from policydb instead of calling security_policydb_len().
--- security/selinux/ss/services.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
security/selinux/ss/services.c +11 -8
@@ -3842,22 +3842,25 @@ int security_read_policy(struct selinux_state *state, void **data, size_t *len) { int rc; +struct policydb *policydb;struct policy_file fp;if (!selinux_initialized (state))return -EINVAL ; - *len =security_policydb_len (state); +read_lock (&state->ss->policy_rwlock); + policydb = &state->ss->policy->policydb; + *len = policydb->len; *data =vmalloc_user (*len); -if (!*data) -return -ENOMEM ; - - fp.data = *data; - fp.len = *len; +if (!*data) { + rc = -ENOMEM ; + }else { + fp.data = *data; + fp.len = *len; -read_lock (&state->ss->policy_rwlock); - rc =policydb_write (&state->ss->policy->policydb, &fp); + rc =policydb_write (policydb, &fp); + }read_unlock (&state->ss->policy_rwlock);if (rc)
Signature · 1 line
2.26.2
Stephen Smalley
maintainerQuoted 32 lines · Ondrej Mosnacek
In
security_read_policy(), the policy length is computed usingsecurity_policydb_len(), which does a separate transaction, and then another transaction is done to write the policydb into a buffer of this length.The bug is that the policy might be re-loaded in between the two transactions and so the length can be wrong. In case the new length is lower than the old length, the length is corrected at the end of the function. In case the new length is higher than the old one, an error is returned.
Fix it by doing everything in a single transaction and getting the length directly from policydb instead of calling
security_policydb_len().Fixes cee74f47a6ba ("SELinux: allow userspace to read policy back out of the kernel")Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/services.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)security/selinux/ss/services.c +1
-0@@ -3842,22 +3842,25 @@ int security_read_policy(struct selinux_state *state, void **data, size_t *len) { int rc; +struct policydb *policydb;struct policy_file fp;
security/selinux/ss/services.c int security_read_policy(struct selinux_state *state, · line 3855
Quoted 10 lines
security/selinux/ss/services.c +3
-1if (!selinux_initialized (state))return -EINVAL ; - *len =security_policydb_len (state); +read_lock (&state->ss->policy_rwlock); + policydb = &state->ss->policy->policydb; + *len = policydb->len; *data =vmalloc_user (*len);
I don't believe you can hold a read_lock() across a vmalloc. That's why this is done the way it is now.
Quoted 21 lines
security/selinux/ss/services.c +7
-8-if (!*data) -return -ENOMEM ; - - fp.data = *data; - fp.len = *len; +if (!*data) { + rc = -ENOMEM ; + }else { + fp.data = *data; + fp.len = *len; -read_lock (&state->ss->policy_rwlock); - rc =policydb_write (&state->ss->policy->policydb, &fp); + rc =policydb_write (policydb, &fp); + }read_unlock (&state->ss->policy_rwlock);if (rc) --2.26.2
Quoted 41 lines · Stephen Smalley
Quoted 40 lines · Ondrej Mosnacek
In
security_read_policy(), the policy length is computed usingsecurity_policydb_len(), which does a separate transaction, and then another transaction is done to write the policydb into a buffer of this length.The bug is that the policy might be re-loaded in between the two transactions and so the length can be wrong. In case the new length is lower than the old length, the length is corrected at the end of the function. In case the new length is higher than the old one, an error is returned.
Fix it by doing everything in a single transaction and getting the length directly from policydb instead of calling
security_policydb_len().Fixes cee74f47a6ba ("SELinux: allow userspace to read policy back out of the kernel")Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/services.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)security/selinux/ss/services.c +3
-1@@ -3842,22 +3842,25 @@ int security_read_policy(struct selinux_state *state, void **data, size_t *len) { int rc; +struct policydb *policydb;struct policy_file fp;if (!selinux_initialized (state))return -EINVAL ; - *len =security_policydb_len (state); +read_lock (&state->ss->policy_rwlock); + policydb = &state->ss->policy->policydb;
Quoted 5 lines
security/selinux/ss/services.c int security_read_policy(struct selinux_state *state, · line 3855
Quoted 2 lines
security/selinux/ss/services.c +1
-0+ *len = policydb->len; *data =vmalloc_user (*len);I don't believe you can hold a
read_lock()across a vmalloc. That's why this is done the way it is now.
Fair point. Then I guess the only option is to keep retrying the allocation until the allocated size is >= the size we are about to write. I'll send a revised patch soon.
Quoted 18 lines
Quoted 18 lines
security/selinux/ss/services.c +7
-7-if (!*data) -return -ENOMEM ; - - fp.data = *data; - fp.len = *len; +if (!*data) { + rc = -ENOMEM ; + }else { + fp.data = *data; + fp.len = *len; -read_lock (&state->ss->policy_rwlock); - rc =policydb_write (&state->ss->policy->policydb, &fp); + rc =policydb_write (policydb, &fp); + }read_unlock (&state->ss->policy_rwlock);
Quoted 5 lines
Quoted 4 lines
security/selinux/ss/services.c +0
-1if (rc) --2.26.2
--
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel Red Hat, Inc.
Stephen Smalley
maintainerQuoted 50 lines · Ondrej Mosnacek
Quoted 41 lines · Stephen Smalley
Quoted 40 lines · Ondrej Mosnacek
In
security_read_policy(), the policy length is computed usingsecurity_policydb_len(), which does a separate transaction, and then another transaction is done to write the policydb into a buffer of this length.The bug is that the policy might be re-loaded in between the two transactions and so the length can be wrong. In case the new length is lower than the old length, the length is corrected at the end of the function. In case the new length is higher than the old one, an error is returned.
Fix it by doing everything in a single transaction and getting the length directly from policydb instead of calling
security_policydb_len().Fixes cee74f47a6ba ("SELinux: allow userspace to read policy back out of the kernel")Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/services.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)security/selinux/ss/services.c +3
-1@@ -3842,22 +3842,25 @@ int security_read_policy(struct selinux_state *state, void **data, size_t *len) { int rc; +struct policydb *policydb;struct policy_file fp;if (!selinux_initialized (state))return -EINVAL ; - *len =security_policydb_len (state); +read_lock (&state->ss->policy_rwlock); + policydb = &state->ss->policy->policydb;Quoted 5 lines
security/selinux/ss/services.c int security_read_policy(struct selinux_state *state, · line 3855
Quoted 2 lines
security/selinux/ss/services.c +1
-0+ *len = policydb->len; *data =vmalloc_user (*len);I don't believe you can hold a
read_lock()across a vmalloc. That's why this is done the way it is now.
Quoted 3 lines
Fair point. Then I guess the only option is to keep retrying the allocation until the allocated size is >= the size we are about to write. I'll send a revised patch soon.
Wondering if this is worthwhile/necessary versus just having userspace retry if needed. Reading /sys/fs/selinux/policy is not a common or frequent operation. By the way, if you have CONFIG_DEBUG_ATOMIC_SLEEP enabled, it should catch things like this for you. I have CONFIG_LOCK_DEBUGGING_SUPPORT=yCONFIG_DEBUG_RT_MUTEXES=yCONFIG_DEBUG_SPINLOCK=yCONFIG_DEBUG_MUTEXES=yCONFIG_DEBUG_WW_MUTEX_SLOWPATH=yCONFIG_DEBUG_RWSEMS=yCONFIG_DEBUG_LOCK_ALLOC=y
# CONFIG_DEBUG_LOCKDEP is not set CONFIG_DEBUG_ATOMIC_SLEEP=y
Paul Moore
maintainerQuoted 52 lines · Stephen Smalley
Quoted 50 lines · Ondrej Mosnacek
Quoted 46 lines · Stephen Smalley
Quoted 32 lines · Ondrej Mosnacek
In
security_read_policy(), the policy length is computed usingsecurity_policydb_len(), which does a separate transaction, and then another transaction is done to write the policydb into a buffer of this length.The bug is that the policy might be re-loaded in between the two transactions and so the length can be wrong. In case the new length is lower than the old length, the length is corrected at the end of the function. In case the new length is higher than the old one, an error is returned.
Fix it by doing everything in a single transaction and getting the length directly from policydb instead of calling
security_policydb_len().Fixes cee74f47a6ba ("SELinux: allow userspace to read policy back out of the kernel")Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/services.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)security/selinux/ss/services.c +1
-0@@ -3842,22 +3842,25 @@ int security_read_policy(struct selinux_state *state, void **data, size_t *len) { int rc; +struct policydb *policydb;struct policy_file fp;security/selinux/ss/services.c int security_read_policy(struct selinux_state *state, · line 3855
Quoted 10 lines
security/selinux/ss/services.c +3
-1if (!selinux_initialized (state))return -EINVAL ; - *len =security_policydb_len (state); +read_lock (&state->ss->policy_rwlock); + policydb = &state->ss->policy->policydb; + *len = policydb->len; *data =vmalloc_user (*len);I don't believe you can hold a
read_lock()across a vmalloc. That's why this is done the way it is now.
Quoted 7 lines
Quoted 3 lines
Fair point. Then I guess the only option is to keep retrying the allocation until the allocated size is >= the size we are about to write. I'll send a revised patch soon.
Wondering if this is worthwhile/necessary versus just having userspace retry if needed. Reading
/sys/fs/selinux/policyis not a common or frequent operation.
I tend to agree. If the kernel is under memory pressure, continually hammering it with requests seems like the last thing we want to do; not to mention that we would need to bound the requests at some point so we don't potentially hang userspace (or worse). Failing and letting userspace rety seems like the best option to me.
Signature · 2 lines
paul moore www.paul-moore.com