[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 2/2] rm static buffer in __acl_quote for thread safety
From: |
Pavel Simovec |
Subject: |
[PATCH v2 2/2] rm static buffer in __acl_quote for thread safety |
Date: |
Tue, 13 Feb 2024 13:24:06 +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 | 38 ++++++++++++++++++--------
tools/setfacl.c | 55 ++++++++++++++++++++++----------------
5 files changed, 71 insertions(+), 44 deletions(-)
diff --git a/include/misc.h b/include/misc.h
index 8700610..3f0bc11 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -44,7 +44,7 @@ hidden ATTR_MALLOC char *__acl_group_name(gid_t uid, int
numeric);
hidden char *__acl_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 ATTR_MALLOC 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 8a35645..82c5d40 100644
--- a/libacl/__acl_to_any_text.c
+++ b/libacl/__acl_to_any_text.c
@@ -129,7 +129,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)
@@ -221,6 +222,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 cd50bf0..764eb41 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 **)"ed_str, "ed_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 0f2abcb..b88c501 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,9 @@ 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"));
+ qp = xquote(path_p, "\n\r");
+ fprintf(stream, "# file: %s\n", qp);
+ free(qp);
while (acl_names != NULL || dacl_names != NULL) {
acl_tag_t acl_tag, dacl_tag;
@@ -449,10 +454,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"),
- strerror(errno));
+ qp = xquote(path_p, "\n\r");
+ fprintf(stderr, "%s: %s: %s\n", progname, qp, strerror(errno));
+ free(qp);
return 1;
}
@@ -514,13 +521,21 @@ 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"));
+ qp = xquote(path_p, "\n\r");
+ printf("# file: %s\n", qp);
+ free(qp);
+ char *qu;
char *un = __acl_user_name(st->st_uid, opt_numeric);
- printf("# owner: %s\n", xquote(un, " \t\n\r"));
+ qu=xquote(un, " \t\n\r");
+ printf("# owner: %s\n", qu);
free(un);
+ free(qu);
+ char *qg;
char *gn = __acl_group_name(st->st_gid, opt_numeric);
- printf("# group: %s\n", xquote(gn, " \t\n\r"));
+ qg=xquote(gn, " \t\n\r");
+ printf("# group: %s\n", qg);
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 +574,9 @@ cleanup:
return error;
fail:
- fprintf(stderr, "%s: %s: %s\n", progname, xquote(path_p, "\n\r"),
- strerror(errno));
+ qp = xquote(path_p, "\n\r");
+ fprintf(stderr, "%s: %s: %s\n", progname, qp, strerror(errno));
+ free(qp);
error = -1;
goto cleanup;
}
diff --git a/tools/setfacl.c b/tools/setfacl.c
index 86ba733..35434e3 100644
--- a/tools/setfacl.c
+++ b/tools/setfacl.c
@@ -88,9 +88,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);
@@ -127,6 +127,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));
@@ -143,10 +145,11 @@ restore(
if (path_p == NULL) {
if (filename) {
+ qf = xquote(filename, "\n\r");
fprintf(stderr, _("%s: %s: No filename found "
"in line %d, aborting\n"),
- progname, xquote(filename, "\n\r"),
- backup_line);
+ progname, qf, backup_line);
+ free(qf);
} else {
fprintf(stderr, _("%s: No filename found in "
"line %d of standard input, "
@@ -169,17 +172,20 @@ restore(
SEQ_PARSE_MULTI,
&lineno, NULL);
if (error != 0) {
+ qf = xquote(filename, "\n\r");
fprintf(stderr, _("%s: %s: %s in line %d\n"),
- progname, xquote(filename, "\n\r"),
strerror(errno),
- lineno);
+ progname, qf, strerror(errno), lineno);
+ free(qf);
status = 1;
goto getout;
}
error = stat(path_p, &st);
if (opt_test && error != 0) {
+ qp = xquote(path_p, "\n\r");
fprintf(stderr, "%s: %s: %s\n", progname,
- xquote(path_p, "\n\r"), strerror(errno));
+ qp , strerror(errno));
+ free(qp);
status = 1;
}
@@ -201,10 +207,11 @@ restore(
if (!opt_test &&
(st.st_uid != -1 || st.st_gid != -1)) {
if (chown(path_p, st.st_uid, st.st_gid) != 0) {
+ qp = xquote(path_p, "\n\r");
fprintf(stderr, _("%s: %s: Cannot change "
"owner/group: %s\n"),
- progname, xquote(path_p, "\n\r"),
- strerror(errno));
+ progname, qp, strerror(errno));
+ free(qp);
status = 1;
}
@@ -220,10 +227,11 @@ restore(
args.mode = st.st_mode;
args.mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
if (chmod(path_p, flags | args.mode) != 0) {
+ qp = xquote(path_p, "\n\r");
fprintf(stderr, _("%s: %s: Cannot change "
"mode: %s\n"),
- progname, xquote(path_p, "\n\r"),
- strerror(errno));
+ progname, qp, strerror(errno));
+ free(qp);
status = 1;
}
}
@@ -252,8 +260,9 @@ getout:
fail_errno:
error = errno;
fail:
- fprintf(stderr, "%s: %s: %s\n", progname, xquote(filename, "\n\r"),
- strerror(error));
+ qf=xquote(filename, "\n\r");
+ fprintf(stderr, "%s: %s: %s\n", progname, qf, strerror(error));
+ free(qf);
status = 1;
goto getout;
}
@@ -338,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]);
@@ -506,10 +516,10 @@ int main(int argc, char *argv[])
} else {
file = fopen(optarg, "r");
if (file == NULL) {
+ qo = xquote(optarg, "\n\r");
fprintf(stderr, "%s: %s: %s\n",
- progname,
- xquote(optarg, "\n\r"),
- strerror(errno));
+ progname, qo,
strerror(errno));
+ free(qo);
status = 2;
goto cleanup;
}
@@ -528,13 +538,12 @@ int main(int argc, char *argv[])
errno = EINVAL;
if (file != stdin) {
+ qo = xquote(optarg, "\n\r");
fprintf(stderr, _(
"%s: %s in line "
"%d of file %s\n"),
- progname,
- strerror(errno),
- lineno,
- xquote(optarg, "\n\r"));
+
progname,strerror(errno), lineno, qo);
+ free(qo);
} else {
fprintf(stderr, _(
"%s: %s in line "
@@ -567,10 +576,10 @@ int main(int argc, char *argv[])
else {
file = fopen(optarg, "r");
if (file == NULL) {
+ qo = xquote(optarg, "\n\r");
fprintf(stderr, "%s: %s: %s\n",
- progname,
- xquote(optarg, "\n\r"),
- strerror(errno));
+ progname, qo,
strerror(errno));
+ free(qo);
status = 2;
goto cleanup;
}
--
2.43.1