avc_open() and netlink_loop()
When we initialize userspace avc using avc_open(3), it internally calls avc_init(3) without any callback functions. The avc_init() is introduced as a deprecated interface from application code, so it is recommended to use avc_open() instead for new applications.
The avc_init() internally calls avc_netlink_open(). If no thread callback is not given, the 'blocking' argument shall be 0, then avc_netlink_open() set O_NONBLOCK flag on the socket file descriptor.
Next, application will create a thread to receive messages via netlink socket to invalidate userspace avc, using avc_netlink_loop(). However, if userspace avc of libselinux is already initialized, the avc_netlink_loop() immediately returns with EWOULDBLOCK, because the netlink socket is not blocked and avc_netlink_receive() does not expect recvfrom() returns error.
It seems to me O_NONBLOCK is a wrong strategy in this case, and select(2) should be checked in avc_netlink_check_nb() instead.
Eamon, what is your opinion?
Signature · 7 lines
OSS Platform Development Division, NEC KaiGai Kohei <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 16 lines · KaiGai Kohei
When we initialize userspace avc using
avc_open(3), it internally callsavc_init(3)without any callback functions. Theavc_init()is introduced as a deprecated interface from application code, so it is recommended to useavc_open()instead for new applications.The
avc_init()internally callsavc_netlink_open(). If no thread callback is not given, the 'blocking' argument shall be 0, thenavc_netlink_open()setO_NONBLOCKflag on the socket file descriptor.Next, application will create a thread to receive messages via netlink socket to invalidate userspace avc, using
avc_netlink_loop(). However, if userspace avc of libselinux is already initialized, theavc_netlink_loop()immediately returns with EWOULDBLOCK, because the netlink socket is not blocked andavc_netlink_receive()does not expectrecvfrom()returns error.
Quoted 5 lines
It seems to me
O_NONBLOCKis a wrong strategy in this case, and select(2) should be checked inavc_netlink_check_nb()instead.Eamon, what is your opinion?
Hi, my apologies for the delayed response. Yes it appears as though this is a bug. I think the solution is to configure the file descriptor in blocking mode at the start of avc_netlink_loop(). When avc_netlink_loop() is called, we always want blocking behavior. See the attached patch.
Signature · 2 lines
Eamon Walsh National Security Agency
libselinux/src/avc.c +1 -1
@@ -222,7 +222,7 @@ int avc_init(const char *prefix, avc_enforcing = rc; } - rc =avc_netlink_open (avc_using_threads); + rc =avc_netlink_open (0 );if (rc <0 ) { avc_log(SELINUX_ERROR, "%s: can't open netlink socket: %d (%s)\n",
libselinux/src/avc_internal.c +2 -0
@@ -233,6 +233,8 @@ void avc_netlink_loop(void)int rc; char buf[1024] __attribute__ ((aligned)); + fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK) +while (1 ) { errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf));
Quoted 24 lines · Eamon Walsh
Quoted 16 lines · KaiGai Kohei
When we initialize userspace avc using
avc_open(3), it internally callsavc_init(3)without any callback functions. Theavc_init()is introduced as a deprecated interface from application code, so it is recommended to useavc_open()instead for new applications.The
avc_init()internally callsavc_netlink_open(). If no thread callback is not given, the 'blocking' argument shall be 0, thenavc_netlink_open()setO_NONBLOCKflag on the socket file descriptor.Next, application will create a thread to receive messages via netlink socket to invalidate userspace avc, using
avc_netlink_loop(). However, if userspace avc of libselinux is already initialized, theavc_netlink_loop()immediately returns with EWOULDBLOCK, because the netlink socket is not blocked andavc_netlink_receive()does not expectrecvfrom()returns error.Quoted 5 lines
It seems to me
O_NONBLOCKis a wrong strategy in this case, and select(2) should be checked inavc_netlink_check_nb()instead.Eamon, what is your opinion?
Quoted 7 lines
Hi, my apologies for the delayed response. Yes it appears as though this is a bug. I think the solution is to configure the file descriptor in blocking mode at the start of
avc_netlink_loop(). Whenavc_netlink_loop()is called, we always want blocking behavior. See the attached patch.
Also, before you launch a thread to run avc_netlink_loop(), you need to call avc_netlink_acquire_fd(). Otherwise, the avc will internally make calls to avc_netlink_check_nb() which will not work properly.
Signature · 8 lines
Eamon Walsh National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
PATCH 1/1 libselinux: fix avc_netlink_loop() error caused by nonblocking mode.
avc_open() creates the netlink socket in nonblocking mode. If the application later takes control of the netlink socket with avc_netlink_acquire_fd() and then calls avc_netlink_loop(), it will fail with EWOULDBLOCK.
To remedy this, remove the O_NONBLOCK flag from the netlink socket at the start of avc_netlink_loop(). Also, with this fix, there is no need for avc_open() to ever create a blocking socket, so change that and update the man page.
---
man/man3/avc_netlink_loop.3 | 5 +---- src/avc.c | 2 +- src/avc_internal.c | 2 ++ 3 files changed, 4 insertions(+), 5 deletions(-)
libselinux/man/man3/avc_netlink_loop.3 +1 -4
@@ -43,10 +43,7 @@ to take ownership of it in application code. The .I blocking argument specifies whether read operations on the socket will block. .BR avc_open (3) -calls this function internally, specifying non-blocking behavior (unless -threading callbacks were explicitly set using the deprecated -.BR avc_init (3) -interface, in which case blocking behavior is set). +calls this function internally, specifying non-blocking behavior. .B avc_netlink_close closes the netlink socket. This function is called automatically by
libselinux/src/avc.c +1 -1
@@ -222,7 +222,7 @@ int avc_init(const char *prefix, avc_enforcing = rc; } - rc =avc_netlink_open (avc_using_threads); + rc =avc_netlink_open (0 );if (rc <0 ) { avc_log(SELINUX_ERROR, "%s: can't open netlink socket: %d (%s)\n",
libselinux/src/avc_internal.c +2 -0
@@ -233,6 +233,8 @@ void avc_netlink_loop(void)int rc; char buf[1024] __attribute__ ((aligned)); +fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +while (1 ) { errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf));
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Re: [PATCH] libselinux: fix avc_netlink_loop() error caused by nonblocking mode.
Quoted 9 lines · Eamon Walsh
avc_open()creates the netlink socket in nonblocking mode. If the application later takes control of the netlink socket withavc_netlink_acquire_fd()and then callsavc_netlink_loop(), it will fail with EWOULDBLOCK.To remedy this, remove the
O_NONBLOCKflag from the netlink socket at the start ofavc_netlink_loop(). Also, with this fix, there is no need foravc_open()to ever create a blocking socket, so change that and update the man page.
Apart from this design is sane, the libselinux allows applications to call avc_netlink_check_nb() directly.
If we call the function although its worker thread calls avc_netlink_loop(), the invocation of avc_netlink_check_nb() can be blocked, because it assumes the internal netlink socket is non-blocking mode.
(Of course, we will not see the matter in most of sane applications.)
I think a better approach is to put select(2) with zero or infinite timeout just before avc_netlink_receive() calls to check whether the unread message is in the netlink socket, or not.
And the 'blocking' argument of avc_netlink_open() shall be obsoleted, because this design makes nonsense whether the socket has O_NONBLOCK flag, or not.
The point is we should not assume the netlink socket has a certain state when we call avc_netlink_loop() and avc_netlink_check_nb().
What is your opinion?
Quoted 59 lines
Signed-off-by Eamon Walsh<[email protected]>---
man/man3/avc_netlink_loop.3 | 5 +---- src/avc.c | 2 +- src/avc_internal.c | 2 ++ 3 files changed, 4 insertions(+), 5 deletions(-)libselinux/man/man3/avc_netlink_loop.3 +1
-4@@ -43,10 +43,7 @@ to take ownership of it in application code. The .I blocking argument specifies whether read operations on the socket will block. .BR avc_open (3) -calls this function internally, specifying non-blocking behavior (unless -threading callbacks were explicitly set using the deprecated -.BR avc_init (3) -interface, in which case blocking behavior is set). +calls this function internally, specifying non-blocking behavior. .B avc_netlink_close closes the netlink socket. This function is called automatically bylibselinux/src/avc.c +1
-1@@ -222,7 +222,7 @@ int avc_init(const char *prefix, avc_enforcing = rc; } - rc =avc_netlink_open (avc_using_threads); + rc =avc_netlink_open (0 );if (rc<0 ) { avc_log(SELINUX_ERROR, "%s: can't open netlink socket: %d (%s)\n",libselinux/src/avc_internal.c +2
-1@@ -233,6 +233,8 @@ void avc_netlink_loop(void)int rc; char buf[1024] __attribute__ ((aligned)); +fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 )& ~O_NONBLOCK ); +while (1 ) { errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf)); --This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Signature · 6 lines
KaiGai Kohei <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Re: [PATCH] libselinux: fix avc_netlink_loop() error caused by nonblocking mode.
Quoted 19 lines · KaiGai Kohei
Quoted 10 lines · Eamon Walsh
avc_open()creates the netlink socket in nonblocking mode. If the application later takes control of the netlink socket withavc_netlink_acquire_fd()and then callsavc_netlink_loop(), it will fail with EWOULDBLOCK.To remedy this, remove the
O_NONBLOCKflag from the netlink socket at the start ofavc_netlink_loop(). Also, with this fix, there is no need foravc_open()to ever create a blocking socket, so change that and update the man page.Apart from this design is sane, the libselinux allows applications to call
avc_netlink_check_nb()directly.
If we call the function although its worker thread callsavc_netlink_loop(), the invocation ofavc_netlink_check_nb()can be blocked, because it assumes the internal netlink socket is non-blocking mode.
(Of course, we will not see the matter in most of sane applications.)
Quoted 7 lines
I think a better approach is to put select(2) with zero or infinite timeout just before
avc_netlink_receive()calls to check whether the unread message is in the netlink socket, or not.
And the 'blocking' argument ofavc_netlink_open()shall be obsoleted, because this design makes nonsense whether the socket hasO_NONBLOCKflag, or not.
OK I see your point. How about the following:
libselinux: fix avc_netlink_loop() error caused by nonblocking mode.
avc_open() creates the netlink socket in nonblocking mode. If the application later takes control of the netlink socket with avc_netlink_acquire_fd() and then calls avc_netlink_loop(), it will fail with EWOULDBLOCK.
To remedy this, remove the O_NONBLOCK flag from the netlink socket at the start of avc_netlink_loop(). Also, with this fix, there is no need for avc_open() to ever create a blocking socket, so change that and update the man page.
-v2: use poll() in avc_netlink_check_nb(). This makes both avc_netlink_loop() and avc_netlink_check_nb() independent of the O_NONBLOCK flag.
--
man/man3/avc_netlink_loop.3 | 11 +++-------- src/avc.c | 2 +- src/avc_internal.c | 13 +++++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-)
libselinux/man/man3/avc_netlink_loop.3 +3 -8
@@ -41,12 +41,9 @@ descriptor is stored internally; use .BR avc_netlink_acquire_fd (3) to take ownership of it in application code. The .I blocking -argument specifies whether read operations on the socket will block. +argument controls whether the O_NONBLOCK flag is set on the socket descriptor. .BR avc_open (3) -calls this function internally, specifying non-blocking behavior (unless -threading callbacks were explicitly set using the deprecated -.BR avc_init (3) -interface, in which case blocking behavior is set). +calls this function internally, specifying non-blocking behavior. .B avc_netlink_close closes the netlink socket. This function is called automatically by
@@ -66,9 +63,7 @@ checks the netlink socket for pending messages and processes them. Callbacks for policyload and enforcing changes will be called; see .BR selinux_set_callback (3). -This function does not block unless -.BR avc_netlink_open (3) -specified blocking behavior. +This function does not block. .B avc_netlink_loop enters a loop blocking on the netlink socket and processing messages as they
libselinux/src/avc.c +1 -1
@@ -222,7 +222,7 @@ int avc_init(const char *prefix, avc_enforcing = rc; } - rc =avc_netlink_open (avc_using_threads); + rc =avc_netlink_open (0 );if (rc <0 ) { avc_log(SELINUX_ERROR, "%s: can't open netlink socket: %d (%s)\n",
libselinux/src/avc_internal.c +13 -0
@@ -15,6 +15,7 @@#include <unistd.h> #include <fcntl.h> #include <string.h> +#include <poll.h> #include <sys/types.h> #include <sys/socket.h> #include <linux/types.h>
@@ -205,8 +206,18 @@ int avc_netlink_check_nb(void) {int rc; char buf[1024] __attribute__ ((aligned)); +struct pollfd pfd = { fd,POLLIN |POLLPRI ,0 };while (1 ) { + rc =poll (&pfd,1 ,0 ); +if (rc ==0 ) +return 0 ; +if (rc <0 ) { +avc_log (SELINUX_ERROR ,"%s: netlink poll: error %d\n" , + avc_prefix, errno); +return rc; + } + errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf));if (rc <0 ) {
@@ -233,6 +244,8 @@ void avc_netlink_loop(void)int rc; char buf[1024] __attribute__ ((aligned)); +fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 ) & ~O_NONBLOCK ); +while (1 ) { errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf));
Signature · 8 lines
Eamon Walsh National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Re: [PATCH] libselinux: fix avc_netlink_loop() error caused by nonblocking mode.
Quoted 37 lines · Eamon Walsh
Quoted 19 lines · KaiGai Kohei
Quoted 10 lines · Eamon Walsh
avc_open()creates the netlink socket in nonblocking mode. If the application later takes control of the netlink socket withavc_netlink_acquire_fd()and then callsavc_netlink_loop(), it will fail with EWOULDBLOCK.To remedy this, remove the
O_NONBLOCKflag from the netlink socket at the start ofavc_netlink_loop(). Also, with this fix, there is no need foravc_open()to ever create a blocking socket, so change that and update the man page.Apart from this design is sane, the libselinux allows applications to call
avc_netlink_check_nb()directly.
If we call the function although its worker thread callsavc_netlink_loop(), the invocation ofavc_netlink_check_nb()can be blocked, because it assumes the internal netlink socket is non-blocking mode.
(Of course, we will not see the matter in most of sane applications.)Quoted 7 lines
I think a better approach is to put select(2) with zero or infinite timeout just before
avc_netlink_receive()calls to check whether the unread message is in the netlink socket, or not.
And the 'blocking' argument ofavc_netlink_open()shall be obsoleted, because this design makes nonsense whether the socket hasO_NONBLOCKflag, or not.OK I see your point. How about the following:
libselinux: fix
avc_netlink_loop()error caused by nonblocking mode.
avc_open()creates the netlink socket in nonblocking mode. If the application later takes control of the netlink socket withavc_netlink_acquire_fd()and then callsavc_netlink_loop(), it will fail with EWOULDBLOCK.
Quoted 8 lines
To remedy this, remove the
O_NONBLOCKflag from the netlink socket at the start ofavc_netlink_loop(). Also, with this fix, there is no need foravc_open()to ever create a blocking socket, so change that and update the man page.-v2: use
poll()inavc_netlink_check_nb(). This makes bothavc_netlink_loop()andavc_netlink_check_nb()independent of theO_NONBLOCKflag.
It seems to me ok basically.
It is just my preference. Can you move the poll(2) at the head of the avc_netlink_receive() call, like as the attachment? How should I say...it seems to me itchy to change non-blocking mode into blocking mode implicitly at avc_netlink_loop(), although we can specify the mode at creation time (avc_netlink_open()).
Thanks,
Quoted 95 lines
Signed-off-by Eamon Walsh<[email protected]>--
man/man3/avc_netlink_loop.3 | 11 +++-------- src/avc.c | 2 +- src/avc_internal.c | 13 +++++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-)libselinux/man/man3/avc_netlink_loop.3 +3
-8@@ -41,12 +41,9 @@ descriptor is stored internally; use .BR avc_netlink_acquire_fd (3) to take ownership of it in application code. The .I blocking -argument specifies whether read operations on the socket will block. +argument controls whether the O_NONBLOCK flag is set on the socket descriptor. .BR avc_open (3) -calls this function internally, specifying non-blocking behavior (unless -threading callbacks were explicitly set using the deprecated -.BR avc_init (3) -interface, in which case blocking behavior is set). +calls this function internally, specifying non-blocking behavior. .B avc_netlink_close closes the netlink socket. This function is called automatically by@@ -66,9 +63,7 @@ checks the netlink socket for pending messages and processes them. Callbacks for policyload and enforcing changes will be called; see .BR selinux_set_callback (3). -This function does not block unless -.BR avc_netlink_open (3) -specified blocking behavior. +This function does not block. .B avc_netlink_loop enters a loop blocking on the netlink socket and processing messages as theylibselinux/src/avc.c +1
-1@@ -222,7 +222,7 @@ int avc_init(const char *prefix, avc_enforcing = rc; } - rc =avc_netlink_open (avc_using_threads); + rc =avc_netlink_open (0 );if (rc<0 ) { avc_log(SELINUX_ERROR, "%s: can't open netlink socket: %d (%s)\n",libselinux/src/avc_internal.c +13
-0@@ -15,6 +15,7 @@#include <unistd.h> #include <fcntl.h> #include <string.h> +#include <poll.h> #include <sys/types.h> #include <sys/socket.h> #include <linux/types.h> @@ -205,8 +206,18 @@ int avc_netlink_check_nb(void) {int rc; char buf[1024] __attribute__ ((aligned)); +struct pollfd pfd = { fd,POLLIN |POLLPRI ,0 };while (1 ) { + rc =poll (&pfd,1 ,0 ); +if (rc ==0 ) +return 0 ; +if (rc<0 ) { +avc_log (SELINUX_ERROR ,"%s: netlink poll: error %d\n" , + avc_prefix, errno); +return rc; + } + errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf));if (rc<0 ) {@@ -233,6 +244,8 @@ void avc_netlink_loop(void)int rc; char buf[1024] __attribute__ ((aligned)); +fcntl (fd,F_SETFL ,fcntl (fd,F_GETFL ,0 )& ~O_NONBLOCK ); +while (1 ) { errno =0 ; rc =avc_netlink_receive (buf,sizeof (buf));
Signature · 1 line
KaiGai Kohei <[email protected]>
libselinux/man/man3/avc_netlink_loop.3 | 11 +++-------- libselinux/src/avc.c | 2 +- libselinux/src/avc_internal.c | 20 +++++++++++++++++--- 3 files changed, 21 insertions(+), 12 deletions(-)
libselinux/man/man3/avc_netlink_loop.3 +3 -8
@@ -41,12 +41,9 @@ descriptor is stored internally; use .BR avc_netlink_acquire_fd (3) to take ownership of it in application code. The .I blocking -argument specifies whether read operations on the socket will block. +argument controls whether the O_NONBLOCK flag is set on the socket descriptor. .BR avc_open (3) -calls this function internally, specifying non-blocking behavior (unless -threading callbacks were explicitly set using the deprecated -.BR avc_init (3) -interface, in which case blocking behavior is set). +calls this function internally, specifying non-blocking behavior. .B avc_netlink_close closes the netlink socket. This function is called automatically by
@@ -66,9 +63,7 @@ checks the netlink socket for pending messages and processes them. Callbacks for policyload and enforcing changes will be called; see .BR selinux_set_callback (3). -This function does not block unless -.BR avc_netlink_open (3) -specified blocking behavior. +This function does not block. .B avc_netlink_loop enters a loop blocking on the netlink socket and processing messages as they
libselinux/src/avc.c +1 -1
@@ -222,7 +222,7 @@ int avc_init(const char *prefix, avc_enforcing = rc; } - rc =avc_netlink_open (avc_using_threads); + rc =avc_netlink_open (0 );if (rc <0 ) { avc_log(SELINUX_ERROR, "%s: can't open netlink socket: %d (%s)\n",
libselinux/src/avc_internal.c +17 -3
@@ -15,6 +15,7 @@#include <unistd.h> #include <fcntl.h> #include <string.h> +#include <poll.h> #include <sys/types.h> #include <sys/socket.h> #include <linux/types.h>
@@ -92,13 +93,26 @@ void avc_netlink_close(void)close (fd); } -static int avc_netlink_receive (char *buf,unsigned buflen) +static int avc_netlink_receive (char *buf,unsigned buflen,int blocking) {int rc; +struct pollfd pfd = { fd,POLLIN |POLLPRI ,0 };struct sockaddr_nl nladdr;socklen_t nladdrlen =sizeof nladdr;struct nlmsghdr *nlh = (struct nlmsghdr *)buf; + rc =poll (&pfd,1 , (blocking ?-1 :0 )); + +if (rc ==0 && !blocking) { + errno =EWOULDBLOCK ; +return -1 ; + } +else if (rc <1 ) { +avc_log (SELINUX_ERROR ,"%s: netlink poll: error %d\n" , + avc_prefix, errno); +return rc; + } + rc =recvfrom (fd, buf, buflen,0 , (struct sockaddr *)&nladdr, &nladdrlen);if (rc <0 )
@@ -208,7 +222,7 @@ int avc_netlink_check_nb(void)while (1 ) { errno =0 ; - rc =avc_netlink_receive (buf,sizeof (buf)); + rc =avc_netlink_receive (buf,sizeof (buf),0 );if (rc <0 ) {if (errno ==EWOULDBLOCK )return 0 ;
@@ -235,7 +249,7 @@ void avc_netlink_loop(void)while (1 ) { errno =0 ; - rc =avc_netlink_receive (buf,sizeof (buf)); + rc =avc_netlink_receive (buf,sizeof (buf),1 );if (rc <0 ) {if (errno ==0 || errno ==EINTR )continue ;
Re: [PATCH] libselinux: fix avc_netlink_loop() error caused by nonblocking mode.
Quoted 11 lines · KaiGai Kohei
It seems to me ok basically.
It is just my preference. Can you move the poll(2) at the head of the
avc_netlink_receive()call, like as the attachment? How should I say...it seems to me itchy to change non-blocking mode into blocking mode implicitly atavc_netlink_loop(), although we can specify the mode at creation time (avc_netlink_open()).Thanks,
I have pushed this patch, thanks.
Signature · 8 lines
Eamon Walsh National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.