From cdda6ba57bbec8aa4b554230f4d3bea2511a273c Mon Sep 17 00:00:00 2001 From: John Johansen Date: Wed, 3 Jun 2020 01:07:26 -0700 Subject: [PATCH] libapparmor: fix api of aa_getpeercon_raw to use unsigned len param The len parameter returns a value that correlates to a getsockopt parameter which is typed to socklen_t which is an unsigned int. This technically changes the fn() api but old code using this is already broken if the getsockopt parameter is large enough to overflow the value. In reality what is returned shouldn't ever be negative and the value should never be large enough to trip the overflow. This is just cleaning up a corner case. MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/561 Signed-off-by: John Johansen Acked-by: Steve Beattie --- libraries/libapparmor/include/sys/apparmor.h | 3 ++- libraries/libapparmor/src/kernel.c | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/libapparmor/include/sys/apparmor.h b/libraries/libapparmor/include/sys/apparmor.h index 05cc70095..0a38a5dab 100644 --- a/libraries/libapparmor/include/sys/apparmor.h +++ b/libraries/libapparmor/include/sys/apparmor.h @@ -20,6 +20,7 @@ #include #include +#include #include #ifdef __cplusplus @@ -93,7 +94,7 @@ extern int aa_getprocattr(pid_t tid, const char *attr, char **label, char **mode); extern int aa_gettaskcon(pid_t target, char **label, char **mode); extern int aa_getcon(char **label, char **mode); -extern int aa_getpeercon_raw(int fd, char *buf, int *len, char **mode); +extern int aa_getpeercon_raw(int fd, char *buf, socklen_t *len, char **mode); extern int aa_getpeercon(int fd, char **label, char **mode); /* A NUL character is used to separate the query command prefix string from the diff --git a/libraries/libapparmor/src/kernel.c b/libraries/libapparmor/src/kernel.c index 98d26104f..9ac4449f4 100644 --- a/libraries/libapparmor/src/kernel.c +++ b/libraries/libapparmor/src/kernel.c @@ -797,7 +797,7 @@ int aa_getcon(char **label, char **mode) * Returns: length of confinement context including null termination or -1 on * error if errno == ERANGE then @len will hold the size needed */ -int aa_getpeercon_raw(int fd, char *buf, int *len, char **mode) +int aa_getpeercon_raw(int fd, char *buf, socklen_t *len, char **mode) { socklen_t optlen; int rc; @@ -806,7 +806,7 @@ int aa_getpeercon_raw(int fd, char *buf, int *len, char **mode) errno = EINVAL; return -1; } - optlen = (socklen_t) *len; + optlen = *len; if (!is_enabled()) { errno = EINVAL; @@ -821,7 +821,7 @@ int aa_getpeercon_raw(int fd, char *buf, int *len, char **mode) /* check for null termination */ if (buf[optlen - 1] != 0) { - if (optlen < (socklen_t) *len) { + if (optlen < *len) { buf[optlen] = 0; optlen++; } else { @@ -862,7 +862,8 @@ out: */ int aa_getpeercon(int fd, char **label, char **mode) { - int rc, last_size, size = INITIAL_GUESS_SIZE; + socklen_t last_size, size = INITIAL_GUESS_SIZE; + int rc; char *buffer = NULL; if (!label) {