From: Cyril Hrubis <chrubis@suse.cz>
To: Yang Xu <xuyang2018.jy@fujitsu.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 1/2] syscalls/faccessat201: Add new testcase
Date: Thu, 10 Aug 2023 16:54:18 +0200 [thread overview]
Message-ID: <ZNT6GkZ05CrGk5pV@yuki> (raw)
In-Reply-To: <1691208482-5464-4-git-send-email-xuyang2018.jy@fujitsu.com>
Hi!
> diff --git a/include/lapi/faccessat2.h b/include/lapi/faccessat2.h
> new file mode 100644
> index 000000000..e8b27d6b1
> --- /dev/null
> +++ b/include/lapi/faccessat2.h
> @@ -0,0 +1,20 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
> + */
> +
> +#ifndef LAPI_FACCESSAT2_H__
> +#define LAPI_FACCESSAT2_H__
> +
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +
> +#ifndef HAVE_FACCESSAT2
> +int faccessat2(int dirfd, const char *pathname, int mode, int flags)
> +{
> + return tst_syscall(__NR_faccessat2, dirfd, pathname, mode, flags);
> +}
> +#endif
> +
> +#endif /* LAPI_FACCESSAT2_H__ */
This one needs the configure check, and I would call the header just
faccessat.h.
> diff --git a/testcases/kernel/syscalls/faccessat2/faccessat201.c b/testcases/kernel/syscalls/faccessat2/faccessat201.c
> new file mode 100644
> index 000000000..658d1dd81
> --- /dev/null
> +++ b/testcases/kernel/syscalls/faccessat2/faccessat201.c
> @@ -0,0 +1,115 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Check the basic functionality of faccessat2().
> + *
> + * Minimum Linux version required is v5.8.
> + */
> +
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <pwd.h>
> +
> +#include "tst_test.h"
> +#include "lapi/syscalls.h"
> +#include "lapi/faccessat2.h"
> +
> +#define TESTUSER "nobody"
> +#define TESTDIR "faccessat2dir"
> +#define TESTFILE "faccessat2file"
> +#define RELPATH "faccessat2dir/faccessat2file"
> +#define TESTSYMLINK "faccessat2symlink"
There is a mix of tabs and spaces in this part that should be cleaned
up.
> +static int dir_fd = -1, bad_fd = -1;
> +static int atcwd_fd = AT_FDCWD;
> +static char *filepaths[4];
> +static char abs_path[128];
> +static struct passwd *ltpuser;
> +
> +static struct tcase {
> + int *fd;
> + char **filename;
> + int flags;
> + int exp_errno;
> +} tcases[] = {
> + {&dir_fd, &filepaths[0], 0, 0},
> + {&bad_fd, &filepaths[1], 0, 0},
> + {&atcwd_fd, &filepaths[2], 0, 0},
> + {&dir_fd, &filepaths[0], AT_EACCESS, 0},
> + {&bad_fd, &filepaths[1], AT_EACCESS, 0},
> + {&atcwd_fd, &filepaths[2], AT_EACCESS, 0},
> + {&dir_fd, &filepaths[0], AT_EACCESS, EACCES},
> + {&bad_fd, &filepaths[1], AT_EACCESS, EACCES},
> + {&atcwd_fd, &filepaths[2], AT_EACCESS, EACCES},
> + {&atcwd_fd, &filepaths[3], AT_SYMLINK_NOFOLLOW, 0},
> +};
> +
> +static void verify_faccessat2(unsigned int i)
> +{
> + struct tcase *tc = &tcases[i];
> +
> + if (tc->exp_errno == EACCES) {
> + if (SAFE_FORK() == 0) {
> + SAFE_SETUID(ltpuser->pw_uid);
> + TST_EXP_FAIL(faccessat2(*tc->fd, *tc->filename, R_OK, tc->flags),
> + tc->exp_errno, "faccessat2(%d, %s, R_OK, %d) as %s",
> + *tc->fd, *tc->filename, tc->flags, TESTUSER);
> + }
> +
> + tst_reap_children();
I would just put the EACCESS tests to a separate testcase, which would
make the test more straightforward and easier to read.
> + } else {
> + TST_EXP_PASS(faccessat2(*tc->fd, *tc->filename, R_OK, tc->flags),
> + "faccessat2(%d, %s, R_OK, %d) as root",
> + *tc->fd, *tc->filename, tc->flags);
> + }
> +}
> +
> +static void setup(void)
> +{
> + char *tmpdir_path = tst_get_tmpdir();
> +
> + sprintf(abs_path, "%s/%s", tmpdir_path, RELPATH);
> + free(tmpdir_path);
> +
> + SAFE_MKDIR(TESTDIR, 0666);
> + dir_fd = SAFE_OPEN(TESTDIR, O_DIRECTORY);
> + SAFE_TOUCH(abs_path, 0444, NULL);
> + SAFE_SYMLINK(abs_path, TESTSYMLINK);
> +
> + filepaths[0] = TESTFILE;
> + filepaths[1] = abs_path;
> + filepaths[2] = RELPATH;
> + filepaths[3] = TESTSYMLINK;
> +
> + ltpuser = SAFE_GETPWNAM(TESTUSER);
> +}
> +
> +static void cleanup(void)
> +{
> + if (dir_fd > 0)
> + SAFE_CLOSE(dir_fd);
> +}
> +
> +static struct tst_test test = {
> + .test = verify_faccessat2,
> + .tcnt = ARRAY_SIZE(tcases),
> + .setup = setup,
> + .cleanup = cleanup,
> + .bufs = (struct tst_buffers []) {
> + {&filepaths[0], .size = sizeof(*filepaths[0])},
> + {&filepaths[1], .size = sizeof(*filepaths[1])},
> + {&filepaths[2], .size = sizeof(*filepaths[2])},
> + {&filepaths[3], .size = sizeof(*filepaths[3])},
> + {<puser, .size = sizeof(ltpuser)},
Why do we allocate anything here when we replace the pointers in the
test setup anyways?
If anything it would make sense to allocate the buffers in the test
setup instead of using static strings there. Also filepath[x] way less
readable than actually giving the variables proper names such as
abs_path.
I guess that we can as well add a helper to the buffer library such as:
diff --git a/include/tst_buffers.h b/include/tst_buffers.h
index d19ac8cf0..a12d70a62 100644
--- a/include/tst_buffers.h
+++ b/include/tst_buffers.h
@@ -46,6 +46,11 @@ char *tst_strdup(const char *str);
*/
void *tst_alloc(size_t size);
+/*
+ * Strdup into a guarded buffer.
+ */
+char *tst_strdup(const char *str);
+
/*
* Allocates iovec structure including the buffers.
*
diff --git a/lib/tst_buffers.c b/lib/tst_buffers.c
index b8b597a12..4488f458e 100644
--- a/lib/tst_buffers.c
+++ b/lib/tst_buffers.c
@@ -76,6 +76,13 @@ void *tst_alloc(size_t size)
return ret + map->buf_shift;
}
+char *tst_strup(const char *str)
+{
+ char *ret = tst_alloc(strlen(str) + 1);
+
+ return strcpy(ret, str);
+}
+
static int count_iovec(int *sizes)
{
int ret = 0;
And slightly more complicated would be addition of tst_aprintf() that
would printf into buffer allocated by tst_alloc(). I will send a patch
that adds this tomorrow.
Once that is done we can use these in the test setup such as:
static char *abs_path;
static char *testfile;
static struct tcase {
int *fd;
char **filename;
int flags;
int exp_errno;
} tcases[] = {
{..., &abs_path, ...},
{..., &testfile, ...},
}
static void setup(void)
{
...
testfile = tst_strdup(TESTFILE);
abs_path = tst_aprintf(abs_path, "%s/%s", tmpdir_path, TESTFILE);
...
}
Which allocates the exact sized guarded buffers so that the canaries are
exaclty after end of the data, not at the end of the rather large
buffer.
We can't pre-allocate buffers in the tst_test structure for data whose
size is not known in advance, which is the case for the tmpdir_path.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2023-08-10 14:53 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-05 4:07 [LTP] [PATCH 1/3] include/faccessat.h: Add header file faccessat.h Yang Xu
2023-08-05 4:07 ` [LTP] [PATCH 2/3] syscalls/faccessat01: Convert to new API Yang Xu
2023-08-05 4:07 ` [LTP] [PATCH 3/3] syscalls/faccessat02: Move errnos check to faccessat02 Yang Xu
2023-08-05 4:07 ` [LTP] [PATCH 1/2] syscalls/faccessat201: Add new testcase Yang Xu
2023-08-10 14:54 ` Cyril Hrubis [this message]
2023-08-11 11:59 ` Cyril Hrubis
2023-08-22 9:50 ` Yang Xu (Fujitsu)
2023-08-22 9:45 ` Yang Xu (Fujitsu)
2023-08-05 4:08 ` [LTP] [PATCH 2/2] syscalls/faccessat202: " Yang Xu
2023-08-05 4:08 ` [LTP] [PATCH 1/2] syscalls/readlinkat01: Convert to new API Yang Xu
2023-08-05 4:08 ` [LTP] [PATCH 2/2] syscalls/readlinkat02: " Yang Xu
2023-08-10 14:29 ` [LTP] [PATCH 1/3] include/faccessat.h: Add header file faccessat.h Cyril Hrubis
2023-08-21 11:26 ` Yang Xu (Fujitsu)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZNT6GkZ05CrGk5pV@yuki \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
--cc=xuyang2018.jy@fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox