diff mbox series

[2/3] libselinux: always set errno on context translation failure

Message ID 20231211150031.127850-2-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit 4c8bf60f441d
Headers show
Series [1/3] libselinux: update const qualifier of parameters in man pages | expand

Commit Message

Christian Göttsche Dec. 11, 2023, 3 p.m. UTC
Allow callers to expect errno is set on failure, e.g. other exported
libselinux functions like setexecfilecon(3).

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/setrans_client.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libselinux/src/setrans_client.c b/libselinux/src/setrans_client.c
index 920f9032..d7dbc0ca 100644
--- a/libselinux/src/setrans_client.c
+++ b/libselinux/src/setrans_client.c
@@ -92,8 +92,10 @@  send_request(int fd, uint32_t function, const char *data1, const char *data2)
 	ssize_t count, expected;
 	unsigned int i;
 
-	if (fd < 0)
+	if (fd < 0) {
+		errno = EINVAL;
 		return -1;
+	}
 
 	if (!data1)
 		data1 = "";
@@ -123,8 +125,12 @@  send_request(int fd, uint32_t function, const char *data1, const char *data2)
 
 	while (((count = sendmsg(fd, &msgh, MSG_NOSIGNAL)) < 0)
 	       && (errno == EINTR)) ;
-	if (count < 0 || count != expected)
+	if (count < 0)
+		return -1;
+	if (count != expected) {
+		errno = EBADMSG;
 		return -1;
+	}
 
 	return 0;
 }
@@ -140,8 +146,10 @@  receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val)
 	struct iovec resp_data;
 	ssize_t count;
 
-	if (fd < 0)
+	if (fd < 0) {
+		errno = EINVAL;
 		return -1;
+	}
 
 	resp_hdr[0].iov_base = &func;
 	resp_hdr[0].iov_len = sizeof(func);
@@ -151,11 +159,17 @@  receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val)
 	resp_hdr[2].iov_len = sizeof(*ret_val);
 
 	while (((count = readv(fd, resp_hdr, 3)) < 0) && (errno == EINTR)) ;
+	if (count < 0) {
+		return -1;
+	}
+
 	if (count != (sizeof(func) + sizeof(data_size) + sizeof(*ret_val))) {
+		errno = EBADMSG;
 		return -1;
 	}
 
 	if (func != function || !data_size || data_size > MAX_DATA_BUF) {
+		errno = EBADMSG;
 		return -1;
 	}
 
@@ -172,6 +186,8 @@  receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val)
 	if (count < 0 || (uint32_t) count != data_size ||
 	    data[data_size - 1] != '\0') {
 		free(data);
+		if (count >= 0)
+			errno = EBADMSG;
 		return -1;
 	}
 	*outdata = data;