Forbid basename(3) and dirname(3)

There are at least two interpretations of basename(3),
in addition to both functions being allowed to /both/ return a static
buffer (unsuitable in multi-threaded environments) /and/ raze the input
(which encourages overallocations, at best)

Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12105
This commit is contained in:
наб
2021-05-22 17:19:14 +02:00
committed by Brian Behlendorf
parent 64dfdaba37
commit feb04e6680
7 changed files with 45 additions and 33 deletions
@@ -11,3 +11,5 @@ pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/tests/functional/ctime
pkgexec_PROGRAMS = ctime
ctime_SOURCES = ctime.c
ctime_LDADD = $(abs_top_builddir)/lib/libzfs_core/libzfs_core.la
@@ -37,6 +37,7 @@
#include <utime.h>
#include <stdio.h>
#include <stdlib.h>
#include <libzutil.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
@@ -149,22 +150,18 @@ static int
do_link(const char *pfile)
{
int ret = 0;
char link_file[BUFSIZ] = { 0 };
char pfile_copy[BUFSIZ] = { 0 };
char *dname;
char link_file[BUFSIZ + 16] = { 0 };
if (pfile == NULL) {
return (-1);
}
strncpy(pfile_copy, pfile, sizeof (pfile_copy)-1);
pfile_copy[sizeof (pfile_copy) - 1] = '\0';
/*
* Figure out source file directory name, and create
* the link file in the same directory.
*/
dname = dirname((char *)pfile_copy);
(void) snprintf(link_file, BUFSIZ, "%s/%s", dname, "link_file");
(void) snprintf(link_file, sizeof (link_file),
"%.*s/%s", (int)zfs_dirnamelen(pfile), pfile, "link_file");
if (link(pfile, link_file) == -1) {
(void) fprintf(stderr, "link(%s, %s) failed with errno %d\n",
@@ -321,7 +318,7 @@ main(int argc, char *argv[])
(void) snprintf(tfile, sizeof (tfile), "%s/%s", penv[0], penv[1]);
/*
* If the test file is exists, remove it first.
* If the test file exists, remove it first.
*/
if (access(tfile, F_OK) == 0) {
(void) unlink(tfile);