libselinux: add getpidprevcon
PATCH 1/1 libselinux: add getpidprevcon
Add the public interfaces getpidprevcon(3) and getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.
--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 9 +++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.c
libselinux/include/selinux/selinux.h +5 -0
@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */
libselinux/man/man3/getcon.3 +9 -0
@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );
@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd with
libselinux/man/man3/getpidprevcon.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/getcon.3
libselinux/man/man3/getpidprevcon_raw.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/getcon.3
libselinux/src/procattr.c +18 -0
@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}
libselinux/utils/.gitignore +1 -0
@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathcon
libselinux/utils/getpidprevcon.c new file +33 -0
@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +}
Signature · 1 line
2.39.0
Stephen Smalley
maintainerQuoted 4 lines · Christian Göttsche
Add the public interfaces getpidprevcon(3) and
getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.
Wondering if we should warn in the manual page for this and other getpid*con() interfaces that they are inherently racy and therefore should never be relied upon for security purposes.
Quoted 152 lines
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 9 +++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.clibselinux/include/selinux/selinux.h +5
-0@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */ libselinux/man/man3/getcon.3 +9
-0@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd withlibselinux/man/man3/getpidprevcon.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/man/man3/getpidprevcon_raw.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/src/procattr.c +18
-0@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}libselinux/utils/.gitignore +1
-0@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathconlibselinux/utils/getpidprevcon.c new file +33
-1@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +} --2.39.0
Quoted 11 lines · Stephen Smalley
Quoted 4 lines · Christian Göttsche
Add the public interfaces getpidprevcon(3) and
getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.Wondering if we should warn in the manual page for this and other getpid*con() interfaces that they are inherently racy and therefore should never be relied upon for security purposes.
With "and other getpid*con() interfaces" do you mean just getpidcon(3) (and getpidcon_raw(3)), which is currently the only interface matching your regex and operating on a foreign process, or also getcon(3) and getprevcon(3)?
With race conditions you mean the possibility of the target process changing its context (via setcon(3)) or querying a replaced process (which is mainly possible via spawning a new process after setting /proc/sys/kernel/ns_last_pid)?
Or do you have others in mind?
p.s.:
after the discussion has settled I'll send a v2 with the interfaces actually published in libselinux.map
Quoted 152 lines
Quoted 152 lines
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 9 +++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.clibselinux/include/selinux/selinux.h +5
-0@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */ libselinux/man/man3/getcon.3 +9
-0@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd withlibselinux/man/man3/getpidprevcon.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/man/man3/getpidprevcon_raw.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/src/procattr.c +18
-0@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}libselinux/utils/.gitignore +1
-0@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathconlibselinux/utils/getpidprevcon.c new file +33
-1@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +} --2.39.0
Stephen Smalley
maintainerQuoted 20 lines · Christian Göttsche
Quoted 11 lines · Stephen Smalley
Quoted 4 lines · Christian Göttsche
Add the public interfaces getpidprevcon(3) and
getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.Wondering if we should warn in the manual page for this and other getpid*con() interfaces that they are inherently racy and therefore should never be relied upon for security purposes.
With "and other getpid*con() interfaces" do you mean just getpidcon(3) (and
getpidcon_raw(3)), which is currently the only interface matching your regex and operating on a foreign process, or also getcon(3) and getprevcon(3)?
Quoted 5 lines
With race conditions you mean the possibility of the target process changing its context (via setcon(3)) or querying a replaced process (which is mainly possible via spawning a new process after setting
/proc/sys/kernel/ns_last_pid)?
Or do you have others in mind?
Yes, just getpidcon/getpidcon_raw. And correct - the inherent race is with respect to looking up a target process by pid,
when said process may potentially switch its context via setcon or execve or exit and be replaced by a process of another context. This was actually successfully done in Android against a service that was using getpidcon() to obtain the context of the binder client before support for passing contexts across binder IPC was added.
Quoted 157 lines
p.s.:
after the discussion has settled I'll send a v2 with the interfaces actually published in libselinux.mapQuoted 152 lines
Quoted 152 lines
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 9 +++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.clibselinux/include/selinux/selinux.h +5
-0@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */ libselinux/man/man3/getcon.3 +9
-0@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd withlibselinux/man/man3/getpidprevcon.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/man/man3/getpidprevcon_raw.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/src/procattr.c +18
-0@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}libselinux/utils/.gitignore +1
-0@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathconlibselinux/utils/getpidprevcon.c new file +33
-1@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +} --2.39.0
Stephen Smalley
maintainerQuoted 29 lines · Stephen Smalley
Quoted 20 lines · Christian Göttsche
Quoted 11 lines · Stephen Smalley
Quoted 4 lines · Christian Göttsche
Add the public interfaces getpidprevcon(3) and
getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.Wondering if we should warn in the manual page for this and other getpid*con() interfaces that they are inherently racy and therefore should never be relied upon for security purposes.
With "and other getpid*con() interfaces" do you mean just getpidcon(3) (and
getpidcon_raw(3)), which is currently the only interface matching your regex and operating on a foreign process, or also getcon(3) and getprevcon(3)?Quoted 5 lines
With race conditions you mean the possibility of the target process changing its context (via setcon(3)) or querying a replaced process (which is mainly possible via spawning a new process after setting
/proc/sys/kernel/ns_last_pid)?
Or do you have others in mind?
Quoted 7 lines
Yes, just getpidcon/getpidcon_raw. And correct - the inherent race is with respect to looking up a target process by pid,
when said process may potentially switch its context via setcon or execve or exit and be replaced by a process of another context. This was actually successfully done in Android against a service that was usinggetpidcon()to obtain the context of the binder client before support for passing contexts across binder IPC was added.
See https://bugs.chromium.org/p/project-zero/issues/detail?id=1741
Quoted 159 lines
Quoted 159 lines
p.s.:
after the discussion has settled I'll send a v2 with the interfaces actually published in libselinux.mapQuoted 152 lines
Quoted 152 lines
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 9 +++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 7 files changed, 68 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.clibselinux/include/selinux/selinux.h +5
-0@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */ libselinux/man/man3/getcon.3 +9
-0@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd withlibselinux/man/man3/getpidprevcon.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/man/man3/getpidprevcon_raw.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/src/procattr.c +18
-0@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}libselinux/utils/.gitignore +1
-0@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathconlibselinux/utils/getpidprevcon.c new file +33
-1@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +} --2.39.0
PATCH v2 1/2 libselinux: add getpidprevcon
In reply to Christian GöttscheAdd the public interfaces getpidprevcon(3) and getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.
---
v2:
added new interfaces to libselinux.map
--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 10 ++++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/libselinux.map | 6 +++++ libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 8 files changed, 75 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.c
libselinux/include/selinux/selinux.h +5 -0
@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */
libselinux/man/man3/getcon.3 +10 -0
@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );
@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd with
@@ -125,6 +134,7 @@ will fail if it is not allowed by policy. .BR getcon_raw (), .BR getprevcon_raw (), .BR getpidcon_raw (), +.BR getpidprevcon_raw (), .BR getpeercon_raw () and .BR setcon_raw ()
libselinux/man/man3/getpidprevcon.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/getcon.3
libselinux/man/man3/getpidprevcon_raw.3 new file +1 -0
@@ -0,0 +1 @@ +.so man3/getcon.3
libselinux/src/libselinux.map +6 -0
@@ -246,3 +246,9 @@ LIBSELINUX_3.4 { selinux_restorecon_get_skipped_errors; selinux_restorecon_parallel; } LIBSELINUX_1.0; + +LIBSELINUX_3.5 { + global: + getpidprevcon; + getpidprevcon_raw; +} LIBSELINUX_3.4;
libselinux/src/procattr.c +18 -0
@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}
libselinux/utils/.gitignore +1 -0
@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathcon
libselinux/utils/getpidprevcon.c new file +33 -0
@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +}
Signature · 1 line
2.39.1
PATCH v2 2/2 libselinux: getcon.3: add note about PID races
Add a note that querying a foreign process via its PID is inherently racy.
--- libselinux/man/man3/getcon.3 | 4 ++++ 1 file changed, 4 insertions(+)
libselinux/man/man3/getcon.3 +4 -0
@@ -149,5 +149,9 @@ The retrieval functions might return success and set .I *context to NULL if and only if SELinux is not enabled. +Querying a foreign process via its PID, e.g. \fBgetpidcon\fR() or +\fBgetpidprevcon\fR(), is inherently racy and therefore should never be relied +upon for security purposes. + .SH "SEE ALSO" .BR selinux "(8), " setexeccon "(3)"
Signature · 1 line
2.39.1
Re: [PATCH v2 2/2] libselinux: getcon.3: add note about PID races
Quoted 5 lines · Christian Göttsche
Add a note that querying a foreign process via its PID is inherently racy.
Suggested-by Stephen Smalley <[email protected]>Signed-off-by Christian Göttsche <[email protected]>
And applied, thanks!
Quoted 21 lines
--- libselinux/man/man3/getcon.3 | 4 ++++ 1 file changed, 4 insertions(+)libselinux/man/man3/getcon.3 +4
-0@@ -149,5 +149,9 @@ The retrieval functions might return success and set .I *context to NULL if and only if SELinux is not enabled. +Querying a foreign process via its PID, e.g. \fBgetpidcon\fR() or +\fBgetpidprevcon\fR(), is inherently racy and therefore should never be relied +upon for security purposes. + .SH "SEE ALSO" .BR selinux "(8), " setexeccon "(3)"Signature · 1 line
2.39.1
Quoted 5 lines · Christian Göttsche
Add the public interfaces getpidprevcon(3) and
getpidprevcon_raw(3), and the utility getpidprevcon to gather the previous context before the last exec of a given process.Signed-off-by Christian Göttsche <[email protected]>
And applied, thanks!
Quoted 176 lines
---
v2:
added new interfaces to libselinux.map--- libselinux/include/selinux/selinux.h | 5 ++++ libselinux/man/man3/getcon.3 | 10 ++++++++ libselinux/man/man3/getpidprevcon.3 | 1 + libselinux/man/man3/getpidprevcon_raw.3 | 1 + libselinux/src/libselinux.map | 6 +++++ libselinux/src/procattr.c | 18 ++++++++++++++ libselinux/utils/.gitignore | 1 + libselinux/utils/getpidprevcon.c | 33 +++++++++++++++++++++++++ 8 files changed, 75 insertions(+) create mode 100644 libselinux/man/man3/getpidprevcon.3 create mode 100644 libselinux/man/man3/getpidprevcon_raw.3 create mode 100644 libselinux/utils/getpidprevcon.clibselinux/include/selinux/selinux.h +5
-0@@ -54,6 +54,11 @@ extern int getpidcon_raw(pid_t pid, char ** con);extern int getprevcon (char ** con);extern int getprevcon_raw (char ** con); +/* Get previous context (prior to last exec) of process identified by pid, and +set *con to refer to it. Caller must free via freecon. */ +extern int getpidprevcon (pid_t pid,char ** con); +extern int getpidprevcon_raw (pid_t pid,char ** con); +/* Get exec context, and set *con to refer to it. Sets *con to NULL if no exec context has been set, i.e. using default. If non-NULL, caller must free via freecon. */ libselinux/man/man3/getcon.3 +10
-0@@ -23,6 +23,10 @@ setcon \- set current security context of a process .sp .BI "int getpidcon_raw(pid_t " pid ", char **" context ); .sp +.BI "int getpidprevcon(pid_t " pid ", char **" context ); +.sp +.BI "int getpidprevcon_raw(pid_t " pid ", char **" context ); +.sp .BI "int getpeercon(int " fd ", char **" context ); .sp .BI "int getpeercon_raw(int " fd ", char **" context );@@ -50,6 +54,11 @@ same as getcon but gets the context before the last exec. returns the process context for the specified PID, which must be free'd with .BR freecon (). +.TP +.BR getpidprevcon () +returns the process context before the last exec for the specified PID, which must be free'd with +.BR freecon (). + .TP .BR getpeercon () retrieves the context of the peer socket, which must be free'd with@@ -125,6 +134,7 @@ will fail if it is not allowed by policy. .BR getcon_raw (), .BR getprevcon_raw (), .BR getpidcon_raw (), +.BR getpidprevcon_raw (), .BR getpeercon_raw () and .BR setcon_raw ()libselinux/man/man3/getpidprevcon.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/man/man3/getpidprevcon_raw.3 new file +1
-0@@ -0,0 +1 @@ +.so man3/getcon.3libselinux/src/libselinux.map +6
-0@@ -246,3 +246,9 @@ LIBSELINUX_3.4 { selinux_restorecon_get_skipped_errors; selinux_restorecon_parallel; } LIBSELINUX_1.0; + +LIBSELINUX_3.5 { + global: + getpidprevcon; + getpidprevcon_raw; +} LIBSELINUX_3.4;libselinux/src/procattr.c +18
-0@@ -300,3 +300,21 @@ int getpidcon(pid_t pid, char **c) }return getprocattrcon (c, pid,"current" ,NULL ); } + +int getpidprevcon_raw (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon_raw (c, pid,"prev" ,NULL ); +} + +int getpidprevcon (pid_t pid,char **c) +{ +if (pid <=0 ) { + errno =EINVAL ; +return -1 ; + } +return getprocattrcon (c, pid,"prev" ,NULL ); +}libselinux/utils/.gitignore +1
-0@@ -9,6 +9,7 @@ getdefaultcon getenforce getfilecon getpidcon +getpidprevcon getsebool getseuser matchpathconlibselinux/utils/getpidprevcon.c new file +33
-0@@ -0,0 +1,33 @@ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <selinux/selinux.h> + +int main (int argc,char **argv) +{ +pid_t pid; +char *buf; +int rc; + +if (argc !=2 ) { +fprintf (stderr,"usage: %s pid\n" , argv[0 ]); +exit (1 ); + } + +if (sscanf (argv[1 ],"%d" , &pid) !=1 ) { +fprintf (stderr,"%s: invalid pid %s\n" , argv[0 ], argv[1 ]); +exit (2 ); + } + + rc =getpidprevcon (pid, &buf); +if (rc <0 ) { +fprintf (stderr,"%s: getpidprevcon() failed: %s\n" , argv[0 ],strerror (errno)); +exit (3 ); + } + +printf ("%s\n" , buf); +freecon (buf); +exit (EXIT_SUCCESS ); +}Signature · 1 line
2.39.1