Fix dirname() handling in run_ptests()
PATCH 1/1 Fix dirname() handling in run_ptests()
It is required to use the return value of dirname() instead of assuming that dirname() modifies the input string. While certain implementations of dirname(), for example glibc, *may* modify the input string, this is not guaranteed behavior. Musl, on the other hand, does not modify the input string but instead returns a pointer to a statically allocated string.
Previously, ptest-runner would fail on musl systems:
$ ptest-runner busybox START: ptest-runner 2026-02-27T09:21 BEGIN: /usr/lib/busybox/ptest/run-ptest ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-ptest), Not a directory
ERROR: Exited from signal Killed (9)
DURATION: 0
END: /usr/lib/busybox/ptest/run-ptest
2026-02-27T09:21
STOP: ptest-runner
TOTAL: 1 FAIL: 1
--- utils.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
utils.c +9 -3
@@ -361,13 +361,19 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,fprintf (fp,"START: %s\n" , progname);PTEST_LIST_ITERATE_START (head, p) -char ptest_dir[PATH_MAX ] = {'\0' }; + char *ptest_dir; +char run_ptest[PATH_MAX ] = {'\0' };int pipefd_stdout[2 ] = {-1 ,-1 };int pipefd_stderr[2 ] = {-1 ,-1 };int pty[2 ] = {-1 ,-1 }; -strcpy (ptest_dir, p->run_ptest); -dirname (ptest_dir); +/* Copy, since dirname() may modify its input buffer */ +if (strlcpy (run_ptest, p->run_ptest,sizeof (run_ptest)) >=sizeof (run_ptest)) { +fprintf (fp,"ERROR: %s exceeds PATH_MAX\n" , p->run_ptest); + rc =-1 ; + goto ptest_list_fail1; + } + ptest_dir =dirname (run_ptest);if (pipe2 (pipefd_stdout,0 ) ==-1 ) {fprintf (fp,"ERROR: pipe2() failed with: %s.\n" ,strerror (errno));
Signature · 1 line
2.52.0
Hi Florian,
Thanks for the patch, comments below,
Can you refer me to the musl dirname(3) function that uses static allocation?.
I reviewed [1] and found that modifying the same string anyway doing the assignment is needed.
[1] https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
Quoted 36 lines · Florian Schmaus
It is required to use the return value of
dirname()instead of assuming thatdirname()modifies the input string. While certain implementations ofdirname(), for example glibc, *may* modify the input string, this is not guaranteed behavior. Musl, on the other hand, does not modify the input string but instead returns a pointer to a statically allocated string.Previously, ptest-runner would fail on musl systems:
$ ptest-runner busybox START: ptest-runner 2026-02-27T09:21 BEGIN: /usr/lib/busybox/ptest/run-ptest ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-ptest), Not a directoryERROR: Exited from signal Killed (9)
DURATION: 0
END:/usr/lib/busybox/ptest/run-ptest
2026-02-27T09:21
STOP: ptest-runner
TOTAL: 1 FAIL: 1Signed-off-by Florian Schmaus <[email protected]>--- utils.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)utils.c +0
-0@@ -361,13 +361,19 @@ run_ptests(struct ptest_list *head, const structptest_options opts, fprintf(fp, "START: %s\n", progname);PTEST_LIST_ITERATE_START (head, p)
utils.c run_ptests(struct ptest_list *head, const struct · line 371
Quoted 10 lines
utils.c +2
-3-char ptest_dir[PATH_MAX ] = {'\0' }; +char *ptest_dir; +char run_ptest[PATH_MAX ] = {'\0' };int pipefd_stdout[2 ] = {-1 ,-1 };int pipefd_stderr[2 ] = {-1 ,-1 };int pty[2 ] = {-1 ,-1 }; -strcpy (ptest_dir, p->run_ptest); -dirname (ptest_dir);
strcpy(ptest_dir, p->run_ptest); // This assumes ptest_dir and run_ptest are both PATH_MAX; perhaps improve with a secure version of strcpy? ptest_dir = dirname(ptest_dir);
Quoted 18 lines
utils.c +1
-0+/* Copy, since dirname() may modify its input buffer */
+ if (strlcpy(run_ptest,p->run_ptest,sizeof(run_ptest)) >=sizeof(run_ptest)) {
+ fprintf(fp, "ERROR: %s exceeds
PATH_MAX\n",p->run_ptest);utils.c +4
-0+ rc =-1 ; + goto ptest_list_fail1; + } + ptest_dir =dirname (run_ptest);if (pipe2 (pipefd_stdout,0 ) ==-1 ) { fprintf(fp, "ERROR: pipe2() failed with:%s.\n", strerror(errno));
--
2.52.0
Quoted 1 line · Anibal Limon
Hi Florian,
Hi Anibal,
Quoted 1 line
Thanks for the patch, comments below,
Thanks for looking at my patch.
Quoted 2 lines
Can you refer me to the musl dirname(3) function that uses static allocation?. I reviewed [1] and found that modifying the same string anyway doing the assignment is needed.
Oh, right. It's the CHERI-enable musl libc that is using a static allocation [1]. I wasn't aware that this is a deviation from upstream.
But it doesn't really matter, the current usage of dirname() is fragile and should be fixed.
Quoted 53 lines
[1] https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>
On Fri, Feb 27, 2026 at 4:09 AM Florian Schmaus <[email protected] <mailto:[email protected]>> wrote:
It is required to use the return value of dirname() instead of assuming that dirname() modifies the input string. While certain implementations of dirname(), for example glibc, *may* modify the input string, this is not guaranteed behavior. Musl, on the other hand, does not modify the input string but instead returns a pointer to a statically allocated string. Previously, ptest-runner would fail on musl systems: $ ptest-runner busybox START: ptest-runner 2026-02-27T09:21 BEGIN: /usr/lib/busybox/ptest/run-ptest ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-ptest), Not a directory ERROR: Exited from signal Killed (9) DURATION: 0 END: /usr/lib/busybox/ptest/run-ptest 2026-02-27T09:21 STOP: ptest-runner TOTAL: 1 FAIL: 1 Signed-off-by: Florian Schmaus <[email protected] <mailto:[email protected]>> --- utils.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils.c b/utils.c index 6cf7705586bc..7614dce99bd8 100644 --- a/utils.c +++ b/utils.c @@ -361,13 +361,19 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, fprintf(fp, "START: %s\n", progname); PTEST_LIST_ITERATE_START(head, p) - char ptest_dir[PATH_MAX] = {'\0'}; + char *ptest_dir; + char run_ptest[PATH_MAX] = {'\0'}; int pipefd_stdout[2] = {-1, -1}; int pipefd_stderr[2] = {-1, -1}; int pty[2] = {-1, -1}; - strcpy(ptest_dir, p->run_ptest); - dirname(ptest_dir);
Quoted 1 line
strcpy(ptest_dir, p->run_ptest); // This assumesptest_dirandrun_ptestare bothPATH_MAX; perhaps improve with a secure version of strcpy?
Not sure if I understand the comment. My patch is using strlcpy() and should be safe.
Quoted 19 lines
ptest_dir=dirname(ptest_dir);+ /* Copy, since dirname() may modify its input buffer */ + if (strlcpy(run_ptest, p->run_ptest, sizeof(run_ptest)) >= sizeof(run_ptest)) { + fprintf(fp, "ERROR: %s exceeds PATH_MAX\n", p->run_ptest); + rc = -1; + goto ptest_list_fail1; + } + ptest_dir = dirname(run_ptest); if (pipe2(pipefd_stdout, 0) == -1) { fprintf(fp, "ERROR: pipe2() failed with: %s.\n", strerror(errno)); -- 2.52.0
- Florian
1: https://git.morello-project.org/morello/musl-libc/-/blob/morello/master/src/misc/dirname.c
Quoted 79 lines · Florian Schmaus
Quoted 1 line · Anibal Limon
Hi Florian,
Hi Anibal,
Quoted 1 line
Thanks for the patch, comments below,
Thanks for looking at my patch.
Quoted 4 lines
Can you refer me to the musl dirname(3) function that uses static allocation?.
I reviewed [1] and found that modifying the same string anyway doing the assignment is needed.Oh, right. It's the CHERI-enable musl libc that is using a static allocation [1]. I wasn't aware that this is a deviation from upstream.
But it doesn't really matter, the current usage of
dirname()is fragile and should be fixed.Quoted 5 lines
[1] https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>On Fri, Feb 27, 2026 at 4:09 AM Florian Schmaus <[email protected] <mailto:[email protected]>>
Quoted 50 lines · wrote:
It is required to use the return value of
dirname()instead of assuming thatdirname()modifies the input string. While certain implementations ofdirname(), for example glibc, *may* modify the input string, this is not guaranteed behavior. Musl, on the other hand, does not modify the input string but instead returns a pointer to a statically allocated string.Previously, ptest-runner would fail on musl systems:
$ ptest-runner busybox
START: ptest-runner
2026-02-27T09:21
BEGIN:/usr/lib/busybox/ptest/run-ptest
ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-ptest), Not a directoryERROR: Exited from signal Killed (9)
DURATION: 0
END:/usr/lib/busybox/ptest/run-ptest
2026-02-27T09:21
STOP: ptest-runner
TOTAL: 1 FAIL: 1Signed-off-by Florian Schmaus <[email protected]<mailto:[email protected]>>
---
utils.c| 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)diff --git
a/utils.cb/utils.c
index6cf7705586bc..7614dce99bd8100644
---a/utils.c
+++b/utils.c
@@ -361,13 +361,19 @@ run_ptests(structptest_list*head, const structptest_optionsopts,
fprintf(fp, "START: %s\n", progname);PTEST_LIST_ITERATE_START(head, p)
- char ptest_dir[PATH_MAX] = {'\0'};
+ char*ptest_dir;
+ char run_ptest[PATH_MAX] = {'\0'};
int pipefd_stdout[2] = {-1, -1}; int pipefd_stderr[2] = {-1, -1}; int pty[2] = {-1, -1};-
strcpy(ptest_dir, p->run_ptest);
-dirname(ptest_dir);
Quoted 10 lines
Quoted 2 lines
strcpy(ptest_dir, p->run_ptest); // This assumesptest_dirandrun_ptestare bothPATH_MAX; perhaps improve with a secure version of strcpy?Not sure if I understand the comment. My patch is using
strlcpy()and should be safe.Quoted 1 line
ptest_dir=dirname(ptest_dir);
I mean making this change should be enough, plus the strcpy change to strlcpy removes the assumption about
using PATH_MAX.
Cheers!.
Anibal
Quoted 26 lines
Quoted 12 lines
+ /* Copy, since
dirname()may modify its
input buffer */
+ if (strlcpy(run_ptest,p->run_ptest,
sizeof(run_ptest)) >=sizeof(run_ptest)) {
+ fprintf(fp, "ERROR: %s exceeds
PATH_MAX\n",p->run_ptest);
+ rc = -1;
+ gotoptest_list_fail1;
+ }
+ptest_dir=dirname(run_ptest);Quoted 6 lines
if (
pipe2(pipefd_stdout, 0)== -1) {
fprintf(fp, "ERROR:pipe2()failed
with: %s.\n", strerror(errno));
--
2.52.0- Florian
1:
https://git.morello-project.org/morello/musl-libc/-/blob/morello/master/src/misc/dirname.c
Quoted 99 lines · Anibal Limon
On Fri, Feb 27, 2026 at 1:52 PM Florian Schmaus <[email protected] <mailto:[email protected]>> wrote:
On 27/02/2026 16.58, Anibal Limon wrote:
> Hi Florian,Hi Anibal,
> Thanks for the patch, comments below,
Thanks for looking at my patch.
> Can you refer me to the musl dirname(3) function that uses static > allocation?.
> I reviewed [1] and found that modifying the same string anyway
doing the
> assignment is needed.Oh, right. It's the CHERI-enable musl libc that is using a static allocation [1]. I wasn't aware that this is a deviation from upstream.
But it doesn't really matter, the current usage of
dirname()is fragile and should be fixed.> [1] https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>
> <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>>
>
> On Fri, Feb 27, 2026 at 4:09 AM Florian Schmaus
> <[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>> wrote:
>
> It is required to use the return value ofdirname()instead of > assuming thatdirname()modifies the input string. While certain > implementations ofdirname(), for example glibc, *may* modify the > input string, this is not guaranteed behavior. Musl, on the other > hand, does not modify the input string but instead returns a
pointer
> to a statically allocated string.
>
> Previously, ptest-runner would fail on musl systems:
>
> $ ptest-runner busybox
> START: ptest-runner
> 2026-02-27T09:21
> BEGIN:/usr/lib/busybox/ptest/run-ptest
> ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-ptest), Not a > directory
>
> ERROR: Exited from signal Killed (9)
> DURATION: 0
> END:/usr/lib/busybox/ptest/run-ptest
> 2026-02-27T09:21
> STOP: ptest-runner
> TOTAL: 1 FAIL: 1
>
> Signed-off-by: Florian Schmaus <[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>
> ---
> utils.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --gita/utils.cb/utils.c
> index6cf7705586bc..7614dce99bd8100644
> ---a/utils.c
> +++b/utils.c
> @@ -361,13 +361,19 @@ run_ptests(structptest_list*head, const > structptest_optionsopts,
>
>fprintf(fp, "START: %s\n", progname); >PTEST_LIST_ITERATE_START(head, p)
> - char ptest_dir[PATH_MAX] = {'\0'}; > + char*ptest_dir;
> + char run_ptest[PATH_MAX] = {'\0'}; > int pipefd_stdout[2] = {-1, -1}; > int pipefd_stderr[2] = {-1, -1}; > int pty[2] = {-1, -1};
>
> - strcpy(ptest_dir,p->run_ptest); > - dirname(ptest_dir);
>
>
>strcpy(ptest_dir, p->run_ptest); // This assumesptest_dirand
run_ptest
> are bothPATH_MAX; perhaps improve with a secure version of strcpy?Not sure if I understand the comment. My patch is using
strlcpy()and should be safe.
Quoted 4 lines
>
ptest_dir=dirname(ptest_dir);I mean making this change should be enough
That doesn't work as it would assign to an array type.
- Florian
Quoted 110 lines · Florian Schmaus
Quoted 4 lines · Anibal Limon
On Fri, Feb 27, 2026 at 1:52 PM Florian Schmaus <[email protected] <mailto:[email protected]>>
Quoted 12 lines · wrote:
On 27/02/2026 16.58, Anibal Limon wrote:
> Hi Florian,Hi Anibal,
> Thanks for the patch, comments below,
Thanks for looking at my patch.
Quoted 8 lines
> Can you refer me to the musl dirname(3) function that uses static > allocation?.
> I reviewed [1] and found that modifying the same string anyway
doing the
> assignment is needed.Oh, right. It's the CHERI-enable musl libc that is using a static allocation [1]. I wasn't aware that this is a deviation from
upstream.
Quoted 2 lines
But it doesn't really matter, the current usage of
dirname()isfragile
Quoted 7 lines
and should be fixed. > [1] https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c> > <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>>Quoted 8 lines
> > On Fri, Feb 27, 2026 at 4:09 AM Florian Schmaus > <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> wrote: > > It is required to use the return value of dirname() instead of > assuming that dirname() modifies the input string. Whilecertain
Quoted 1 line
> implementations of
dirname(), for example glibc, *may* modifythe
Quoted 1 line
> input string, this is not guaranteed behavior. Musl, on the
other
Quoted 11 lines
> hand, does not modify the input string but instead returns a
pointer
> to a statically allocated string.
>
> Previously, ptest-runner would fail on musl systems:
>
> $ ptest-runner busybox
> START: ptest-runner
> 2026-02-27T09:21
> BEGIN:/usr/lib/busybox/ptest/run-ptest
> ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-ptest), Nota
Quoted 14 lines
> directory > > ERROR: Exited from signal Killed (9) > DURATION: 0 > END: /usr/lib/busybox/ptest/run-ptest > 2026-02-27T09:21 > STOP: ptest-runner > TOTAL: 1 FAIL: 1 > > Signed-off-by: Florian Schmaus <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>>> > ---Quoted 8 lines
> utils.c | 12 +++++++++--- > 1 file changed, 9 insertions(+), 3 deletions(-) > > diff --git a/utils.c b/utils.c > index 6cf7705586bc..7614dce99bd8 100644 > --- a/utils.c > +++ b/utils.c > @@ -361,13 +361,19 @@ run_ptests(struct ptest_list *head,const
Quoted 10 lines
> struct ptest_options opts, > > fprintf(fp, "START: %s\n", progname); > PTEST_LIST_ITERATE_START(head, p) > - char ptest_dir[PATH_MAX] = {'\0'}; > + char *ptest_dir; > + char run_ptest[PATH_MAX] = {'\0'}; > int pipefd_stdout[2] = {-1, -1}; > int pipefd_stderr[2] = {-1, -1}; > int pty[2] = {-1, -1};Quoted 8 lines
> > - strcpy(ptest_dir, p->run_ptest); > - dirname(ptest_dir); > > > strcpy(ptest_dir, p->run_ptest); // This assumes ptest_dir and run_ptest > are both PATH_MAX; perhaps improve with a secure version ofstrcpy?
Quoted 6 lines
Not sure if I understand the comment. My patch is using
strlcpy()and should be safe.
Quoted 6 lines
Quoted 4 lines
> ptest_dir = dirname(ptest_dir);I mean making this change should be enough
That doesn't work as it would assign to an array type.
A possible implementation could be:
```
--- a/utils.c
+++ b/utils.c
@@ -361,13 +361,14 @@ run_ptests(struct ptest_list *head, const struct
ptest_options opts,
fprintf(fp, "START: %s\n", progname);
PTEST_LIST_ITERATE_START(head, p)
- char ptest_dir[PATH_MAX] = {'\0'};
+ char ptest_dir_tmp[PATH_MAX] = {'\0'};
+ char *ptest_dir = NULL;
int pipefd_stdout[2] = {-1, -1};
int pipefd_stderr[2] = {-1, -1};
int pty[2] = {-1, -1};
- strcpy(ptest_dir, p->run_ptest);
- dirname(ptest_dir);
+ strcpy(ptest_dir_tmp, p->run_ptest);
+ ptest_dir = dirname(ptest_dir_tmp);
if (pipe2(pipefd_stdout, 0) == -1) {
fprintf(fp, "ERROR: pipe2() failed with:
%s.\n", strerror(errno));
```Anibal
Quoted 3 lines
- Florian
Quoted 162 lines · Anibal Limon
On Fri, Feb 27, 2026 at 2:37 PM Florian Schmaus <[email protected] <mailto:[email protected]>> wrote:
On 27/02/2026 21.20, Anibal Limon wrote:
>
>
> On Fri, Feb 27, 2026 at 1:52 PM Florian Schmaus
> <[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>> wrote:
>
> On 27/02/2026 16.58, Anibal Limon wrote:
> > Hi Florian,
>
> Hi Anibal,
>
>
> > Thanks for the patch, comments below,
>
> Thanks for looking at my patch.
>
>
> > Can you refer me to the musl dirname(3) function that uses
static
> > allocation?.
> > I reviewed [1] and found that modifying the same string anyway > doing the
> > assignment is needed.
>
> Oh, right. It's the CHERI-enable musl libc that is using a static > allocation [1]. I wasn't aware that this is a deviation from
upstream.
>
> But it doesn't really matter, the current usage ofdirname()
is fragile
> and should be fixed.
>
>
> > [1] https://git.musl-libc.org/cgit/musl/tree/src/misc/
dirname.c<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>
> <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>>
> > <https://git.musl-libc.org/cgit/musl/tree/src/misc/
dirname.c<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>
> <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>>>
> >
> > On Fri, Feb 27, 2026 at 4:09 AM Florian Schmaus
> > <[email protected]
<mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>
> <mailto:[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>> wrote:
> >
> > It is required to use the return value ofdirname()
instead of
> > assuming thatdirname()modifies the input string.
While certain
> > implementations ofdirname(), for example glibc, *may*
modify the
> > input string, this is not guaranteed behavior. Musl,
on the other
> > hand, does not modify the input string but instead
returns a
> pointer
> > to a statically allocated string.
> >
> > Previously, ptest-runner would fail on musl systems:
> >
> > $ ptest-runner busybox
> > START: ptest-runner
> > 2026-02-27T09:21
> > BEGIN:/usr/lib/busybox/ptest/run-ptest
> > ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-
ptest), Not a
> > directory
> >
> > ERROR: Exited from signal Killed (9)
> > DURATION: 0
> > END:/usr/lib/busybox/ptest/run-ptest
> > 2026-02-27T09:21
> > STOP: ptest-runner
> > TOTAL: 1 FAIL: 1
> >
> > Signed-off-by: Florian Schmaus
<[email protected] <mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>
> > <mailto:[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>>
> > ---
> > utils.c | 12 +++++++++---
> > 1 file changed, 9 insertions(+), 3 deletions(-) > >
> > diff --gita/utils.cb/utils.c
> > index6cf7705586bc..7614dce99bd8100644
> > ---a/utils.c
> > +++b/utils.c
> > @@ -361,13 +361,19 @@ run_ptests(structptest_list
*head, const
> > structptest_optionsopts,
> >
> >fprintf(fp, "START: %s\n", progname); > >PTEST_LIST_ITERATE_START(head, p)> > - char ptest_dir[PATH_MAX] = {'\0'}; > > + char*ptest_dir;
> > + char run_ptest[PATH_MAX] = {'\0'}; > > int pipefd_stdout[2] = {-1, -1}; > > int pipefd_stderr[2] = {-1, -1}; > > int pty[2] = {-1, -1}; > >
> > - strcpy(ptest_dir,p->run_ptest); > > - dirname(ptest_dir);
> >
> >
> >strcpy(ptest_dir, p->run_ptest); // This assumesptest_dirand > run_ptest
> > are bothPATH_MAX; perhaps improve with a secure version
of strcpy?
>
> Not sure if I understand the comment. My patch is using
strlcpy()and
> should be safe.
>
>
>
> >ptest_dir=dirname(ptest_dir);
>
>
> I mean making this change should be enough
That doesn't work as it would assign to an array type.A possible implementation could be:
```
utils.c +4
-3@@ -361,13 +361,14 @@ run_ptests(struct ptest_list *head, const structptest_options opts, fprintf(fp, "START: %s\n", progname); PTEST_LIST_ITERATE_START(head, p) - char ptest_dir[PATH_MAX] = {'\0'}; + char ptest_dir_tmp[PATH_MAX] = {'\0'}; + char *ptest_dir = NULL; int pipefd_stdout[2] = {-1, -1}; int pipefd_stderr[2] = {-1, -1}; int pty[2] = {-1, -1}; - strcpy(ptest_dir, p->run_ptest); - dirname(ptest_dir); + strcpy(ptest_dir_tmp, p->run_ptest); + ptest_dir = dirname(ptest_dir_tmp); if (pipe2(pipefd_stdout, 0) == -1) {
Quoted 2 lines
fprintf(fp, "ERROR:
pipe2()failed
with: %s.\n", strerror(errno));
Thank you for taking the time to look into this. I noticed your patch uses strcpy() instead of strlcpy(). While I generally prefer size-limited string copies for security reasons, as the maintainer, it's ultimately your call.
- Florian
Quoted 174 lines · Florian Schmaus
Quoted 4 lines · Anibal Limon
On Fri, Feb 27, 2026 at 2:37 PM Florian Schmaus <[email protected] <mailto:[email protected]>>
Quoted 16 lines · wrote:
On 27/02/2026 21.20, Anibal Limon wrote: > > > On Fri, Feb 27, 2026 at 1:52 PM Florian Schmaus > <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> wrote: > > On 27/02/2026 16.58, Anibal Limon wrote: > > Hi Florian, > > Hi Anibal, > > > > Thanks for the patch, comments below,Quoted 8 lines
> > Thanks for looking at my patch. > > > > Can you refer me to the musl dirname(3) function that uses static > > allocation?. > > I reviewed [1] and found that modifying the same stringanyway
Quoted 4 lines
> doing the
> > assignment is needed.
>
> Oh, right. It's the CHERI-enable musl libc that is using astatic
Quoted 10 lines
> allocation [1]. I wasn't aware that this is a deviation from upstream. > > But it doesn't really matter, the current usage of dirname() is fragile > and should be fixed. > > > > [1] https://git.musl-libc.org/cgit/musl/tree/src/misc/ dirname.c <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>
Quoted 4 lines
> <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>>
> > <https://git.musl-libc.org/cgit/musl/tree/src/misc/
dirname.c<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>
Quoted 58 lines
> <https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c
<https://git.musl-libc.org/cgit/musl/tree/src/misc/dirname.c>>>
> >
> > On Fri, Feb 27, 2026 at 4:09 AM Florian Schmaus
> > <[email protected]
<mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>
> <mailto:[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>> wrote:
> >
> > It is required to use the return value ofdirname()
instead of
> > assuming thatdirname()modifies the input string.
While certain
> > implementations ofdirname(), for example glibc, *may*
modify the
> > input string, this is not guaranteed behavior. Musl,
on the other
> > hand, does not modify the input string but instead
returns a
> pointer
> > to a statically allocated string.
> >
> > Previously, ptest-runner would fail on musl systems:
> >
> > $ ptest-runner busybox
> > START: ptest-runner
> > 2026-02-27T09:21
> > BEGIN:/usr/lib/busybox/ptest/run-ptest
> > ERROR: Unable to chdir(/usr/lib/busybox/ptest/run-
ptest), Not a
> > directory
> >
> > ERROR: Exited from signal Killed (9)
> > DURATION: 0
> > END:/usr/lib/busybox/ptest/run-ptest
> > 2026-02-27T09:21
> > STOP: ptest-runner
> > TOTAL: 1 FAIL: 1
> >
> > Signed-off-by: Florian Schmaus
<[email protected] <mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>
> > <mailto:[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>>
> > ---
> >utils.c| 12 +++++++++---
> > 1 file changed, 9 insertions(+), 3 deletions(-)
> >
> > diff --gita/utils.cb/utils.c
> > index6cf7705586bc..7614dce99bd8100644
> > ---a/utils.cQuoted 8 lines
> > +++
b/utils.c
> > @@ -361,13 +361,19 @@ run_ptests(structptest_list
*head, const
> > structptest_optionsopts,
> >
> >fprintf(fp, "START: %s\n", progname);
> >PTEST_LIST_ITERATE_START(head, p)
> > - char ptest_dir[PATH_MAX] ={'\0'};
Quoted 2 lines
> > + char *ptest_dir; > > + char run_ptest[PATH_MAX] ={'\0'};
Quoted 1 line
> > int pipefd_stdout[2] = {-1,
-1};
Quoted 1 line
> > int pipefd_stderr[2] = {-1,
-1};
Quoted 3 lines
> > int pty[2] = {-1, -1}; > > > > - strcpy(ptest_dir,
p->run_ptest);Quoted 4 lines
> > - dirname(ptest_dir); > > > > > > strcpy(ptest_dir, p->run_ptest); // This assumes ptest_dirand
Quoted 39 lines
> run_ptest > > are both PATH_MAX; perhaps improve with a secure version of strcpy? > > Not sure if I understand the comment. My patch is using strlcpy() and > should be safe. > > > > > ptest_dir = dirname(ptest_dir); > > > I mean making this change should be enough That doesn't work as it would assign to an array type.A possible implementation could be:
```
utils.c +4
-3@@ -361,13 +361,14 @@ run_ptests(struct ptest_list *head, const structptest_options opts, fprintf(fp, "START: %s\n", progname); PTEST_LIST_ITERATE_START(head, p) -char ptest_dir[PATH_MAX ] = {'\0' }; +char ptest_dir_tmp[PATH_MAX ] = {'\0' }; +char *ptest_dir =NULL ;int pipefd_stdout[2 ] = {-1 ,-1 };int pipefd_stderr[2 ] = {-1 ,-1 };int pty[2 ] = {-1 ,-1 }; -strcpy (ptest_dir, p->run_ptest); -dirname (ptest_dir); +strcpy (ptest_dir_tmp, p->run_ptest); + ptest_dir =dirname (ptest_dir_tmp);
Quoted 10 lines
Quoted 3 lines
utils.c +0
-0if (pipe2 (pipefd_stdout,0 ) ==-1 ) {fprintf(fp, "ERROR:
pipe2()failed
with: %s.\n", strerror(errno));Thank you for taking the time to look into this. I noticed your patch uses
strcpy()instead ofstrlcpy(). While I generally prefer size-limited string copies for security reasons, as the maintainer, it's ultimately your call.
I'm open to changing strlcpy(), if the potential patch up + strlcpy() change works for you.
I would be glad to integrate. Let me know.
Cheers!,
Anibal
Quoted 4 lines
- Florian