Return error on invalid pids for procattr funcs.
cover Return error on invalid pids for procattr funcs.
From: dcashman <[email protected]>
The getprocattrcon functions currently do not do any validation of the supplied pid argument. This argument is nonsensical for negative pid values and is also not specified for an input value of zero. This is an error-prone convention and should be changed. This is evidenced by issues such as the following android bug:
https://code.google.com/p/android/issues/detail?id=200617
dcashman (2):
libselinux: procattr: return error on invalid pid_t input.
libselinux: procattr: return einval for <= 0 pid args.
libselinux/src/procattr.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.0.rc3.207.g0ac5344
PATCH 1/2 libselinux: procattr: return error on invalid pid_t input.
From: dcashman <[email protected]>
--- libselinux/src/procattr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
libselinux/src/procattr.c +5 -2
@@ -70,9 +70,9 @@ static int openattr(pid_t pid, const char *attr, int flags)char *path;pid_t tid; -if (pid >0 ) +if (pid >0 ) { rc =asprintf (&path,"/proc/%d/attr/%s" , pid, attr); -else { + }else if (pid ==0 ) { rc =asprintf (&path,"/proc/thread-self/attr/%s" , attr);if (rc <0 )return -1 ;
@@ -82,6 +82,9 @@ static int openattr(pid_t pid, const char *attr, int flags)free (path); tid =gettid (); rc =asprintf (&path,"/proc/self/task/%d/attr/%s" , tid, attr); + }else { + errno =EINVAL ; +return -1 ; }if (rc <0 )return -1 ;
Signature · 1 line
2.7.0.rc3.207.g0ac5344
PATCH 2/2 libselinux: procattr: return einval for <= 0 pid args.
From: dcashman <[email protected]>
getpidcon documentation does not specify that a pid of 0 refers to the current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.
--- libselinux/src/procattr.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
libselinux/src/procattr.c +12 -2
@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,#define getpidattr_def (fn, attr) \ int get##fn##_raw(pid_t pid, char **c) \ { \ - return getprocattrcon_raw(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon_raw(c, pid, #attr); \ + } \ } \ int get##fn(pid_t pid, char **c) \ { \ - return getprocattrcon(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon(c, pid, #attr); \ + } \ }all_selfattr_def (con, current)
Signature · 1 line
2.7.0.rc3.207.g0ac5344
Re: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.
Quoted 32 lines · Daniel Cashman
From: dcashman <[email protected]>
getpidcon documentation does not specify that a pid of 0 refers to the current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.
Signed-off-by Daniel Cashman <[email protected]>--- libselinux/src/procattr.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)libselinux/src/procattr.c +6
-1@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,#define getpidattr_def (fn, attr) \ int get##fn##_raw(pid_t pid, char **c) \ { \ - return getprocattrcon_raw(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon_raw(c, pid, #attr); \ + } \ } \ int get##fn(pid_t pid, char **c) \ { \
libselinux/src/procattr.c static int setprocattrcon(const char * context, · line 326
Quoted 10 lines
libselinux/src/procattr.c +6
-1- return getprocattrcon(c, pid, #attr); \ +if (pid <=0 ) { \ + errno =EINVAL ; \ +return -1 ; \ + }else { \ + return getprocattrcon(c, pid, #attr); \ + } \ }all_selfattr_def (con, current)
I need to point out explicitly that this change would, of course, break the existing ABI and result in a change in behavior for applications relying on getpidcon(0,) calls to be the same as getcon().
Thanks,
Dan
Nick Kralevich
Acked-byRe: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.
Thanks for proposing this patch Dan.
As you said, the current API feels error prone, as it has two entirely different behaviors depending on whether the pid is zero or non-zero. Your patch corrects this error prone API and clearly separates out a query by PID vs a query of the process itself.
This patch helps provide robustness against bugs similar to:
https://code.google.com/p/google-security-research/issues/detail?id=727
https://code.google.com/p/android/issues/detail?id=200617
(note that the Android code in question was reviewed by Stephen, myself, and others within Google, and we all missed this particular bug)
I would recommend the upstream SELinux community accept the patch, even at the potential expense of breaking compatibility with other apps.
-- Nick
Quoted 10 lines · Daniel Cashman
Quoted 9 lines · Daniel Cashman
From: dcashman <[email protected]>
getpidcon documentation does not specify that a pid of 0 refers to the current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.
Signed-off-by Daniel Cashman <[email protected]>
Quoted 40 lines
Quoted 23 lines
--- libselinux/src/procattr.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)libselinux/src/procattr.c +6
-1@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,#define getpidattr_def (fn, attr) \ int get##fn##_raw(pid_t pid, char **c) \ { \ - return getprocattrcon_raw(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon_raw(c, pid, #attr); \ + } \ } \ int get##fn(pid_t pid, char **c) \ { \libselinux/src/procattr.c static int setprocattrcon(const char * context, · line 326
Quoted 10 lines
libselinux/src/procattr.c +6
-1- return getprocattrcon(c, pid, #attr); \ +if (pid <=0 ) { \ + errno =EINVAL ; \ +return -1 ; \ + }else { \ + return getprocattrcon(c, pid, #attr); \ + } \ }all_selfattr_def (con, current)I need to point out explicitly that this change would, of course, break the existing ABI and result in a change in behavior for applications relying on
getpidcon(0,)calls to be the same asgetcon().Thanks,
Dan
Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
Stephen Smalley
AppliedRe: [PATCH 1/2] libselinux: procattr: return error on invalid pid_t input.
In reply to Daniel CashmanQuoted 3 lines · Daniel Cashman
From: dcashman <[email protected]>
Signed-off-by Daniel Cashman <[email protected]>
Thanks, applied.
Quoted 31 lines
--- libselinux/src/procattr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)libselinux/src/procattr.c +5
-2@@ -70,9 +70,9 @@ static int openattr(pid_t pid, const char *attr, int flags)char *path;pid_t tid; -if (pid >0 ) +if (pid >0 ) { rc =asprintf (&path,"/proc/%d/attr/%s" , pid, attr); -else { + }else if (pid ==0 ) { rc =asprintf (&path,"/proc/thread-self/attr/%s" , attr);if (rc <0 )return -1 ;@@ -82,6 +82,9 @@ static int openattr(pid_t pid, const char *attr, int flags)free (path); tid =gettid (); rc =asprintf (&path,"/proc/self/task/%d/attr/%s" , tid, attr); + }else { + errno =EINVAL ; +return -1 ; }if (rc <0 )return -1 ;
getpidcon with pid == 0 (Was: Re: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.)
In reply to Daniel CashmanQuoted 7 lines · Daniel Cashman
From: dcashman <[email protected]>
getpidcon documentation does not specify that a pid of 0 refers to the current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.
I'll try to see if there are any legitimate users of getpidcon with pid == 0. If anyone on the list knows of one, please speak up.
Quoted 35 lines
Signed-off-by Daniel Cashman <[email protected]>--- libselinux/src/procattr.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)libselinux/src/procattr.c +12
-2@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,#define getpidattr_def (fn, attr) \ int get##fn##_raw(pid_t pid, char **c) \ { \ - return getprocattrcon_raw(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon_raw(c, pid, #attr); \ + } \ } \ int get##fn(pid_t pid, char **c) \ { \ - return getprocattrcon(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon(c, pid, #attr); \ + } \ }all_selfattr_def (con, current)
Re: getpidcon with pid == 0 (Was: Re: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.)
A quick Google search for "getpidcon(0" shows only the Android bug.
https://www.google.com/webhp#q=%22getpidcon(0%22
-- Nick
Quoted 51 lines · Stephen Smalley
Quoted 8 lines · Daniel Cashman
From: dcashman <[email protected]>
getpidcon documentation does not specify that a pid of 0 refers to the current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.
I'll try to see if there are any legitimate users of getpidcon with pid ==
0. If anyone on the list knows of one, please speak up.Quoted 35 lines
Signed-off-by Daniel Cashman <[email protected]>--- libselinux/src/procattr.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)libselinux/src/procattr.c +12
-2@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,#define getpidattr_def (fn, attr) \ int get##fn##_raw(pid_t pid, char **c) \ { \ - return getprocattrcon_raw(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon_raw(c, pid, #attr); \ + } \ } \ int get##fn(pid_t pid, char **c) \ { \ - return getprocattrcon(c, pid, #attr); \ + if (pid <= 0) { \ + errno = EINVAL; \ + return -1; \ + } else { \ + return getprocattrcon(c, pid, #attr); \ + } \ }all_selfattr_def (con, current)
Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
Re: getpidcon with pid == 0 (Was: Re: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.)
Quoted 5 lines · Nick Kralevich
A quick Google search for "getpidcon(0" shows only the Android bug.
https://www.google.com/webhp#q=%22getpidcon(0%22
-- Nick
Yes, I looked through searchcode.com results for getpidcon (not just with a hardcoded 0 but also looking for any cases where they assume that setting a variable pid == 0 degenerates to getcon behavior), and didn't see anything.
I've also asked the Fedora SELinux maintainers if they know of anything that would break.
Quoted 63 lines
On Wed, Feb 24, 2016 at 6:49 AM, Stephen Smalley <[email protected] <mailto:[email protected]>> wrote:
On 02/23/2016 03:24 PM, Daniel Cashman wrote:
From: dcashman <[email protected] <mailto:[email protected]>>
getpidcon documentation does not specify that a pid of 0 refers to the
current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.I'll try to see if there are any legitimate users of getpidcon with pid == 0. If anyone on the list knows of one, please speak up.
Signed-off-by Daniel Cashman <[email protected]<mailto:[email protected]>>
---
libselinux/src/procattr.c| 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)diff --git
a/libselinux/src/procattr.cb/libselinux/src/procattr.cindex c20f003..eee4612 100644
---a/libselinux/src/procattr.c
+++b/libselinux/src/procattr.c
@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,
#definegetpidattr_def(fn, attr)\
int get##fn##_raw(pid_t pid, char **c) \
{ \
- returngetprocattrcon_raw(c, pid, #attr); \
+ if (pid <= 0) { \
+ errno = EINVAL; \
+ return -1; \
+ } else { \
+ returngetprocattrcon_raw(c, pid, #attr); \
+ } \
} \
int get##fn(pid_t pid, char **c) \
{ \
- returngetprocattrcon(c, pid, #attr); \
+ if (pid <= 0) { \
+ errno = EINVAL; \
+ return -1; \
+ } else { \
+ returngetprocattrcon(c, pid, #attr); \
+ } \
}
all_selfattr_def(con, current)--
Nick Kralevich | Android Security | [email protected] <mailto:[email protected]> | 650.214.4037
Re: getpidcon with pid == 0 (Was: Re: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.)
Quoted 7 lines · Stephen Smalley
Quoted 5 lines · Nick Kralevich
A quick Google search for "getpidcon(0" shows only the Android bug.
https://www.google.com/webhp#q=%22getpidcon(0%22
-- Nick
Quoted 7 lines
Yes, I looked through searchcode.com results for getpidcon (not just with a hardcoded 0 but also looking for any cases where they assume that setting a variable pid == 0 degenerates to getcon behavior), and didn't see anything.
I've also asked the Fedora SELinux maintainers if they know of anything that would break.
Thanks for heads up.
I haven't found such case as well. Nevertheless I resent it to the Fedora development mailing list to make the change more visible.
Thanks,
Petr
Quoted 74 lines
Quoted 58 lines
On Wed, Feb 24, 2016 at 6:49 AM, Stephen Smalley <[email protected] <mailto:[email protected]>> wrote:
On 02/23/2016 03:24 PM, Daniel Cashman wrote:
From: dcashman <[email protected]
<mailto:[email protected]>>getpidcon documentation does not specify that a pid of 0 refers to the
current process, and getcon exists specifically to provide this functionality, and getpidcon(getpid()) would provide it as well. Disallow pid values <= 0 that may lead to unintended behavior in userspace object managers.I'll try to see if there are any legitimate users of getpidcon with pid == 0. If anyone on the list knows of one, please speak up.
Signed-off-by Daniel Cashman <[email protected]<mailto:[email protected]>>
---
libselinux/src/procattr.c| 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)diff --git
a/libselinux/src/procattr.cb/libselinux/src/procattr.c
index c20f003..eee4612 100644
---a/libselinux/src/procattr.c
+++b/libselinux/src/procattr.c
@@ -306,11 +306,21 @@ static int setprocattrcon(const char * context,
#definegetpidattr_def(fn, attr)\
int get##fn##_raw(pid_t pid, char **c) \
{ \
- returngetprocattrcon_raw(c, pid, #attr); \
+ if (pid <= 0) { \
+ errno = EINVAL; \
+ return -1; \
+ } else { \
+ return getprocattrcon_raw(c, pid,
#attr); \
+ } \
} \
int get##fn(pid_t pid, char **c) \
{ \
- returngetprocattrcon(c, pid, #attr); \
+ if (pid <= 0) { \
+ errno = EINVAL; \
+ return -1; \
+ } else { \
+ returngetprocattrcon(c, pid, #attr); \
+ } \
}Quoted 9 lines
all_selfattr_def(con, current)Signature · 2 lines
Nick Kralevich | Android Security | [email protected] <mailto:[email protected]> | 650.214.4037_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].
Re: [PATCH 2/2] libselinux: procattr: return einval for <= 0 pid args.
In reply to Nick KralevichQuoted 12 lines · Nick Kralevich
Thanks for proposing this patch Dan.
As you said, the current API feels error prone, as it has two entirely different behaviors depending on whether the pid is zero or non-zero. Your patch corrects this error prone API and clearly separates out a query by PID vs a query of the process itself.
This patch helps provide robustness against bugs similar to:
https://code.google.com/p/google-security-research/issues/detail?id=727 https://code.google.com/p/android/issues/detail?id=200617
Quoted 7 lines
(note that the Android code in question was reviewed by Stephen, myself, and others within Google, and we all missed this particular bug)
I would recommend the upstream SELinux community accept the patch, even at the potential expense of breaking compatibility with other apps.
As I haven't heard or seen of anything that would break with this change, I've merged this patch. You'll need to apply it separately to Android libselinux since that is still a fork.