Add terminating NUL after calling readlink()
PATCH 1/1 Add terminating NUL after calling readlink()
readlink() does not append a NUL byte to the target buffer. Fix it in the pidns03 and fsstress testcases to avoid wrong test results.
testcases/kernel/containers/pidns/pidns03.c +1 -1
@@ -63,7 +63,7 @@ int child_func(void *arg) }/* self is symlink to directory named after current pid number */ -if (readlink (PROCDIR "/self" , buf,sizeof (buf)) ==-1 ) { +if (SAFE_READLINK (NULL ,PROCDIR "/self" , buf,sizeof (buf)) ==-1 ) {perror ("readlink" );umount (PROCDIR );return 1 ;
testcases/kernel/fs/fsstress/fsstress.c +2 -0
@@ -1143,6 +1143,8 @@ int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz)int rval; rval =readlink (name->path, lbuf, lbufsiz); +if (rval >=0 ) + lbuf[rval] =0 ;/* add terminating NUL */ if (rval >=0 || errno !=ENAMETOOLONG )return rval;separate_pathname (name, buf, &newname);
PATCH 1/1 Add terminating NUL after calling readlink()
Hi,
Quoted 2 lines
readlink()does not append a NUL byte to the target buffer. Fix it in the pidns03 and fsstress testcases to avoid wrong test results.
<snip>
testcases/kernel/fs/fsstress/fsstress.c int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz) · line 1147
Quoted 3 lines
testcases/kernel/fs/fsstress/fsstress.c +2
-0rval =readlink (name->path, lbuf, lbufsiz); +if (rval >=0 ) + lbuf[rval] =0 ;/* add terminating NUL */
How about using '\0' and leave the comment?
Kind regards,
Petr
PATCH 1/1 Add terminating NUL after calling readlink()
In reply to Helge DellerHi!
testcases/kernel/containers/pidns/pidns03.c int child_func(void *arg) · line 69
Quoted 13 lines
testcases/kernel/containers/pidns/pidns03.c +1
-1@@ -63,7 +63,7 @@ int child_func(void *arg) }/* self is symlink to directory named after current pid number */ -if (readlink (PROCDIR "/self" , buf,sizeof (buf)) ==-1 ) { +if (SAFE_READLINK (NULL ,PROCDIR "/self" , buf,sizeof (buf)) ==-1 ) {perror ("readlink" );umount (PROCDIR );return 1 ;
SAFE_READLINK() will newer return -1 here, it will abort the test with TBROK as a exit value.
If we want to use it here it should look as:
static void child_cleanup(void)
{
umount(PROCDIR);
}...
SAFE_READLINK(child_cleanup, PROCDIR"/self", buf, sizeof(buf)); umount(PROC_DIR);
...
I also wonder if we have to umount the "/proc" in the child or if the kernel will umount it automatically once the child exits, which would allow us to simplify the child function...
testcases/kernel/fs/fsstress/fsstress.c int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz) · line 1147
Quoted 10 lines
testcases/kernel/fs/fsstress/fsstress.c +2
-0@@ -1143,6 +1143,8 @@ int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz)int rval; rval =readlink (name->path, lbuf, lbufsiz); +if (rval >=0 ) + lbuf[rval] =0 ;/* add terminating NUL */
Shouldn't we check here that the rval < lbufsiz? Or pass lbufsiz - 1 to the readlink() call?
Quoted 3 lines
testcases/kernel/fs/fsstress/fsstress.c +0
-0if (rval >=0 || errno !=ENAMETOOLONG )return rval;separate_pathname (name, buf, &newname);
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 Add terminating NUL after calling readlink()
Quoted 8 lines · Cyril Hrubis
Hi!
Quoted 7 lines
testcases/kernel/containers/pidns/pidns03.c +0
-0@@ -63,7 +63,7 @@ int child_func(void *arg) }
Quoted 9 lines
testcases/kernel/containers/pidns/pidns03.c int child_func(void *arg) · line 69
Quoted 6 lines
testcases/kernel/containers/pidns/pidns03.c +1
-1/* self is symlink to directory named after current pid number */ -if (readlink (PROCDIR "/self" , buf,sizeof (buf)) ==-1 ) { +if (SAFE_READLINK (NULL ,PROCDIR "/self" , buf,sizeof (buf)) ==-1 ) {perror ("readlink" );umount (PROCDIR );return 1 ;
SAFE_READLINK()will newer return -1 here, it will abort the test with TBROK as a exit value.
Ah, right.
Quoted 4 lines
...
I also wonder if we have to umount the "/proc" in the child or if the kernel will umount it automatically once the child exits,
No, this doesn't happen.
Quoted 2 lines
which would
allow us to simplify the child function...
It's probably best to stick with readlink() instead of SAFE_READLINK(). Here is my new proposal, I'll send a full patch in a few moments:
- if (readlink(PROCDIR"/self", buf, sizeof(buf)) == -1) {
+ r = readlink(PROCDIR"/self", buf, sizeof(buf)-1);
+ if (r == -1) {
perror("readlink");
umount(PROCDIR);
return 1;- }
+ } else
+ buf[r] = '\0';
Quoted 7 lines
Quoted 7 lines
testcases/kernel/fs/fsstress/fsstress.c +0
-0@@ -1143,6 +1143,8 @@ int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz)int rval;
Quoted 6 lines
testcases/kernel/fs/fsstress/fsstress.c int readlink_path(pathname_t * name, char *lbuf, size_t lbufsiz) · line 1147
Quoted 3 lines
testcases/kernel/fs/fsstress/fsstress.c +2
-0rval =readlink (name->path, lbuf, lbufsiz); +if (rval >=0 ) + lbuf[rval] =0 ;/* add terminating NUL */ Shouldn't we check here that the rval < lbufsiz? Or pass lbufsiz - 1 to the
readlink()call?
I like the latter, and will change adding the NUL to:
buf[r] = '\0';
(as suggested by Petr Vorel <[email protected]>).
Will send new patch...
Thanks,
Helge