From 407d038993aa2e1894c7312b94d0798d4cd96f02 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 7 Nov 2024 09:37:17 -0800 Subject: [PATCH] file-has-acl: minor ENOMEM fixes * lib/file-has-acl.c: Include limits.h. (get_aclinfo): Use a saturating add rather than reporting overflow, in the very unlikely case that the size overflows. This is simpler and is good enough here. Also, defend against implausible system that fails with ERANGE even when the buffer size is SSIZE_MAX. (file_has_aclinfo): Since we already assume elsewhere that malloc sets errno, rely on that here too. * modules/file-has-acl (Depends-on): Add limits.h. Also add malloc-posix, since we rely on malloc errno. --- ChangeLog | 10 ++++++++++ lib/file-has-acl.c | 23 ++++++----------------- modules/file-has-acl | 2 ++ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2bcee7d3f6..e881e194dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2024-11-07 Paul Eggert + file-has-acl: minor ENOMEM fixes + * lib/file-has-acl.c: Include limits.h. + (get_aclinfo): Use a saturating add rather than reporting + overflow, in the very unlikely case that the size overflows. + This is simpler and is good enough here. + Also, defend against implausible system that fails with ERANGE + even when the buffer size is SSIZE_MAX. + * modules/file-has-acl (Depends-on): Add limits.h. + Also add malloc-posix, since we rely on malloc errno. + file-has-acl: remove __gl_acl_alloc member It may have been needed in earlier versions of this code, but it is no longer needed. diff --git a/lib/file-has-acl.c b/lib/file-has-acl.c index a436793388..980c20249a 100644 --- a/lib/file-has-acl.c +++ b/lib/file-has-acl.c @@ -28,6 +28,7 @@ #include "acl.h" #include +#include #include "acl-internal.h" #include "attribute.h" @@ -125,7 +126,7 @@ get_aclinfo (char const *name, struct aclinfo *ai, int flags) if (0 < ai->size) break; ai->u.err = ai->size < 0 ? errno : 0; - if (! (ai->size < 0 && ai->u.err == ERANGE)) + if (! (ai->size < 0 && ai->u.err == ERANGE && acl_alloc < SSIZE_MAX)) break; /* The buffer was too small. Find how large it should have been. */ @@ -146,10 +147,7 @@ get_aclinfo (char const *name, struct aclinfo *ai, int flags) ai->buf = ai->u.__gl_acl_ch; } if (ckd_add (&acl_alloc, acl_alloc, acl_alloc >> 1)) - { - ai->u.err = ENOMEM; - break; - } + acl_alloc = SSIZE_MAX; if (acl_alloc < size) acl_alloc = size; if (SIZE_MAX < acl_alloc) @@ -543,10 +541,7 @@ file_has_aclinfo (MAYBE_UNUSED char const *restrict name, entries = malloced = (aclent_t *) malloc (alloc * sizeof (aclent_t)); if (entries == NULL) - { - errno = ENOMEM; - return -1; - } + return -1; continue; } break; @@ -616,10 +611,7 @@ file_has_aclinfo (MAYBE_UNUSED char const *restrict name, alloc = 2 * alloc; /* <= alloc_max */ entries = malloced = (ace_t *) malloc (alloc * sizeof (ace_t)); if (entries == NULL) - { - errno = ENOMEM; - return -1; - } + return -1; continue; } break; @@ -773,10 +765,7 @@ file_has_aclinfo (MAYBE_UNUSED char const *restrict name, free (acl); acl = malloc (aclsize); if (acl == NULL) - { - errno = ENOMEM; - return -1; - } + return -1; } if (type.u64 == ACL_AIXC) diff --git a/modules/file-has-acl b/modules/file-has-acl index b5821dab00..2fac2027da 100644 --- a/modules/file-has-acl +++ b/modules/file-has-acl @@ -16,6 +16,8 @@ errno extern-inline minmax free-posix +limits-h +malloc-posix ssize_t stat stdbool -- 2.39.5