tst_tmpdir: Fix buffer overflow in tst_tmpdir.c
PATCH 1/1 tst_tmpdir: Fix buffer overflow in tst_tmpdir.c
Using sprintf without length checking in tst_tmpdir may lead to buffer overflow. So in this patch use openat() instead of open().
Fixs:1241
--- lib/tst_tmpdir.c | 119 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 8 deletions(-)
lib/tst_tmpdir.c +111 -8
@@ -102,6 +102,7 @@ static char test_start_work_dir[PATH_MAX];extern futex_t *tst_futexes;static int rmobj (const char *obj,char **errmsg); +static int rmobjat (int dir_fd,const char *obj,char **errmsg);int tst_tmpdir_created (void ) {
@@ -149,8 +150,8 @@ static int purge_dir(const char *path, char **errptr)int ret_val =0 ;DIR *dir;struct dirent *dir_ent; -char dirobj[PATH_MAX ];static char err_msg[PATH_MAX +1280 ]; +int dir_fd =-1 ;/* Do NOT perform the request if the directory is "/" */ if (!strcmp (path,"/" )) {
@@ -167,7 +168,7 @@ static int purge_dir(const char *path, char **errptr)/* Open the directory to get access to what is in it */ if (!(dir =opendir (path))) {if (errptr) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"Cannot open directory %s; errno=%d: %s" , path, errno,tst_strerrno (errno)); *errptr = err_msg;
@@ -175,6 +176,18 @@ static int purge_dir(const char *path, char **errptr)return -1 ; } + dir_fd =dirfd (dir); +if (dir_fd ==-1 ) { +closedir (dir); +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot get file descriptor for directory %s; errno=%d: %s" , + path, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } +/* Loop through the entries in the directory, removing each one */ for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) {/* Don't remove "." or ".." */
@@ -183,8 +196,57 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (dir_fd, dir_ent->d_name, errptr) !=0 ) + ret_val =-1 ; + } + +closedir (dir); +return ret_val; +} + +static int purge_dirat (int dir_fd,const char *path,char **errptr) +{ +int ret_val =0 ; +DIR *dir; +struct dirent *dir_ent; +static char err_msg[PATH_MAX +1280 ]; +int subdir_fd; + + errno =0 ; + +/* Open the subdirectory using openat */ + subdir_fd =openat (dir_fd, path,O_RDONLY |O_DIRECTORY |O_NOFOLLOW ); +if (subdir_fd <0 ) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open subdirectory %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } + + dir =fdopendir (subdir_fd); +if (!dir) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open directory stream for %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +close (subdir_fd); +return -1 ; + } + +/* Loop through the entries in the directory, removing each one */ +for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) { +/* Don't remove "." or ".." */ +if (!strcmp (dir_ent->d_name,"." ) + || !strcmp (dir_ent->d_name,".." )) +continue ; + +/* Recursively remove the current entry */ +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }
@@ -212,7 +274,7 @@ static int rmobj(const char *obj, char **errmsg)/* Get the link count, now that all the entries have been removed */ if (lstat (obj, &statbuf) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"lstat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;
@@ -225,7 +287,7 @@ static int rmobj(const char *obj, char **errmsg)/* The directory is linked; unlink() must be used */ if (unlink (obj) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"unlink(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;
@@ -236,7 +298,7 @@ static int rmobj(const char *obj, char **errmsg)/* The directory is not linked; remove() can be used */ if (remove (obj) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"remove(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;
@@ -247,7 +309,7 @@ static int rmobj(const char *obj, char **errmsg) }else {if (unlink (obj) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"unlink(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;
@@ -259,6 +321,47 @@ static int rmobj(const char *obj, char **errmsg)return 0 ; } +static int rmobjat (int dir_fd,const char *obj,char **errmsg) +{ +int ret_val =0 ; +struct stat statbuf; +static char err_msg[PATH_MAX +1280 ]; +int fd; + + fd =openat (dir_fd, obj,O_DIRECTORY |O_NOFOLLOW ); +if (fd >=0 ) { +close (fd); + ret_val =purge_dirat (dir_fd, obj, errmsg); + +/* If there were problems removing an entry, don't attempt to +remove the directory itself */ +if (ret_val ==-1 ) +return -1 ; + +if (unlinkat (dir_fd, obj,AT_REMOVEDIR ) <0 ) { +if (errmsg !=NULL ) { +snprintf (err_msg,sizeof (err_msg), +"remove(%s) failed; errno=%d: %s" , + obj, errno,tst_strerrno (errno)); + *errmsg = err_msg; + } +return -1 ; + } + }else { +if (unlinkat (dir_fd, obj,0 ) <0 ) { +if (errmsg !=NULL ) { +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, + errno,tst_strerrno (errno)); + *errmsg = err_msg; + } +return -1 ; + } + } + +return 0 ; +} +void tst_tmpdir (void ) {char template[PATH_MAX ];
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei, all,
@all: is this a candidate for a release? It'd be nice to get it fixed.
Quoted 2 lines
Using sprintf without length checking in
tst_tmpdirmay lead to buffer overflow. So in this patch useopenat()instead ofopen().
LGTM, but it'd be nice if we could use only rmobjat().
Could you please remove the unused variable?
tst_tmpdir.c: In function ‘rmobjat’:
tst_tmpdir.c:327:21: warning: unused variable ‘statbuf’ [-Wunused-variable]
327 | struct stat statbuf;
| ^~~~~~~Quoted 1 line
Fixs:1241
This is better as it shows link in GitHub web:
Fixes: #1241
Or, IMHO better
Fixes: https://github.com/linux-test-project/ltp/issues/1241
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 4 lines · Petr Vorel
Hi Wei, all,
@all: is this a candidate for a release? It'd be nice to get it fixed.
I'd wait after release, it's not a trivial change and other than static analysis there are no reports of it actually happening. And it also allows more time for review.
Quoted 27 lines
Quoted 3 lines
Using sprintf without length checking in
tst_tmpdirmay lead to buffer overflow. So in this patch useopenat()instead ofopen().LGTM, but it'd be nice if we could use only
rmobjat().Could you please remove the unused variable?
tst_tmpdir.c: In function ‘rmobjat’:
tst_tmpdir.c:327:21: warning: unused variable ‘statbuf’ [-Wunused-variable]327 | struct stat statbuf; | ^~~~~~~Suggested-by Cyril Hrubis <[email protected]>Quoted 1 line
Fixs:1241
This is better as it shows link in GitHub web:
Fixes: #1241Or, IMHO better
Fixes: https://github.com/linux-test-project/ltp/issues/1241Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Jan, all,
Quoted 9 lines · Petr Vorel
Quoted 4 lines
Hi Wei, all,
@all: is this a candidate for a release? It'd be nice to get it fixed.
I'd wait after release, it's not a trivial change and other than static analysis there are no reports of it actually happening. And it also allows more time for review.
Makes sense.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 5 lines
Quoted 1 line
@all: is this a candidate for a release? It'd be nice to get it fixed.
I'd wait after release, it's not a trivial change and other than static analysis there are no reports of it actually happening. And it also allows more time for review.
Agree here.
I guess that it could be triggered if we pass long enough TMPDIR= to the tests, but for changes like this we need a proper testing phase and we are too close to the release for that to happen.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 tst_tmpdir: Fix buffer overflow in tst_tmpdir.c
In reply to Wei Gao via ltpUsing sprintf without length checking in tst_tmpdir may lead to buffer overflow. So in this patch use openat() instead of open().
--- lib/tst_tmpdir.c | 122 ++++++++++++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 44 deletions(-)
lib/tst_tmpdir.c +78 -44
@@ -101,7 +101,7 @@ static char test_start_work_dir[PATH_MAX];/* lib/tst_checkpoint.c */ extern futex_t *tst_futexes; -static int rmobj (const char *obj,char **errmsg); +static int rmobjat (int dir_fd,const char *obj,char **errmsg);int tst_tmpdir_created (void ) {
@@ -149,8 +149,8 @@ static int purge_dir(const char *path, char **errptr)int ret_val =0 ;DIR *dir;struct dirent *dir_ent; -char dirobj[PATH_MAX ];static char err_msg[PATH_MAX +1280 ]; +int dir_fd =-1 ;/* Do NOT perform the request if the directory is "/" */ if (!strcmp (path,"/" )) {
@@ -167,7 +167,7 @@ static int purge_dir(const char *path, char **errptr)/* Open the directory to get access to what is in it */ if (!(dir =opendir (path))) {if (errptr) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"Cannot open directory %s; errno=%d: %s" , path, errno,tst_strerrno (errno)); *errptr = err_msg;
@@ -175,6 +175,68 @@ static int purge_dir(const char *path, char **errptr)return -1 ; } + dir_fd =dirfd (dir); +if (dir_fd ==-1 ) { +closedir (dir); +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot get file descriptor for directory %s; errno=%d: %s" , + path, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } + +/* Loop through the entries in the directory, removing each one */ +for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) { +/* Don't remove "." or ".." */ +if (!strcmp (dir_ent->d_name,"." ) + || !strcmp (dir_ent->d_name,".." )) +continue ; + +/* Recursively remove the current entry */ +if (rmobjat (dir_fd, dir_ent->d_name, errptr) !=0 ) + ret_val =-1 ; + } + +closedir (dir); +return ret_val; +} + +static int purge_dirat (int dir_fd,const char *path,char **errptr) +{ +int ret_val =0 ; +DIR *dir; +struct dirent *dir_ent; +static char err_msg[PATH_MAX +1280 ]; +int subdir_fd; + + errno =0 ; + +/* Open the subdirectory using openat */ + subdir_fd =openat (dir_fd, path,O_RDONLY |O_DIRECTORY |O_NOFOLLOW ); +if (subdir_fd <0 ) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open subdirectory %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } + + dir =fdopendir (subdir_fd); +if (!dir) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open directory stream for %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +close (subdir_fd); +return -1 ; + } +/* Loop through the entries in the directory, removing each one */ for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) {/* Don't remove "." or ".." */
@@ -183,8 +245,7 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }
@@ -192,63 +253,36 @@ static int purge_dir(const char *path, char **errptr)return ret_val; } -static int rmobj (const char *obj,char **errmsg) +static int rmobjat (int dir_fd,const char *obj,char **errmsg) {int ret_val =0 ; -struct stat statbuf;static char err_msg[PATH_MAX +1280 ];int fd; - fd =open (obj,O_DIRECTORY |O_NOFOLLOW ); + fd =openat (dir_fd, obj,O_DIRECTORY |O_NOFOLLOW );if (fd >=0 ) {close (fd); - ret_val =purge_dir (obj, errmsg); + ret_val =purge_dirat (dir_fd, obj, errmsg);/* If there were problems removing an entry, don't attempt to remove the directory itself */ if (ret_val ==-1 )return -1 ; -/* Get the link count, now that all the entries have been removed */ -if (lstat (obj, &statbuf) <0 ) { +if (unlinkat (dir_fd, obj,AT_REMOVEDIR ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"lstat(%s) failed; errno=%d: %s" , obj, - errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; - } - -/* Remove the directory itself */ -if (statbuf.st_nlink >=3 ) { -/* The directory is linked; unlink() must be used */ -if (unlink (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , - obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; - } - }else { -/* The directory is not linked; remove() can be used */ -if (remove (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"remove(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; + *errmsg = err_msg; } +return -1 ; } }else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }
@@ -305,7 +339,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
@@ -343,7 +377,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 123 lines
Using sprintf without length checking in
tst_tmpdirmay lead to buffer overflow. So in this patch useopenat()instead ofopen().Suggested-by Cyril Hrubis <[email protected]>Signed-off-by Wei Gao <[email protected]>--- lib/tst_tmpdir.c | 122 ++++++++++++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 44 deletions(-)lib/tst_tmpdir.c +3
-3@@ -101,7 +101,7 @@ static char test_start_work_dir[PATH_MAX];/* lib/tst_checkpoint.c */ extern futex_t *tst_futexes; -static int rmobj (const char *obj,char **errmsg); +static int rmobjat (int dir_fd,const char *obj,char **errmsg);int tst_tmpdir_created (void ) {@@ -149,8 +149,8 @@ static int purge_dir(const char *path, char **errptr)int ret_val =0 ;DIR *dir;struct dirent *dir_ent; -char dirobj[PATH_MAX ];static char err_msg[PATH_MAX +1280 ]; +int dir_fd =-1 ;/* Do NOT perform the request if the directory is "/" */ if (!strcmp (path,"/" )) {@@ -167,7 +167,7 @@ static int purge_dir(const char *path, char **errptr)/* Open the directory to get access to what is in it */ if (!(dir =opendir (path))) {if (errptr) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"Cannot open directory %s; errno=%d: %s" , path, errno,tst_strerrno (errno));lib/tst_tmpdir.c +63
-2*errptr = err_msg;@@ -175,6 +175,68 @@ static int purge_dir(const char *path, char **errptr)return -1 ; } + dir_fd =dirfd (dir); +if (dir_fd ==-1 ) { +closedir (dir); +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot get file descriptor for directory %s; errno=%d: %s" , + path, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } + +/* Loop through the entries in the directory, removing each one */ +for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) { +/* Don't remove "." or ".." */ +if (!strcmp (dir_ent->d_name,"." ) + || !strcmp (dir_ent->d_name,".." )) +continue ; + +/* Recursively remove the current entry */ +if (rmobjat (dir_fd, dir_ent->d_name, errptr) !=0 ) + ret_val =-1 ; + } + +closedir (dir); +return ret_val; +} + +static int purge_dirat (int dir_fd,const char *path,char **errptr) +{ +int ret_val =0 ; +DIR *dir; +struct dirent *dir_ent; +static char err_msg[PATH_MAX +1280 ]; +int subdir_fd; + + errno =0 ; + +/* Open the subdirectory using openat */ + subdir_fd =openat (dir_fd, path,O_RDONLY |O_DIRECTORY |O_NOFOLLOW ); +if (subdir_fd <0 ) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open subdirectory %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } + + dir =fdopendir (subdir_fd); +if (!dir) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open directory stream for %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +close (subdir_fd); +return -1 ; + } +/* Loop through the entries in the directory, removing each one */ for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) {/* Don't remove "." or ".." */ @@ -183,8 +245,7 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 254
Quoted 2 lines
lib/tst_tmpdir.c +0
-0@@ -192,63 +253,36 @@ static int purge_dir(const char *path, char **errptr)return ret_val; }
Again, there shouldn't be two function for the same job. There should be only purge_dirat() and the tst_purge_dir() should call purge_dirat() with AT_FDCWD as the dirfd.
Quoted 49 lines
lib/tst_tmpdir.c +4
-31-static int rmobj (const char *obj,char **errmsg) +static int rmobjat (int dir_fd,const char *obj,char **errmsg) {int ret_val =0 ; -struct stat statbuf;static char err_msg[PATH_MAX +1280 ];int fd; - fd =open (obj,O_DIRECTORY |O_NOFOLLOW ); + fd =openat (dir_fd, obj,O_DIRECTORY |O_NOFOLLOW );if (fd >=0 ) {close (fd); - ret_val =purge_dir (obj, errmsg); + ret_val =purge_dirat (dir_fd, obj, errmsg);/* If there were problems removing an entry, don't attempt to remove the directory itself */ if (ret_val ==-1 )return -1 ; -/* Get the link count, now that all the entries have been removed */ -if (lstat (obj, &statbuf) <0 ) { +if (unlinkat (dir_fd, obj,AT_REMOVEDIR ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"lstat(%s) failed; errno=%d: %s" , obj, - errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; - } - -/* Remove the directory itself */ -if (statbuf.st_nlink >=3 ) { -/* The directory is linked; unlink() must be used */ -if (unlink (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , - obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; - } - }else { -/* The directory is not linked; remove() can be used */ -if (remove (obj) <0 ) { -if (errmsg !=NULL ) { - sprintf(err_msg,
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 280
Quoted 10 lines
lib/tst_tmpdir.c +3
-3+snprintf (err_msg,sizeof (err_msg),"remove(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; + *errmsg = err_msg; } +return -1 ; }
I do not think that we can remove the part where we check the number of links to the directory. I suppose that the new code should look like:
int flags = AT_REMOVEDIR;
if (lstatat(dir_fd, &statbuf)) {
...
}
if (statbuf.st_nlink >= 3)
flags = 0;
if (unlinkat(dir_fd, obj, flags)) {
...
}Quoted 21 lines
lib/tst_tmpdir.c +4
-4}else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }@@ -305,7 +339,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
lib/tst_tmpdir.c void tst_rmdir(void) · line 383
Quoted 8 lines
lib/tst_tmpdir.c +1
-1@@ -343,7 +377,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
We should pass AT_FDCWD to the two rmobjat() here since it's possible to pass relative path in the TMPDIR environment variable. Otherwise the code will not work with e.g. TMPDIR="." ./test_foo
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi all,
...
Quoted 7 lines
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 254
Quoted 3 lines
lib/tst_tmpdir.c +0
-0@@ -192,63 +253,36 @@ static int purge_dir(const char *path, char **errptr)return ret_val; }Again, there shouldn't be two function for the same job. There should be only
purge_dirat()and thetst_purge_dir()should callpurge_dirat()withAT_FDCWDas the dirfd.
+1. That is what I meant by "it'd be nice if we could use only rmobjat()" in v1.
Quoted 25 lines
Quoted 25 lines
lib/tst_tmpdir.c +4
-5}else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }@@ -305,7 +339,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }@@ -343,7 +377,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) {
Quoted 8 lines
lib/tst_tmpdir.c void tst_tmpdir(void) · line 345
Quoted 4 lines
lib/tst_tmpdir.c +1
-0+if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }We should pass
AT_FDCWDto the twormobjat()here since it's possible to pass relative path in the TMPDIR environment variable. Otherwise the code will not work with e.g. TMPDIR="." ./test_foo
Very good catch. BTW we expect TMPDIR to be correct - without double quotes and trailing '/' which are example for shell tests which lead to failures in LTP NFS tests. That was fixed in:
273c497935 ("tst_test.sh: Remove possible double/trailing slashes from TMPDIR")
Wouldn't be better just to normalize relative TMPDIR to absolute path? Simple realpath() would do the job, right?
e.g. this patch "swapon03: Remove grep dependency" [1] + fix attempt to swapoff leftover from previous run (when one does ctrl+C in previous run) expect that TMPDIR is absolute. I'll note it below the patch that either we change lib/tst_tmpdir.c to convert relative to absolute, or swapon03.c test needs to do it itself. I would prefer lib/tst_tmpdir.c do the job including normalizing the path (more tests will benefit/need it).
Kind regards,
Petr
[1] https://lore.kernel.org/ltp/[email protected]/
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 56 lines · Petr Vorel
Hi all,
...
Quoted 7 lines
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 254
Quoted 3 lines
lib/tst_tmpdir.c +0
-0@@ -192,63 +253,36 @@ static int purge_dir(const char *path, char **errptr)return ret_val; }Again, there shouldn't be two function for the same job. There should be only
purge_dirat()and thetst_purge_dir()should callpurge_dirat()withAT_FDCWDas the dirfd.+1. That is what I meant by "it'd be nice if we could use only
rmobjat()" in v1.Quoted 25 lines
Quoted 25 lines
lib/tst_tmpdir.c +4
-5}else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }@@ -305,7 +339,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }@@ -343,7 +377,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) {Quoted 8 lines
lib/tst_tmpdir.c void tst_tmpdir(void) · line 345
Quoted 4 lines
lib/tst_tmpdir.c +1
-0+if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }We should pass
AT_FDCWDto the twormobjat()here since it's possible to pass relative path in the TMPDIR environment variable. Otherwise the code will not work with e.g. TMPDIR="." ./test_fooVery good catch. BTW we expect TMPDIR to be correct - without double quotes and trailing '/' which are example for shell tests which lead to failures in LTP NFS tests. That was fixed in:
273c497935("tst_test.sh: Remove possible double/trailing slashes from TMPDIR")Wouldn't be better just to normalize relative TMPDIR to absolute path? Simple
realpath()would do the job, right?
Quoted 6 lines
e.g. this patch "swapon03: Remove grep dependency" [1] + fix attempt to swapoff leftover from previous run (when one does ctrl+C in previous run) expect that TMPDIR is absolute. I'll note it below the patch that either we change
lib/tst_tmpdir.cto convert relative to absolute, orswapon03.ctest needs to do it itself. I would preferlib/tst_tmpdir.cdo the job including normalizing the path (more tests will benefit/need it).
Also i guess we need use another patch for this?
Quoted 6 lines
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 33 lines · Cyril Hrubis
Hi!
Quoted 32 lines
Using sprintf without length checking in
tst_tmpdirmay lead to buffer overflow. So in this patch useopenat()instead ofopen().Suggested-by Cyril Hrubis <[email protected]>Signed-off-by Wei Gao <[email protected]>lib/tst_tmpdir.c +4
-5}else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }@@ -305,7 +339,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }@@ -343,7 +377,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) {
Quoted 9 lines
lib/tst_tmpdir.c void tst_tmpdir(void) · line 345
Quoted 4 lines
lib/tst_tmpdir.c +1
-0+if (rmobjat (0 ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }We should pass
AT_FDCWDto the twormobjat()here since it's possible to pass relative path in the TMPDIR environment variable. Otherwise the code will not work with e.g. TMPDIR="." ./test_foo
My test show we never support TMPDIR=".", following error will show:
tst_tmpdir.c:135: TBROK: You must specify an absolute pathname for environment variable TMPDIR I guess we need another patch enable this.
Quoted 3 lines
Signature · 2 lines
Cyril Hrubis [email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 1/1 tst_tmpdir: Fix buffer overflow in tst_tmpdir.c
In reply to Wei Gao via ltpUsing sprintf without length checking in tst_tmpdir may lead to buffer overflow. So in this patch use openat() instead of open().
--- lib/tst_tmpdir.c | 84 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 46 deletions(-)
lib/tst_tmpdir.c +38 -46
@@ -101,7 +101,7 @@ static char test_start_work_dir[PATH_MAX];/* lib/tst_checkpoint.c */ extern futex_t *tst_futexes; -static int rmobj (const char *obj,char **errmsg); +static int rmobjat (int dir_fd,const char *obj,char **errmsg);int tst_tmpdir_created (void ) {
@@ -144,34 +144,37 @@ const char *tst_get_startwd(void)return test_start_work_dir; } -static int purge_dir (const char *path,char **errptr) +static int purge_dirat (int dir_fd,const char *path,char **errptr) {int ret_val =0 ;DIR *dir;struct dirent *dir_ent; -char dirobj[PATH_MAX ];static char err_msg[PATH_MAX +1280 ]; +int subdir_fd; -/* Do NOT perform the request if the directory is "/" */ -if (!strcmp (path,"/" )) { + errno =0 ; + +/* Open the subdirectory using openat */ + subdir_fd =openat (dir_fd, path,O_RDONLY |O_DIRECTORY |O_NOFOLLOW ); +if (subdir_fd <0 ) {if (errptr) { -strcpy (err_msg,"Cannot purge system root directory" ); +snprintf (err_msg,sizeof (err_msg), +"Cannot open subdirectory %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); *errptr = err_msg; } -return -1 ; } - errno =0 ; - -/* Open the directory to get access to what is in it */ -if (!(dir =opendir (path))) { + dir =fdopendir (subdir_fd); +if (!dir) {if (errptr) { -sprintf (err_msg, -"Cannot open directory %s; errno=%d: %s" , - path, errno,tst_strerrno (errno)); +snprintf (err_msg,sizeof (err_msg), +"Cannot open directory stream for %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); *errptr = err_msg; } +close (subdir_fd);return -1 ; }
@@ -183,8 +186,7 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }
@@ -192,25 +194,27 @@ static int purge_dir(const char *path, char **errptr)return ret_val; } -static int rmobj (const char *obj,char **errmsg) +static int rmobjat (int dir_fd,const char *obj,char **errmsg) {int ret_val =0 ;struct stat statbuf;static char err_msg[PATH_MAX +1280 ];int fd; - fd =open (obj,O_DIRECTORY |O_NOFOLLOW ); + fd =openat (dir_fd, obj,O_DIRECTORY |O_NOFOLLOW );if (fd >=0 ) {close (fd); - ret_val =purge_dir (obj, errmsg); + ret_val =purge_dirat (dir_fd, obj, errmsg);/* If there were problems removing an entry, don't attempt to remove the directory itself */ if (ret_val ==-1 )return -1 ; +int flags =AT_REMOVEDIR ; +/* Get the link count, now that all the entries have been removed */ -if (lstat (obj, &statbuf) <0 ) { +if (fstatat (dir_fd, obj, &statbuf,AT_SYMLINK_NOFOLLOW ) <0 ) {if (errmsg !=NULL ) { sprintf(err_msg, "lstat(%s) failed; errno=%d: %s", obj,
@@ -220,35 +224,23 @@ static int rmobj(const char *obj, char **errmsg)return -1 ; } -/* Remove the directory itself */ -if (statbuf.st_nlink >=3 ) { -/* The directory is linked; unlink() must be used */ -if (unlink (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , - obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; - } - }else { -/* The directory is not linked; remove() can be used */ -if (remove (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, +if (statbuf.st_nlink >=3 ) + flags =0 ; + +if (unlinkat (dir_fd, obj, flags) <0 ) { +if (errmsg !=NULL ) { +snprintf (err_msg,sizeof (err_msg),"remove(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; + *errmsg = err_msg; } +return -1 ; } }else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }
@@ -305,7 +297,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (AT_FDCWD ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
@@ -343,7 +335,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (AT_FDCWD ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
@@ -353,7 +345,7 @@ void tst_purge_dir(const char *path) {char *err; -if (purge_dir (path, &err)) +if (purge_dirat (AT_FDCWD , path, &err))tst_brkm (TBROK ,NULL ,"%s: %s" , __func__, err); }
Signature · 5 lines
2.51.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 220
Quoted 2 lines
lib/tst_tmpdir.c +0
-0sprintf(err_msg, "lstat(%s) failed; errno=%d: %s", obj,
Here we are still using sprintf().
Signature · 7 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 1/1 tst_tmpdir: Fix buffer overflow in tst_tmpdir.c
In reply to Wei Gao via ltpUsing sprintf without length checking in tst_tmpdir may lead to buffer overflow. So in this patch use openat() instead of open().
--- lib/tst_tmpdir.c | 88 ++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 48 deletions(-)
lib/tst_tmpdir.c +40 -48
@@ -101,7 +101,7 @@ static char test_start_work_dir[PATH_MAX];/* lib/tst_checkpoint.c */ extern futex_t *tst_futexes; -static int rmobj (const char *obj,char **errmsg); +static int rmobjat (int dir_fd,const char *obj,char **errmsg);int tst_tmpdir_created (void ) {
@@ -144,34 +144,37 @@ const char *tst_get_startwd(void)return test_start_work_dir; } -static int purge_dir (const char *path,char **errptr) +static int purge_dirat (int dir_fd,const char *path,char **errptr) {int ret_val =0 ;DIR *dir;struct dirent *dir_ent; -char dirobj[PATH_MAX ];static char err_msg[PATH_MAX +1280 ]; +int subdir_fd; -/* Do NOT perform the request if the directory is "/" */ -if (!strcmp (path,"/" )) { + errno =0 ; + +/* Open the subdirectory using openat */ + subdir_fd =openat (dir_fd, path,O_RDONLY |O_DIRECTORY |O_NOFOLLOW ); +if (subdir_fd <0 ) {if (errptr) { -strcpy (err_msg,"Cannot purge system root directory" ); +snprintf (err_msg,sizeof (err_msg), +"Cannot open subdirectory %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); *errptr = err_msg; } -return -1 ; } - errno =0 ; - -/* Open the directory to get access to what is in it */ -if (!(dir =opendir (path))) { + dir =fdopendir (subdir_fd); +if (!dir) {if (errptr) { -sprintf (err_msg, -"Cannot open directory %s; errno=%d: %s" , - path, errno,tst_strerrno (errno)); +snprintf (err_msg,sizeof (err_msg), +"Cannot open directory stream for %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); *errptr = err_msg; } +close (subdir_fd);return -1 ; }
@@ -183,8 +186,7 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }
@@ -192,63 +194,53 @@ static int purge_dir(const char *path, char **errptr)return ret_val; } -static int rmobj (const char *obj,char **errmsg) +static int rmobjat (int dir_fd,const char *obj,char **errmsg) {int ret_val =0 ;struct stat statbuf;static char err_msg[PATH_MAX +1280 ];int fd; - fd =open (obj,O_DIRECTORY |O_NOFOLLOW ); + fd =openat (dir_fd, obj,O_DIRECTORY |O_NOFOLLOW );if (fd >=0 ) {close (fd); - ret_val =purge_dir (obj, errmsg); + ret_val =purge_dirat (dir_fd, obj, errmsg);/* If there were problems removing an entry, don't attempt to remove the directory itself */ if (ret_val ==-1 )return -1 ; +int flags =AT_REMOVEDIR ; +/* Get the link count, now that all the entries have been removed */ -if (lstat (obj, &statbuf) <0 ) { +if (fstatat (dir_fd, obj, &statbuf,AT_SYMLINK_NOFOLLOW ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"lstat(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"fstatat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }return -1 ; } -/* Remove the directory itself */ -if (statbuf.st_nlink >=3 ) { -/* The directory is linked; unlink() must be used */ -if (unlink (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , - obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; - } - }else { -/* The directory is not linked; remove() can be used */ -if (remove (obj) <0 ) { -if (errmsg !=NULL ) { -sprintf (err_msg, +if (statbuf.st_nlink >=3 ) + flags =0 ; + +if (unlinkat (dir_fd, obj, flags) <0 ) { +if (errmsg !=NULL ) { +snprintf (err_msg,sizeof (err_msg),"remove(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); - *errmsg = err_msg; - } -return -1 ; + *errmsg = err_msg; } +return -1 ; } }else { -if (unlink (obj) <0 ) { +if (unlinkat (dir_fd, obj,0 ) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, -"unlink(%s) failed; errno=%d: %s" , obj, +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg; }
@@ -305,7 +297,7 @@ void tst_tmpdir(void)tst_resm (TERRNO ,"%s: chdir(%s) failed" , __func__,TESTDIR );/* Try to remove the directory */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (AT_FDCWD ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
@@ -343,7 +335,7 @@ void tst_rmdir(void)/* * Attempt to remove the "TESTDIR" directory, using rmobj(). */ -if (rmobj (TESTDIR , &errmsg) ==-1 ) { +if (rmobjat (AT_FDCWD ,TESTDIR , &errmsg) ==-1 ) {tst_resm (TWARN ,"%s: rmobj(%s) failed: %s" , __func__,TESTDIR , errmsg); }
@@ -353,7 +345,7 @@ void tst_purge_dir(const char *path) {char *err; -if (purge_dir (path, &err)) +if (purge_dirat (AT_FDCWD , path, &err))tst_brkm (TBROK ,NULL ,"%s: %s" , __func__, err); }
Signature · 5 lines
2.52.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 233
Quoted 4 lines
lib/tst_tmpdir.c +3
-0+if (unlinkat (dir_fd, obj, flags) <0 ) { +if (errmsg !=NULL ) { + snprintf(err_msg, sizeof(err_msg), "remove(%s) failed; errno=%d: %s",
There's still one more issue in here:
We are checking the error for unlinkat, so error message should not mention remove(%s). But this can be fixed before merge.
Otherwise LGTM
Signature · 7 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 6 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 191
Quoted 9 lines
lib/tst_tmpdir.c +1
-2@@ -183,8 +186,7 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }
Shouldn't we close(subdir_fd) here as well?
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 196
Quoted 4 lines
lib/tst_tmpdir.c +0
-0@@ -192,63 +194,53 @@ static int purge_dir(const char *path, char **errptr)return ret_val; }
The rest seems to be fine.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 4 lines · Cyril Hrubis
Hi!
Quoted 3 lines
(quoted diff) +0
-0@@ -183,8 +186,7 @@ static int purge_dir(const char *path, char **errptr) continue;
Quoted 8 lines
Review static int purge_dir(const char *path, char **errptr) · line 191
Quoted 6 lines
(quoted diff) +1
-2/* Recursively remove the current entry */ - sprintf(dirobj, "%s/%s", path, dir_ent->d_name); - if (rmobj(dirobj, errptr) != 0) + if (rmobjat(subdir_fd, dir_ent->d_name, errptr) != 0) ret_val = -1; }Shouldn't we
close(subdir_fd)here as well?
I guess you mean following change? Then i think adding close(subdir_fd) inside loop will actually cause a double-close, since after for loop there is another closedir(dir) will be called. closedir will also try closes underlying file descriptor. Correct me if any mistake, thanks.
lib/tst_tmpdir.c +3 -1
@@ -186,8 +186,10 @@ static int purge_dirat(int dir_fd, const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) { + close(subdir_fd); <<<<<<<<<< first close ret_val =-1 ; + } } closedir(dir); <<<<<<<<<<<<< second close
return ret_val;
Quoted 11 lines
Review static int purge_dir(const char *path, char **errptr) · line 196
Quoted 5 lines
(quoted diff) +0
-0@@ -192,63 +194,53 @@ static int purge_dir(const char *path, char **errptr) return ret_val; }The rest seems to be fine.
Signature · 2 lines
Cyril Hrubis [email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 6 lines
lib/tst_tmpdir.c +0
-0@@ -186,8 +186,10 @@ static int purge_dirat(int dir_fd, const char *path, char **errptr)continue ;
lib/tst_tmpdir.c static int purge_dirat(int dir_fd, const char *path, char **errptr) · line 195
Quoted 10 lines
lib/tst_tmpdir.c +3
-1/* Recursively remove the current entry */ -if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) { + close(subdir_fd); <<<<<<<<<< first close ret_val =-1 ; + } } closedir(dir); <<<<<<<<<<<<< second close
Ah, right, we hand the fd to the fdopendir() and it's closed in the closedir() call. I've missed that since the closedir() is not shown in the diff since that part of the code wasn't modified and haven't shown up in the diff.
The patch looks good to me then.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 16 lines · Cyril Hrubis
Hi!
Quoted 15 lines
lib/tst_tmpdir.c +3
-1@@ -186,8 +186,10 @@ static int purge_dirat(int dir_fd, const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) { + close(subdir_fd); <<<<<<<<<< first close ret_val =-1 ; + } }
Quoted 8 lines
lib/tst_tmpdir.c static int purge_dirat(int dir_fd, const char *path, char **errptr) · line 195
Quoted 1 line
lib/tst_tmpdir.c +0
-0closedir(dir); <<<<<<<<<<<<< second closeAh, right, we hand the fd to the
fdopendir()and it's closed in theclosedir()call. I've missed that since theclosedir()is not shown in the diff since that part of the code wasn't modified and haven't shown up in the diff.The patch looks good to me then.
Feel free to ack and merge :-)
Signature · 7 lines
Andrea Cervesato SUSE QE Automation Engineer Linux [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 22 lines
Quoted 22 lines
Quoted 6 lines
lib/tst_tmpdir.c +0
-0@@ -186,8 +186,10 @@ static int purge_dirat(int dir_fd, const char *path, char **errptr)continue ;lib/tst_tmpdir.c static int purge_dirat(int dir_fd, const char *path, char **errptr) · line 195
Quoted 10 lines
lib/tst_tmpdir.c +3
-1/* Recursively remove the current entry */ -if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) { + close(subdir_fd); <<<<<<<<<< first close ret_val =-1 ; + } } closedir(dir); <<<<<<<<<<<<< second closeAh, right, we hand the fd to the
fdopendir()and it's closed in theclosedir()call. I've missed that since theclosedir()is not shown in the diff since that part of the code wasn't modified and haven't shown up in the diff.
Quoted 3 lines
Quoted 1 line
The patch looks good to me then.
Feel free to ack and merge :-)
Done, thanks Andrea and Wei.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 14 lines
Using sprintf without length checking in
tst_tmpdirmay lead to buffer overflow. So in this patch useopenat()instead ofopen().Fixs:1241
Signed-off-by Wei Gao <[email protected]>--- lib/tst_tmpdir.c | 119 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 8 deletions(-)lib/tst_tmpdir.c +0
-0
lib/tst_tmpdir.c static char test_start_work_dir[PATH_MAX]; · line 104
Quoted 3 lines
lib/tst_tmpdir.c +0
-0@@ -102,6 +102,7 @@ static char test_start_work_dir[PATH_MAX];extern futex_t *tst_futexes;static int rmobj (const char *obj,char **errmsg);
We shouldn't keep the old rmobj() purge_dir() calls, these should be replaced by rmobjat() and purge_dirat(). Hint: use AT_FDCWD when there is no dir_fd to pass.
Quoted 43 lines
lib/tst_tmpdir.c +15
-2+static int rmobjat (int dir_fd,const char *obj,char **errmsg);int tst_tmpdir_created (void ) {@@ -149,8 +150,8 @@ static int purge_dir(const char *path, char **errptr)int ret_val =0 ;DIR *dir;struct dirent *dir_ent; -char dirobj[PATH_MAX ];static char err_msg[PATH_MAX +1280 ]; +int dir_fd =-1 ;/* Do NOT perform the request if the directory is "/" */ if (!strcmp (path,"/" )) {@@ -167,7 +168,7 @@ static int purge_dir(const char *path, char **errptr)/* Open the directory to get access to what is in it */ if (!(dir =opendir (path))) {if (errptr) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"Cannot open directory %s; errno=%d: %s" , path, errno,tst_strerrno (errno)); *errptr = err_msg;@@ -175,6 +176,18 @@ static int purge_dir(const char *path, char **errptr)return -1 ; } + dir_fd =dirfd (dir); +if (dir_fd ==-1 ) { +closedir (dir); +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot get file descriptor for directory %s; errno=%d: %s" , + path, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } +/* Loop through the entries in the directory, removing each one */ for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) {/* Don't remove "." or ".." */
lib/tst_tmpdir.c static int purge_dir(const char *path, char **errptr) · line 201
Quoted 8 lines
lib/tst_tmpdir.c +3
-2@@ -183,8 +196,57 @@ static int purge_dir(const char *path, char **errptr)continue ;/* Recursively remove the current entry */ -sprintf (dirobj,"%s/%s" , path, dir_ent->d_name); -if (rmobj (dirobj, errptr) !=0 ) +if (rmobjat (dir_fd, dir_ent->d_name, errptr) !=0 ) + ret_val =-1 ; + }
close(dir_fd) here?
Quoted 90 lines
lib/tst_tmpdir.c +51
-4+closedir (dir); +return ret_val; +} + +static int purge_dirat (int dir_fd,const char *path,char **errptr) +{ +int ret_val =0 ; +DIR *dir; +struct dirent *dir_ent; +static char err_msg[PATH_MAX +1280 ]; +int subdir_fd; + + errno =0 ; + +/* Open the subdirectory using openat */ + subdir_fd =openat (dir_fd, path,O_RDONLY |O_DIRECTORY |O_NOFOLLOW ); +if (subdir_fd <0 ) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open subdirectory %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +return -1 ; + } + + dir =fdopendir (subdir_fd); +if (!dir) { +if (errptr) { +snprintf (err_msg,sizeof (err_msg), +"Cannot open directory stream for %s (via fd %d); errno=%d: %s" , + path, dir_fd, errno,tst_strerrno (errno)); + *errptr = err_msg; + } +close (subdir_fd); +return -1 ; + } + +/* Loop through the entries in the directory, removing each one */ +for (dir_ent =readdir (dir); dir_ent; dir_ent =readdir (dir)) { +/* Don't remove "." or ".." */ +if (!strcmp (dir_ent->d_name,"." ) + || !strcmp (dir_ent->d_name,".." )) +continue ; + +/* Recursively remove the current entry */ +if (rmobjat (subdir_fd, dir_ent->d_name, errptr) !=0 ) ret_val =-1 ; }@@ -212,7 +274,7 @@ static int rmobj(const char *obj, char **errmsg)/* Get the link count, now that all the entries have been removed */ if (lstat (obj, &statbuf) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"lstat(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;@@ -225,7 +287,7 @@ static int rmobj(const char *obj, char **errmsg)/* The directory is linked; unlink() must be used */ if (unlink (obj) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"unlink(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;@@ -236,7 +298,7 @@ static int rmobj(const char *obj, char **errmsg)/* The directory is not linked; remove() can be used */ if (remove (obj) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"remove(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;@@ -247,7 +309,7 @@ static int rmobj(const char *obj, char **errmsg) }else {if (unlink (obj) <0 ) {if (errmsg !=NULL ) { -sprintf (err_msg, +snprintf (err_msg,sizeof (err_msg),"unlink(%s) failed; errno=%d: %s" , obj, errno,tst_strerrno (errno)); *errmsg = err_msg;@@ -259,6 +321,47 @@ static int rmobj(const char *obj, char **errmsg)return 0 ; }
lib/tst_tmpdir.c static int rmobj(const char *obj, char **errmsg) · line 333
Quoted 10 lines
lib/tst_tmpdir.c +10
-0+static int rmobjat (int dir_fd,const char *obj,char **errmsg) +{ +int ret_val =0 ; +struct stat statbuf; +static char err_msg[PATH_MAX +1280 ]; +int fd; + + fd =openat (dir_fd, obj,O_DIRECTORY |O_NOFOLLOW ); +if (fd >=0 ) { +close (fd);
We close the fd here just to open it in purge_dirat(), shouldn't we pass it to the purge_dirat() function instead?
Quoted 40 lines
lib/tst_tmpdir.c +31
-0+ ret_val =purge_dirat (dir_fd, obj, errmsg); + +/* If there were problems removing an entry, don't attempt to +remove the directory itself */ +if (ret_val ==-1 ) +return -1 ; + +if (unlinkat (dir_fd, obj,AT_REMOVEDIR ) <0 ) { +if (errmsg !=NULL ) { +snprintf (err_msg,sizeof (err_msg), +"remove(%s) failed; errno=%d: %s" , + obj, errno,tst_strerrno (errno)); + *errmsg = err_msg; + } +return -1 ; + } + }else { +if (unlinkat (dir_fd, obj,0 ) <0 ) { +if (errmsg !=NULL ) { +snprintf (err_msg,sizeof (err_msg), +"unlinkat(%s) failed; errno=%d: %s" , obj, + errno,tst_strerrno (errno)); + *errmsg = err_msg; + } +return -1 ; + } + } + +return 0 ; +} +void tst_tmpdir (void ) {char template[PATH_MAX ];Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp