acl-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/4] rm static buffer in __acl_quote for thread safety


From: Pavel Simovec
Subject: [PATCH 4/4] rm static buffer in __acl_quote for thread safety
Date: Wed, 24 Jan 2024 13:06:54 +0100

Replace static buffer with dynamically allocated one.
Adjust all usages of __acl_quote & xquote to be freed.
---
 include/misc.h             |  2 +-
 libacl/__acl_to_any_text.c |  4 +++-
 libmisc/quote.c            | 16 ++++++++--------
 tools/getfacl.c            | 30 +++++++++++++++++++++---------
 tools/setfacl.c            | 34 +++++++++++++++++++++++-----------
 5 files changed, 56 insertions(+), 30 deletions(-)

diff --git a/include/misc.h b/include/misc.h
index a1c8ae7..8044638 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -40,7 +40,7 @@ group_name(gid_t uid, int numeric);
 hidden char *grow_buffer(char **buffer, size_t *bufsize, int type);
 hidden int __acl_high_water_alloc(void **buf, size_t *bufsize, size_t newsize);
 
-hidden const char *__acl_quote(const char *str, const char *quote_chars);
+hidden char *__acl_quote(const char *str, const char *quote_chars);
 hidden char *__acl_unquote(char *str);
 
 hidden char *__acl_next_line(FILE *file);
diff --git a/libacl/__acl_to_any_text.c b/libacl/__acl_to_any_text.c
index fef411a..24bbe3c 100644
--- a/libacl/__acl_to_any_text.c
+++ b/libacl/__acl_to_any_text.c
@@ -130,7 +130,8 @@ acl_entry_to_any_str(const acl_entry_t entry_d, char 
*text_p, ssize_t size,
        permset_t effective;
        acl_tag_t type;
        ssize_t x;
-       const char *orig_text_p = text_p, *str;
+       const char *orig_text_p = text_p;
+       char *str = NULL;
        char *gn = NULL;
        char *un = NULL;
        if (!entry_obj_p)
@@ -222,6 +223,7 @@ acl_entry_to_any_str(const acl_entry_t entry_d, char 
*text_p, ssize_t size,
        }
        free(gn);
        free(un);
+       free(str);
 
        switch ((size >= 3) ? 3 : size) {
                case 3:
diff --git a/libmisc/quote.c b/libmisc/quote.c
index 9358c4d..1d5a678 100644
--- a/libmisc/quote.c
+++ b/libmisc/quote.c
@@ -24,25 +24,25 @@
 #include <string.h>
 #include "misc.h"
 
-const char *__acl_quote(const char *str, const char *quote_chars)
+char *__acl_quote(const char *str, const char *quote_chars)
 {
-       static char *quoted_str;
-       static size_t quoted_str_len;
+       char *quoted_str;
        const unsigned char *s;
        char *q;
-       size_t nonpr;
+       size_t nonpr, len;
 
        if (!str)
-               return str;
+               return NULL;
 
        for (nonpr = 0, s = (unsigned char *)str; *s != '\0'; s++)
                if (*s == '\\' || strchr(quote_chars, *s))
                        nonpr++;
        if (nonpr == 0)
-               return str;
+               return strdup(str);
 
-       if (__acl_high_water_alloc((void **)&quoted_str, &quoted_str_len,
-                            (s - (unsigned char *)str) + nonpr * 3 + 1))
+       len = (s - (unsigned char *)str) + nonpr * 3 + 1;
+       quoted_str = malloc(len);
+       if (!quoted_str)
                return NULL;
        for (s = (unsigned char *)str, q = quoted_str; *s != '\0'; s++) {
                if (strchr(quote_chars, *s)) {
diff --git a/tools/getfacl.c b/tools/getfacl.c
index 7039161..5e7b8b7 100644
--- a/tools/getfacl.c
+++ b/tools/getfacl.c
@@ -87,9 +87,9 @@ int print_options = TEXT_SOME_EFFECTIVE;
 int opt_numeric;  /* don't convert id's to symbolic names */
 
 
-static const char *xquote(const char *str, const char *quote_chars)
+static char *xquote(const char *str, const char *quote_chars)
 {
-       const char *q = __acl_quote(str, quote_chars);
+       char *q = __acl_quote(str, quote_chars);
        if (q == NULL) {
                fprintf(stderr, "%s: %s\n", progname, strerror(errno));
                exit(1);
@@ -155,7 +155,7 @@ struct name_list *get_list(const struct stat *st, acl_t acl)
                                }
                                break;
                }
-               const char *qname = xquote(name, "\t\n\r");
+               char *qname = xquote(name, "\t\n\r");
                free(name);
                len = strlen(qname);
                if (last == NULL) {
@@ -168,10 +168,12 @@ struct name_list *get_list(const struct stat *st, acl_t 
acl)
                }
                if (last == NULL) {
                        free_list(first);
+                       free(qname);
                        return NULL;
                }
                last->next = NULL;
                strcpy(last->name, qname);
+               free(qname);
 
                ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &ent);
        }
@@ -339,6 +341,7 @@ int do_show(FILE *stream, const char *path_p, const struct 
stat *st,
        acl_entry_t dacl_ent;
        char acl_mask[ACL_PERMS+1], dacl_mask[ACL_PERMS+1];
        int ret;
+       char *qp = NULL;
 
        names_width = 8;
        if (acl_names_width > names_width)
@@ -364,7 +367,8 @@ int do_show(FILE *stream, const char *path_p, const struct 
stat *st,
                if (ret < 0)
                        return ret;
        }
-       fprintf(stream, "# file: %s\n", xquote(path_p, "\n\r"));
+       fprintf(stream, "# file: %s\n", qp=xquote(path_p, "\n\r"));
+       free(qp);
        while (acl_names != NULL || dacl_names != NULL) {
                acl_tag_t acl_tag, dacl_tag;
 
@@ -449,10 +453,12 @@ int do_print(const char *path_p, const struct stat *st, 
int walk_flags, void *un
        const char *default_prefix = NULL;
        acl_t acl = NULL, default_acl = NULL;
        int error = 0;
+       char *qp = NULL;
 
        if (walk_flags & WALK_TREE_FAILED) {
-               fprintf(stderr, "%s: %s: %s\n", progname, xquote(path_p, 
"\n\r"),
+               fprintf(stderr, "%s: %s: %s\n", progname, qp=xquote(path_p, 
"\n\r"),
                        strerror(errno));
+               free(qp);
                return 1;
        }
 
@@ -514,13 +520,18 @@ int do_print(const char *path_p, const struct stat *st, 
int walk_flags, void *un
                        goto fail;
        } else {
                if (opt_comments) {
-                       printf("# file: %s\n", xquote(path_p, "\n\r"));
+                       printf("# file: %s\n", qp=xquote(path_p, "\n\r"));
+                       free(qp);
+                       char *qu;
                        char *un = user_name(st->st_uid, opt_numeric);
-                       printf("# owner: %s\n", xquote(un, " \t\n\r"));
+                       printf("# owner: %s\n", qu=xquote(un, " \t\n\r"));
                        free(un);
+                       free(qu);
+                       char *qg;
                        char *gn = group_name(st->st_gid, opt_numeric);
-                       printf("# group: %s\n", xquote(gn, " \t\n\r"));
+                       printf("# group: %s\n", qg=xquote(gn, " \t\n\r"));
                        free(gn);
+                       free(qg);
                        if ((st->st_mode & (S_ISVTX | S_ISUID | S_ISGID)) && 
!posixly_correct)
                                printf("# flags: %s\n", flagstr(st->st_mode));
                }
@@ -559,8 +570,9 @@ cleanup:
        return error;
 
 fail:
-       fprintf(stderr, "%s: %s: %s\n", progname, xquote(path_p, "\n\r"),
+       fprintf(stderr, "%s: %s: %s\n", progname, qp=xquote(path_p, "\n\r"),
                strerror(errno));
+       free(qp);
        error = -1;
        goto cleanup;
 }
diff --git a/tools/setfacl.c b/tools/setfacl.c
index 4140276..c4cd245 100644
--- a/tools/setfacl.c
+++ b/tools/setfacl.c
@@ -89,9 +89,9 @@ int chown_error;
 int promote_warning;
 
 
-static const char *xquote(const char *str, const char *quote_chars)
+static char *xquote(const char *str, const char *quote_chars)
 {
-       const char *q = __acl_quote(str, quote_chars);
+       char *q = __acl_quote(str, quote_chars);
        if (q == NULL) {
                fprintf(stderr, "%s: %s\n", progname, strerror(errno));
                exit(1);
@@ -128,6 +128,8 @@ restore(
        int lineno = 0, backup_line;
        int error, status = 0;
        int chmod_required = 0;
+       char *qf=NULL;
+       char *qp=NULL;
 
        memset(&st, 0, sizeof(st));
 
@@ -146,8 +148,9 @@ restore(
                        if (filename) {
                                fprintf(stderr, _("%s: %s: No filename found "
                                                  "in line %d, aborting\n"),
-                                       progname, xquote(filename, "\n\r"),
+                                       progname, qf=xquote(filename, "\n\r"),
                                        backup_line);
+                               free(qf);
                        } else {
                                fprintf(stderr, _("%s: No filename found in "
                                                 "line %d of standard input, "
@@ -171,8 +174,9 @@ restore(
                                     &lineno, NULL);
                if (error != 0) {
                        fprintf(stderr, _("%s: %s: %s in line %d\n"),
-                               progname, xquote(filename, "\n\r"), 
strerror(errno),
+                               progname, qf=xquote(filename, "\n\r"), 
strerror(errno),
                                lineno);
+                       free(qf);
                        status = 1;
                        goto getout;
                }
@@ -180,7 +184,8 @@ restore(
                error = stat(path_p, &st);
                if (opt_test && error != 0) {
                        fprintf(stderr, "%s: %s: %s\n", progname,
-                               xquote(path_p, "\n\r"), strerror(errno));
+                               qp=xquote(path_p, "\n\r"), strerror(errno));
+                       free(qp);
                        status = 1;
                }
 
@@ -204,8 +209,9 @@ restore(
                        if (chown(path_p, st.st_uid, st.st_gid) != 0) {
                                fprintf(stderr, _("%s: %s: Cannot change "
                                                  "owner/group: %s\n"),
-                                       progname, xquote(path_p, "\n\r"),
+                                       progname, qp=xquote(path_p, "\n\r"),
                                        strerror(errno));
+                               free(qp);
                                status = 1;
                        }
 
@@ -223,8 +229,9 @@ restore(
                        if (chmod(path_p, flags | args.mode) != 0) {
                                fprintf(stderr, _("%s: %s: Cannot change "
                                                  "mode: %s\n"),
-                                       progname, xquote(path_p, "\n\r"),
+                                       progname, qp=xquote(path_p, "\n\r"),
                                        strerror(errno));
+                               free(qp);
                                status = 1;
                        }
                }
@@ -253,8 +260,9 @@ getout:
 fail_errno:
        error = errno;
 fail:
-       fprintf(stderr, "%s: %s: %s\n", progname, xquote(filename, "\n\r"),
+       fprintf(stderr, "%s: %s: %s\n", progname, qf=xquote(filename, "\n\r"),
                strerror(error));
+       free(qf);
        status = 1;
        goto getout;
 }
@@ -339,6 +347,7 @@ int main(int argc, char *argv[])
        int error;
        seq_t seq;
        int seq_cmd, parse_mode;
+       char *qo=NULL;
        
        progname = basename(argv[0]);
 
@@ -509,8 +518,9 @@ int main(int argc, char *argv[])
                                        if (file == NULL) {
                                                fprintf(stderr, "%s: %s: %s\n",
                                                        progname,
-                                                       xquote(optarg, "\n\r"),
+                                                       qo=xquote(optarg, 
"\n\r"),
                                                        strerror(errno));
+                                               free(qo);
                                                status = 2;
                                                goto cleanup;
                                        }
@@ -535,7 +545,8 @@ int main(int argc, char *argv[])
                                                        progname,
                                                        strerror(errno),
                                                        lineno,
-                                                       xquote(optarg, "\n\r"));
+                                                       qo=xquote(optarg, 
"\n\r"));
+                                               free(qo);
                                        } else {
                                                fprintf(stderr, _(
                                                        "%s: %s in line "
@@ -570,8 +581,9 @@ int main(int argc, char *argv[])
                                        if (file == NULL) {
                                                fprintf(stderr, "%s: %s: %s\n",
                                                        progname,
-                                                       xquote(optarg, "\n\r"),
+                                                       qo=xquote(optarg, 
"\n\r"),
                                                        strerror(errno));
+                                               free(qo);
                                                status = 2;
                                                goto cleanup;
                                        }
-- 
2.43.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]