SELinux Archive mirror
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgzones@googlemail.com>
To: selinux@vger.kernel.org
Subject: [PATCH 2/3] libselinux: always set errno on context translation failure
Date: Mon, 11 Dec 2023 16:00:30 +0100	[thread overview]
Message-ID: <20231211150031.127850-2-cgzones@googlemail.com> (raw)
In-Reply-To: <20231211150031.127850-1-cgzones@googlemail.com>

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 --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;
-- 
2.43.0


  reply	other threads:[~2023-12-11 15:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-11 15:00 [PATCH 1/3] libselinux: update const qualifier of parameters in man pages Christian Göttsche
2023-12-11 15:00 ` Christian Göttsche [this message]
2023-12-11 15:00 ` [PATCH 3/3] libselinux: state setexecfilecon(3) sets errno on failure Christian Göttsche
2023-12-12 14:24 ` [PATCH 1/3] libselinux: update const qualifier of parameters in man pages James Carter
2023-12-12 16:45   ` James Carter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231211150031.127850-2-cgzones@googlemail.com \
    --to=cgzones@googlemail.com \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).