selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] libselinux: simplify string copying
@ 2022-11-09 20:09 Christian Göttsche
  2022-11-09 20:09 ` [PATCH 2/3] checkpolicy: " Christian Göttsche
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Christian Göttsche @ 2022-11-09 20:09 UTC (permalink / raw)
  To: selinux

Use strdup(3)/strndup(3) instead of allocating memory and then manually
copying the content.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/src/context.c                     | 11 +++++------
 libselinux/src/get_default_type.c            |  3 +--
 libselinux/src/matchpathcon.c                |  9 +++------
 libselinux/utils/selabel_lookup_best_match.c | 10 ++++------
 4 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/libselinux/src/context.c b/libselinux/src/context.c
index 9dddbc5a..8830bf42 100644
--- a/libselinux/src/context.c
+++ b/libselinux/src/context.c
@@ -149,19 +149,18 @@ static int set_comp(context_private_t * n, int idx, const char *str)
 	char *t = NULL;
 	const char *p;
 	if (str) {
-		t = (char *)malloc(strlen(str) + 1);
-		if (!t) {
-			return -1;
-		}
 		for (p = str; *p; p++) {
 			if (*p == '\t' || *p == '\n' || *p == '\r' ||
 			    ((*p == ':' || *p == ' ') && idx != COMP_RANGE)) {
-				free(t);
 				errno = EINVAL;
 				return -1;
 			}
 		}
-		strcpy(t, str);
+
+		t = strdup(str);
+		if (!t) {
+			return -1;
+		}
 	}
 	conditional_free(&n->component[idx]);
 	n->component[idx] = t;
diff --git a/libselinux/src/get_default_type.c b/libselinux/src/get_default_type.c
index dd7b5d79..766ea4b7 100644
--- a/libselinux/src/get_default_type.c
+++ b/libselinux/src/get_default_type.c
@@ -62,10 +62,9 @@ static int find_default_type(FILE * fp, const char *role, char **type)
 		return -1;
 	}
 
-	t = malloc(strlen(buf) - len);
+	t = strndup(ptr, strlen(buf) - len - 1);
 	if (!t)
 		return -1;
-	strcpy(t, ptr);
 	*type = t;
 	return 0;
 }
diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c
index ea78a23e..bf2da083 100644
--- a/libselinux/src/matchpathcon.c
+++ b/libselinux/src/matchpathcon.c
@@ -215,10 +215,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
 			if (ret < 0 || sb.st_ino != ino) {
 				fl->specind = specind;
 				free(fl->file);
-				fl->file = malloc(strlen(file) + 1);
+				fl->file = strdup(file);
 				if (!fl->file)
 					goto oom;
-				strcpy(fl->file, file);
 				return fl->specind;
 
 			}
@@ -232,10 +231,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
 			     __FUNCTION__, file, fl->file,
 			     con_array[fl->specind]);
 			free(fl->file);
-			fl->file = malloc(strlen(file) + 1);
+			fl->file = strdup(file);
 			if (!fl->file)
 				goto oom;
-			strcpy(fl->file, file);
 			return fl->specind;
 		}
 
@@ -248,10 +246,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
 		goto oom;
 	fl->ino = ino;
 	fl->specind = specind;
-	fl->file = malloc(strlen(file) + 1);
+	fl->file = strdup(file);
 	if (!fl->file)
 		goto oom_freefl;
-	strcpy(fl->file, file);
 	fl->next = prevfl->next;
 	prevfl->next = fl;
 	return fl->specind;
diff --git a/libselinux/utils/selabel_lookup_best_match.c b/libselinux/utils/selabel_lookup_best_match.c
index a4af0679..e816c04b 100644
--- a/libselinux/utils/selabel_lookup_best_match.c
+++ b/libselinux/utils/selabel_lookup_best_match.c
@@ -30,7 +30,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
 	exit(1);
 }
 
-static mode_t string_to_mode(char *s)
+static mode_t string_to_mode(const char *s)
 {
 	switch (s[0]) {
 	case 'b':
@@ -53,7 +53,7 @@ static mode_t string_to_mode(char *s)
 
 int main(int argc, char **argv)
 {
-	int raw = 0, mode = 0, rc, opt, i, num_links, string_len;
+	int raw = 0, mode = 0, rc, opt, i, num_links;
 	char *validate = NULL, *path = NULL, *context = NULL, *file = NULL;
 	char **links = NULL;
 
@@ -101,13 +101,11 @@ int main(int argc, char **argv)
 		}
 
 		for (i = optind, num_links = 0; i < argc; i++, num_links++) {
-			string_len = strlen(argv[i]) + 1;
-			links[num_links] = malloc(string_len);
+			links[num_links] = strdup(argv[i]);
 			if (!links[num_links]) {
-				fprintf(stderr, "ERROR: malloc failed.\n");
+				fprintf(stderr, "ERROR: strdup failed.\n");
 				exit(1);
 			}
-			strcpy(links[num_links], argv[i]);
 		}
 	}
 
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] checkpolicy: simplify string copying
  2022-11-09 20:09 [PATCH 1/3] libselinux: simplify string copying Christian Göttsche
@ 2022-11-09 20:09 ` Christian Göttsche
  2022-11-09 20:09 ` [PATCH 3/3] libsepol: " Christian Göttsche
  2022-11-10 13:55 ` [PATCH 1/3] libselinux: " James Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Christian Göttsche @ 2022-11-09 20:09 UTC (permalink / raw)
  To: selinux

Use strdup(3) instead of allocating memory and then manually copying the
content.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 checkpolicy/checkpolicy.c   | 10 ++++------
 checkpolicy/policy_define.c |  3 +--
 checkpolicy/test/dispol.c   |  5 ++---
 3 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/checkpolicy/checkpolicy.c b/checkpolicy/checkpolicy.c
index 926ce72c..48c31261 100644
--- a/checkpolicy/checkpolicy.c
+++ b/checkpolicy/checkpolicy.c
@@ -1148,12 +1148,11 @@ int main(int argc, char **argv)
 			FGETS(ans, sizeof(ans), stdin);
 			ans[strlen(ans) - 1] = 0;
 
-			name = malloc((strlen(ans) + 1) * sizeof(char));
+			name = strdup(ans);
 			if (name == NULL) {
-				fprintf(stderr, "couldn't malloc string.\n");
+				fprintf(stderr, "couldn't strdup string.\n");
 				break;
 			}
-			strcpy(name, ans);
 
 			printf("state? ");
 			FGETS(ans, sizeof(ans), stdin);
@@ -1296,12 +1295,11 @@ int main(int argc, char **argv)
 			FGETS(ans, sizeof(ans), stdin);
 			ans[strlen(ans) - 1] = 0;
 
-			name = malloc((strlen(ans) + 1) * sizeof(char));
+			name = strdup(ans);
 			if (!name) {
-				fprintf(stderr, "couldn't malloc string.\n");
+				fprintf(stderr, "couldn't strdup string.\n");
 				break;
 			}
-			strcpy(name, ans);
 
 			printf("port? ");
 			FGETS(ans, sizeof(ans), stdin);
diff --git a/checkpolicy/policy_define.c b/checkpolicy/policy_define.c
index 54bb304b..41e44631 100644
--- a/checkpolicy/policy_define.c
+++ b/checkpolicy/policy_define.c
@@ -117,12 +117,11 @@ int insert_id(const char *id, int push)
 	char *newid = 0;
 	int error;
 
-	newid = (char *)malloc(strlen(id) + 1);
+	newid = strdup(id);
 	if (!newid) {
 		yyerror("out of memory");
 		return -1;
 	}
-	strcpy(newid, id);
 	if (push)
 		error = queue_push(id_queue, (queue_element_t) newid);
 	else
diff --git a/checkpolicy/test/dispol.c b/checkpolicy/test/dispol.c
index 8ddefb04..36a3362c 100644
--- a/checkpolicy/test/dispol.c
+++ b/checkpolicy/test/dispol.c
@@ -486,12 +486,11 @@ int main(int argc, char **argv)
 			}
 			ans[strlen(ans) - 1] = 0;
 
-			name = malloc((strlen(ans) + 1) * sizeof(char));
+			name = strdup(ans);
 			if (name == NULL) {
-				fprintf(stderr, "couldn't malloc string.\n");
+				fprintf(stderr, "couldn't strdup string.\n");
 				break;
 			}
-			strcpy(name, ans);
 
 			printf("state? ");
 			if (fgets(ans, sizeof(ans), stdin) == NULL) {
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] libsepol: simplify string copying
  2022-11-09 20:09 [PATCH 1/3] libselinux: simplify string copying Christian Göttsche
  2022-11-09 20:09 ` [PATCH 2/3] checkpolicy: " Christian Göttsche
@ 2022-11-09 20:09 ` Christian Göttsche
  2022-11-10 13:55 ` [PATCH 1/3] libselinux: " James Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Christian Göttsche @ 2022-11-09 20:09 UTC (permalink / raw)
  To: selinux

Use strdup(3) instead of allocating memory and then manually copying the
content.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsepol/src/policydb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libsepol/src/policydb.c b/libsepol/src/policydb.c
index 8a65df05..b79c19b9 100644
--- a/libsepol/src/policydb.c
+++ b/libsepol/src/policydb.c
@@ -776,12 +776,11 @@ static int roles_init(policydb_t * p)
 		rc = -ENOMEM;
 		goto out;
 	}
-	key = malloc(strlen(OBJECT_R) + 1);
+	key = strdup(OBJECT_R);
 	if (!key) {
 		rc = -ENOMEM;
 		goto out_free_role;
 	}
-	strcpy(key, OBJECT_R);
 	rc = symtab_insert(p, SYM_ROLES, key, role,
 			   (p->policy_type ==
 			    POLICY_MOD ? SCOPE_REQ : SCOPE_DECL), 1,
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] libselinux: simplify string copying
  2022-11-09 20:09 [PATCH 1/3] libselinux: simplify string copying Christian Göttsche
  2022-11-09 20:09 ` [PATCH 2/3] checkpolicy: " Christian Göttsche
  2022-11-09 20:09 ` [PATCH 3/3] libsepol: " Christian Göttsche
@ 2022-11-10 13:55 ` James Carter
  2022-11-21 20:55   ` James Carter
  2 siblings, 1 reply; 5+ messages in thread
From: James Carter @ 2022-11-10 13:55 UTC (permalink / raw)
  To: Christian Göttsche; +Cc: selinux

On Wed, Nov 9, 2022 at 3:11 PM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> Use strdup(3)/strndup(3) instead of allocating memory and then manually
> copying the content.
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

For these three patches:
Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  libselinux/src/context.c                     | 11 +++++------
>  libselinux/src/get_default_type.c            |  3 +--
>  libselinux/src/matchpathcon.c                |  9 +++------
>  libselinux/utils/selabel_lookup_best_match.c | 10 ++++------
>  4 files changed, 13 insertions(+), 20 deletions(-)
>
> diff --git a/libselinux/src/context.c b/libselinux/src/context.c
> index 9dddbc5a..8830bf42 100644
> --- a/libselinux/src/context.c
> +++ b/libselinux/src/context.c
> @@ -149,19 +149,18 @@ static int set_comp(context_private_t * n, int idx, const char *str)
>         char *t = NULL;
>         const char *p;
>         if (str) {
> -               t = (char *)malloc(strlen(str) + 1);
> -               if (!t) {
> -                       return -1;
> -               }
>                 for (p = str; *p; p++) {
>                         if (*p == '\t' || *p == '\n' || *p == '\r' ||
>                             ((*p == ':' || *p == ' ') && idx != COMP_RANGE)) {
> -                               free(t);
>                                 errno = EINVAL;
>                                 return -1;
>                         }
>                 }
> -               strcpy(t, str);
> +
> +               t = strdup(str);
> +               if (!t) {
> +                       return -1;
> +               }
>         }
>         conditional_free(&n->component[idx]);
>         n->component[idx] = t;
> diff --git a/libselinux/src/get_default_type.c b/libselinux/src/get_default_type.c
> index dd7b5d79..766ea4b7 100644
> --- a/libselinux/src/get_default_type.c
> +++ b/libselinux/src/get_default_type.c
> @@ -62,10 +62,9 @@ static int find_default_type(FILE * fp, const char *role, char **type)
>                 return -1;
>         }
>
> -       t = malloc(strlen(buf) - len);
> +       t = strndup(ptr, strlen(buf) - len - 1);
>         if (!t)
>                 return -1;
> -       strcpy(t, ptr);
>         *type = t;
>         return 0;
>  }
> diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c
> index ea78a23e..bf2da083 100644
> --- a/libselinux/src/matchpathcon.c
> +++ b/libselinux/src/matchpathcon.c
> @@ -215,10 +215,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
>                         if (ret < 0 || sb.st_ino != ino) {
>                                 fl->specind = specind;
>                                 free(fl->file);
> -                               fl->file = malloc(strlen(file) + 1);
> +                               fl->file = strdup(file);
>                                 if (!fl->file)
>                                         goto oom;
> -                               strcpy(fl->file, file);
>                                 return fl->specind;
>
>                         }
> @@ -232,10 +231,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
>                              __FUNCTION__, file, fl->file,
>                              con_array[fl->specind]);
>                         free(fl->file);
> -                       fl->file = malloc(strlen(file) + 1);
> +                       fl->file = strdup(file);
>                         if (!fl->file)
>                                 goto oom;
> -                       strcpy(fl->file, file);
>                         return fl->specind;
>                 }
>
> @@ -248,10 +246,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
>                 goto oom;
>         fl->ino = ino;
>         fl->specind = specind;
> -       fl->file = malloc(strlen(file) + 1);
> +       fl->file = strdup(file);
>         if (!fl->file)
>                 goto oom_freefl;
> -       strcpy(fl->file, file);
>         fl->next = prevfl->next;
>         prevfl->next = fl;
>         return fl->specind;
> diff --git a/libselinux/utils/selabel_lookup_best_match.c b/libselinux/utils/selabel_lookup_best_match.c
> index a4af0679..e816c04b 100644
> --- a/libselinux/utils/selabel_lookup_best_match.c
> +++ b/libselinux/utils/selabel_lookup_best_match.c
> @@ -30,7 +30,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
>         exit(1);
>  }
>
> -static mode_t string_to_mode(char *s)
> +static mode_t string_to_mode(const char *s)
>  {
>         switch (s[0]) {
>         case 'b':
> @@ -53,7 +53,7 @@ static mode_t string_to_mode(char *s)
>
>  int main(int argc, char **argv)
>  {
> -       int raw = 0, mode = 0, rc, opt, i, num_links, string_len;
> +       int raw = 0, mode = 0, rc, opt, i, num_links;
>         char *validate = NULL, *path = NULL, *context = NULL, *file = NULL;
>         char **links = NULL;
>
> @@ -101,13 +101,11 @@ int main(int argc, char **argv)
>                 }
>
>                 for (i = optind, num_links = 0; i < argc; i++, num_links++) {
> -                       string_len = strlen(argv[i]) + 1;
> -                       links[num_links] = malloc(string_len);
> +                       links[num_links] = strdup(argv[i]);
>                         if (!links[num_links]) {
> -                               fprintf(stderr, "ERROR: malloc failed.\n");
> +                               fprintf(stderr, "ERROR: strdup failed.\n");
>                                 exit(1);
>                         }
> -                       strcpy(links[num_links], argv[i]);
>                 }
>         }
>
> --
> 2.38.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] libselinux: simplify string copying
  2022-11-10 13:55 ` [PATCH 1/3] libselinux: " James Carter
@ 2022-11-21 20:55   ` James Carter
  0 siblings, 0 replies; 5+ messages in thread
From: James Carter @ 2022-11-21 20:55 UTC (permalink / raw)
  To: Christian Göttsche; +Cc: selinux

On Thu, Nov 10, 2022 at 8:55 AM James Carter <jwcart2@gmail.com> wrote:
>
> On Wed, Nov 9, 2022 at 3:11 PM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > Use strdup(3)/strndup(3) instead of allocating memory and then manually
> > copying the content.
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> For these three patches:
> Acked-by: James Carter <jwcart2@gmail.com>
>

These three patches have been merged.
Thanks,
Jim

> > ---
> >  libselinux/src/context.c                     | 11 +++++------
> >  libselinux/src/get_default_type.c            |  3 +--
> >  libselinux/src/matchpathcon.c                |  9 +++------
> >  libselinux/utils/selabel_lookup_best_match.c | 10 ++++------
> >  4 files changed, 13 insertions(+), 20 deletions(-)
> >
> > diff --git a/libselinux/src/context.c b/libselinux/src/context.c
> > index 9dddbc5a..8830bf42 100644
> > --- a/libselinux/src/context.c
> > +++ b/libselinux/src/context.c
> > @@ -149,19 +149,18 @@ static int set_comp(context_private_t * n, int idx, const char *str)
> >         char *t = NULL;
> >         const char *p;
> >         if (str) {
> > -               t = (char *)malloc(strlen(str) + 1);
> > -               if (!t) {
> > -                       return -1;
> > -               }
> >                 for (p = str; *p; p++) {
> >                         if (*p == '\t' || *p == '\n' || *p == '\r' ||
> >                             ((*p == ':' || *p == ' ') && idx != COMP_RANGE)) {
> > -                               free(t);
> >                                 errno = EINVAL;
> >                                 return -1;
> >                         }
> >                 }
> > -               strcpy(t, str);
> > +
> > +               t = strdup(str);
> > +               if (!t) {
> > +                       return -1;
> > +               }
> >         }
> >         conditional_free(&n->component[idx]);
> >         n->component[idx] = t;
> > diff --git a/libselinux/src/get_default_type.c b/libselinux/src/get_default_type.c
> > index dd7b5d79..766ea4b7 100644
> > --- a/libselinux/src/get_default_type.c
> > +++ b/libselinux/src/get_default_type.c
> > @@ -62,10 +62,9 @@ static int find_default_type(FILE * fp, const char *role, char **type)
> >                 return -1;
> >         }
> >
> > -       t = malloc(strlen(buf) - len);
> > +       t = strndup(ptr, strlen(buf) - len - 1);
> >         if (!t)
> >                 return -1;
> > -       strcpy(t, ptr);
> >         *type = t;
> >         return 0;
> >  }
> > diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c
> > index ea78a23e..bf2da083 100644
> > --- a/libselinux/src/matchpathcon.c
> > +++ b/libselinux/src/matchpathcon.c
> > @@ -215,10 +215,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
> >                         if (ret < 0 || sb.st_ino != ino) {
> >                                 fl->specind = specind;
> >                                 free(fl->file);
> > -                               fl->file = malloc(strlen(file) + 1);
> > +                               fl->file = strdup(file);
> >                                 if (!fl->file)
> >                                         goto oom;
> > -                               strcpy(fl->file, file);
> >                                 return fl->specind;
> >
> >                         }
> > @@ -232,10 +231,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
> >                              __FUNCTION__, file, fl->file,
> >                              con_array[fl->specind]);
> >                         free(fl->file);
> > -                       fl->file = malloc(strlen(file) + 1);
> > +                       fl->file = strdup(file);
> >                         if (!fl->file)
> >                                 goto oom;
> > -                       strcpy(fl->file, file);
> >                         return fl->specind;
> >                 }
> >
> > @@ -248,10 +246,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)
> >                 goto oom;
> >         fl->ino = ino;
> >         fl->specind = specind;
> > -       fl->file = malloc(strlen(file) + 1);
> > +       fl->file = strdup(file);
> >         if (!fl->file)
> >                 goto oom_freefl;
> > -       strcpy(fl->file, file);
> >         fl->next = prevfl->next;
> >         prevfl->next = fl;
> >         return fl->specind;
> > diff --git a/libselinux/utils/selabel_lookup_best_match.c b/libselinux/utils/selabel_lookup_best_match.c
> > index a4af0679..e816c04b 100644
> > --- a/libselinux/utils/selabel_lookup_best_match.c
> > +++ b/libselinux/utils/selabel_lookup_best_match.c
> > @@ -30,7 +30,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)
> >         exit(1);
> >  }
> >
> > -static mode_t string_to_mode(char *s)
> > +static mode_t string_to_mode(const char *s)
> >  {
> >         switch (s[0]) {
> >         case 'b':
> > @@ -53,7 +53,7 @@ static mode_t string_to_mode(char *s)
> >
> >  int main(int argc, char **argv)
> >  {
> > -       int raw = 0, mode = 0, rc, opt, i, num_links, string_len;
> > +       int raw = 0, mode = 0, rc, opt, i, num_links;
> >         char *validate = NULL, *path = NULL, *context = NULL, *file = NULL;
> >         char **links = NULL;
> >
> > @@ -101,13 +101,11 @@ int main(int argc, char **argv)
> >                 }
> >
> >                 for (i = optind, num_links = 0; i < argc; i++, num_links++) {
> > -                       string_len = strlen(argv[i]) + 1;
> > -                       links[num_links] = malloc(string_len);
> > +                       links[num_links] = strdup(argv[i]);
> >                         if (!links[num_links]) {
> > -                               fprintf(stderr, "ERROR: malloc failed.\n");
> > +                               fprintf(stderr, "ERROR: strdup failed.\n");
> >                                 exit(1);
> >                         }
> > -                       strcpy(links[num_links], argv[i]);
> >                 }
> >         }
> >
> > --
> > 2.38.1
> >

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-21 20:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09 20:09 [PATCH 1/3] libselinux: simplify string copying Christian Göttsche
2022-11-09 20:09 ` [PATCH 2/3] checkpolicy: " Christian Göttsche
2022-11-09 20:09 ` [PATCH 3/3] libsepol: " Christian Göttsche
2022-11-10 13:55 ` [PATCH 1/3] libselinux: " James Carter
2022-11-21 20:55   ` James Carter

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 NNTP newsgroup(s).