lib: Add safe timerfd macros
PATCH 1/2 lib: Add safe timerfd macros
SAFE_TIMERFD_CREATE(), SAFE_TIMERFD_GETTIME() and SAFE_TIMERFD_SETTIME()
Added only to new C API.
---
Hi,
NOTE: ENOSYS is handled by ltp_syscall in lapi/timerfd.h.
+ I wonder include/tst_safe_timerfd.h and lapi/timerfd.h shouldn't be merged into single file.
Kind regards,
Petr
include/tst_safe_timerfd.h | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/tst_safe_timerfd.h
include/tst_safe_timerfd.h new file +73 -0
@@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 Petr Vorel <[email protected]> +*/ + +#ifndef TST_SAFE_TIMERFD_H__ +#define TST_SAFE_TIMERFD_H__ + +#include <errno.h> +#include "lapi/timerfd.h" +#include "tst_test.h" + +#define TTYPE (errno == ENOTSUP ? TCONF : TBROK) + +static inline int safe_timerfd_create (const char *file,const int lineno, +int clockid,int flags) +{ +int fd; + + fd =timerfd_create (clockid, flags); + +if (fd <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_create() failed" , + file, lineno); + } + +return fd; +} + +static inline int safe_timerfd_gettime (const char *file,const int lineno, +int fd,struct itimerspec *curr_value) +{ +int rval; + + rval =timerfd_gettime (fd, curr_value); +if (rval <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_gettime() failed" , + file, lineno); + } + +return rval; +} + +static inline int safe_timerfd_settime (const char *file,const int lineno, +int fd,int flags, +const struct itimerspec *new_value, +struct itimerspec *old_value) +{ +int rval; + +if (tst_kvercmp (2 ,6 ,26 ) <=0 ) + flags =0 ; + + rval =timerfd_settime (fd, flags, new_value, old_value); +if (rval <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_settime() failed" , + file, lineno); + } + +return rval; +} + +#define SAFE_TIMERFD_CREATE (clockid, flags)\ + safe_timerfd_create(__FILE__, __LINE__, (clockid), (flags)) + +#define SAFE_TIMERFD_GETTIME (fd, curr_value)\ + safe_timerfd_gettime(__FILE__, __LINE__, (fd), (curr_value)) + +#define SAFE_TIMERFD_SETTIME (fd, flags, new_value, old_value)\ + safe_timerfd_settime(__FILE__, __LINE__, (fd), (flags), (new_value), \ + (old_value)) + +#endif /* SAFE_TIMERFD_H__ */
Signature · 1 line
2.25.1
PATCH 2/2 timerfd: Use safe macros
SAFE_TIMERFD_CREATE() and SAFE_TIMERFD_SETTIME()
---
Safe functions will be used also in other timerfd related tests once they're migrated to new C API.
Kind regards,
Petr
testcases/kernel/syscalls/timerfd/timerfd01.c | 9 +++------ .../kernel/syscalls/timerfd/timerfd_settime02.c | 12 ++---------- 2 files changed, 5 insertions(+), 16 deletions(-)
testcases/kernel/syscalls/timerfd/timerfd01.c +3 -6
@@ -19,7 +19,7 @@#include <poll.h> #include "tst_test.h" #include "tst_timer.h" -#include "lapi/timerfd.h" +#include "tst_safe_timerfd.h" static struct tcase { int id;
@@ -47,8 +47,7 @@ static void settime(int tfd, struct itimerspec *tmr, int tflags, tmr->it_value =tst_us_to_timespec (tvalue); tmr->it_interval =tst_us_to_timespec (tinterval); -if (timerfd_settime (tfd, tflags, tmr,NULL )) -tst_brk (TBROK |TERRNO ,"timerfd_settime() failed" ); +SAFE_TIMERFD_SETTIME (tfd, tflags, tmr,NULL ); }static void waittmr (int tfd,unsigned int exp_ticks)
@@ -87,9 +86,7 @@ static void run(unsigned int n)tst_res (TINFO ,"testing %s" , clks->name); - tfd =timerfd_create (clks->id,0 ); -if (tfd ==-1 ) -tst_brk (TFAIL |TERRNO ,"timerfd_create() failed" ); + tfd =SAFE_TIMERFD_CREATE (clks->id,0 );tst_res (TINFO ,"relative timer (100 ms)" );settime (tfd, &tmr,0 ,100 *1000 ,0 );
testcases/kernel/syscalls/timerfd/timerfd_settime02.c +2 -10
@@ -15,7 +15,7 @@* timerfd: Protect the might cancel mechanism proper */ #include <unistd.h> -#include <lapi/timerfd.h> +#include "tst_safe_timerfd.h" #include "tst_test.h" #include "tst_fuzzy_sync.h" #include "tst_taint.h"
@@ -32,16 +32,8 @@ static struct tst_fzsync_pair fzsync_pair;static void setup (void ) { -int ttype; -tst_taint_init (TST_TAINT_W |TST_TAINT_D ); - errno =0 ; - fd =timerfd_create (CLOCK_REALTIME ,0 ); - -if (fd <0 ) { - ttype = (errno ==ENOTSUP ?TCONF :TBROK ); -tst_brk (ttype |TERRNO ,"Cannot create timer" ); - } + fd =SAFE_TIMERFD_CREATE (CLOCK_REALTIME ,0 ); fzsync_pair.exec_loops =1000000 ;tst_fzsync_pair_init (&fzsync_pair);
Signature · 1 line
2.25.1
PATCH 2/2 timerfd: Use safe macros
Quoted 3 lines · Petr Vorel
SAFE_TIMERFD_CREATE()andSAFE_TIMERFD_SETTIME()Signed-off-by Petr Vorel <[email protected]>
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 1/2 lib: Add safe timerfd macros
In reply to Petr VorelHi!
Quoted 5 lines
SAFE_TIMERFD_CREATE(),SAFE_TIMERFD_GETTIME()andSAFE_TIMERFD_SETTIME()Added only to new C API.
Signed-off-by Petr Vorel <[email protected]>
I do have a patch that adds tst_clock_name() to the tst_safe_clocks.h I will send it so that you can make use of it here as well.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 lib: Add safe timerfd macros
In reply to Petr VorelHello
Quoted 25 lines · Petr Vorel
SAFE_TIMERFD_CREATE(),SAFE_TIMERFD_GETTIME()andSAFE_TIMERFD_SETTIME()Added only to new C API.
Signed-off-by Petr Vorel <[email protected]>---
Hi,NOTE: ENOSYS is handled by
ltp_syscallinlapi/timerfd.h.
+ I wonderinclude/tst_safe_timerfd.handlapi/timerfd.hshouldn't be merged into single file.Kind regards,
Petrinclude/tst_safe_timerfd.h | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/tst_safe_timerfd.hinclude/tst_safe_timerfd.h new file +0
-0
include/tst_safe_timerfd.h line 8
Quoted 8 lines
include/tst_safe_timerfd.h new file +8
-0@@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 Petr Vorel <[email protected]> +*/ + +#ifndef TST_SAFE_TIMERFD_H__ +#define TST_SAFE_TIMERFD_H__ +
Please split the following code off to a separate .c file:
include/tst_safe_timerfd.h line 19
Quoted 11 lines
include/tst_safe_timerfd.h new file +11
-0+#include <errno.h> +#include "lapi/timerfd.h" +#include "tst_test.h" + +#define TTYPE (errno == ENOTSUP ? TCONF : TBROK) + +static inline int safe_timerfd_create (const char *file,const int lineno, +int clockid,int flags) +{ +int fd; +
Don't forget to clear errno when you're not using the TEST() macro.
Quoted 5 lines
include/tst_safe_timerfd.h new file +5
-0+ fd =timerfd_create (clockid, flags); + +if (fd <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_create() failed" , + file, lineno);
include/tst_safe_timerfd.h line 34
Quoted 10 lines
include/tst_safe_timerfd.h new file +10
-0+ } + +return fd; +} + +static inline int safe_timerfd_gettime (const char *file,const int lineno, +int fd,struct itimerspec *curr_value) +{ +int rval; +
Same here.
Quoted 6 lines
include/tst_safe_timerfd.h new file +6
-0+ rval =timerfd_gettime (fd, curr_value); +if (rval <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_gettime() failed" , + file, lineno); + } +
include/tst_safe_timerfd.h line 50
Quoted 10 lines
include/tst_safe_timerfd.h new file +10
-0+return rval; +} + +static inline int safe_timerfd_settime (const char *file,const int lineno, +int fd,int flags, +const struct itimerspec *new_value, +struct itimerspec *old_value) +{ +int rval; +
And here-ish.
include/tst_safe_timerfd.h line 52
Quoted 2 lines
include/tst_safe_timerfd.h new file +2
-0+if (tst_kvercmp (2 ,6 ,26 ) <=0 ) + flags =0 ;
I think tst_brk(TCONF) would be better here. Or at least tst_res(TWARN), since resetting flags to 0 may render some tests useless.
include/tst_safe_timerfd.h line 61
Quoted 9 lines
include/tst_safe_timerfd.h new file +9
-0+ + rval =timerfd_settime (fd, flags, new_value, old_value); +if (rval <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_settime() failed" , + file, lineno); + } + +return rval; +}
Split off up to here.
Quoted 13 lines
include/tst_safe_timerfd.h new file +12
-0+ +#define SAFE_TIMERFD_CREATE (clockid, flags)\ + safe_timerfd_create(__FILE__, __LINE__, (clockid), (flags)) + +#define SAFE_TIMERFD_GETTIME (fd, curr_value)\ + safe_timerfd_gettime(__FILE__, __LINE__, (fd), (curr_value)) + +#define SAFE_TIMERFD_SETTIME (fd, flags, new_value, old_value)\ + safe_timerfd_settime(__FILE__, __LINE__, (fd), (flags), (new_value), \ + (old_value)) + +#endif /* SAFE_TIMERFD_H__ */
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 1/2 lib: Add safe timerfd macros
I forgot to add one more thing: making the return value checks more pedantic would also be nice.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 1/2 lib: Add safe timerfd macros
Hi Martin,
Quoted 2 lines
I forgot to add one more thing: making the return value checks more pedantic would also be nice.
I guess you mean if (rval == -1). Sure, I'll do that.
Kind regards,
Petr
PATCH 1/2 lib: Add safe timerfd macros
Hi!
Quoted 3 lines
Quoted 2 lines
I forgot to add one more thing: making the return value checks more pedantic would also be nice.
I guess you mean if (rval == -1). Sure, I'll do that.
Actually for the sake of SAFE_MACROS() it makes sense to fail unless we got a valid result. So for > 0 for timerfd_create() and != 0 for timerfd_settime() and timerfd_gettime().
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 lib: Add safe timerfd macros
Hi Cyril,
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
I forgot to add one more thing: making the return value checks more pedantic would also be nice.
I guess you mean if (rval == -1). Sure, I'll do that.
Actually for the sake of
SAFE_MACROS()it makes sense to fail unless we got a valid result. So for > 0 fortimerfd_create()
I guess you mean fd < 0 here (no " > 0), as it's a file descriptor.
Quoted 2 lines
and != 0 for
timerfd_settime()andtimerfd_gettime().
Kind regards,
Petr
PATCH 1/2 lib: Add safe timerfd macros
Hi Cyril, Martin,
Quoted 3 lines
Quoted 2 lines
Actually for the sake of
SAFE_MACROS()it makes sense to fail unless we got a valid result. So for > 0 fortimerfd_create()I guess you mean fd < 0 here (no " > 0), as it's a file descriptor.
Merged with this change.
Cyril, you can continue with your patchset.
Kind regards,
Petr
PATCH 1/2 lib: Add safe timerfd macros
In reply to Martin DouchaHi!
Quoted 1 line
Don't forget to clear errno when you're not using the
TEST()macro.
Hmm, I'm not sure how useful is this. Generally the syscalls in libc have single macro definition that is used everywhere to copy the error from the errno variable. If that piece of code is buggy half of the test in LTP would fail anyway.
...
Quoted 5 lines
include/tst_safe_timerfd.h line 52
Quoted 2 lines
include/tst_safe_timerfd.h new file +2
-0+if (tst_kvercmp (2 ,6 ,26 ) <=0 ) + flags =0 ;I think
tst_brk(TCONF)would be better here. Or at leasttst_res(TWARN), since resetting flags to 0 may render some tests useless.
I think that it's completely wrong to put kernel version comparsion to the safe_macros. If the test needs specific kernel version it should be either put into the tst_test structure of handled in the test setup.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 lib: Add safe timerfd macros
Hi!
Quoted 5 lines
Quoted 1 line
Don't forget to clear errno when you're not using the
TEST()macro.Hmm, I'm not sure how useful is this. Generally the syscalls in libc have single macro definition that is used everywhere to copy the error from the errno variable. If that piece of code is buggy half of the test
^
to the
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/2 lib: Add safe timerfd macros
In reply to Cyril HrubisHi Cyril,
Quoted 6 lines
Quoted 1 line
Don't forget to clear errno when you're not using the
TEST()macro.Hmm, I'm not sure how useful is this. Generally the syscalls in libc have single macro definition that is used everywhere to copy the error from the errno variable. If that piece of code is buggy half of the test in LTP would fail anyway.
That's exactly what I thought when I omit that.
+ we reset errno on fewer places than we don't.
Quoted 9 lines
Quoted 5 lines
include/tst_safe_timerfd.h line 52
Quoted 2 lines
include/tst_safe_timerfd.h new file +2
-0+if (tst_kvercmp (2 ,6 ,26 ) <=0 ) + flags =0 ;I think
tst_brk(TCONF)would be better here. Or at leasttst_res(TWARN), since resetting flags to 0 may render some tests useless.I think that it's completely wrong to put kernel version comparsion to the
safe_macros. If the test needs specific kernel version it should be either put into thetst_teststructure of handled in the test setup.
OK, I'll remove it.
For my previous state:timerfd01.c (all)timerfd_settime01.c (3th and 4rd test)timerfd_settime02.c (not sure whether this old version is really affected by CVE-2017-10661)
It means that 1st and 2nd test from timerfd_settime01.c could be tested on 2.6.25 and .26, but it won't due TCONF. But probably not many people will miss it nowadays.
Kind regards,
Petr
PATCH 1/2 lib: Add safe timerfd macros
In reply to Martin DouchaHi Martin,
Quoted 1 line
Please split the following code off to a separate .c file:
OK, I'll do :). We had some discussions about it, I remember problems caused by using lapi files in the library headers (tst_device.h), I'd consider ok having just header tst_safe_timerfd.h unless it's loaded by library itself, but somebody could do it later. And I also consider it as a cleaner solution (hoping that linker will really drop unused symbols).
Quoted 5 lines
Quoted 5 lines
include/tst_safe_timerfd.h new file +5
-0+#include <errno.h> +#include "lapi/timerfd.h" +#include "tst_test.h" + +#define TTYPE (errno == ENOTSUP ? TCONF : TBROK)
Quoted 8 lines
include/tst_safe_timerfd.h line 19
Quoted 6 lines
include/tst_safe_timerfd.h new file +6
-0+ +static inline int safe_timerfd_create (const char *file,const int lineno, +int clockid,int flags) +{ +int fd; +Don't forget to clear errno when you're not using the
TEST()macro.
Do you think it's necessary? I've noticed you clear errno here in original test, but I'd expect errno is be set if (fd < 0).
But ok, it's done in some macros (tst_safe_macros.c, safe_macros.c)
Quoted 5 lines
include/tst_safe_timerfd.h line 52
Quoted 2 lines
include/tst_safe_timerfd.h new file +2
-0+if (tst_kvercmp (2 ,6 ,26 ) <=0 ) + flags =0 ;I think
tst_brk(TCONF)would be better here. Or at leasttst_res(TWARN), since resetting flags to 0 may render some tests useless.
Well, this is just to keep test working under 2.6.25 and .26. Obviously it would not affect many users, but these would got broken these tests timerfd01.c (all)timerfd_settime01.c (3th and 4rd test)timerfd_settime02.c (not sure whether this old version is really affected by CVE-2017-10661)
break all tests). Agree that hiding it is not good.
Quoted 11 lines
include/tst_safe_timerfd.h line 61
Quoted 9 lines
include/tst_safe_timerfd.h new file +9
-0+ + rval =timerfd_settime (fd, flags, new_value, old_value); +if (rval <0 ) { +tst_brk (TTYPE |TERRNO ,"%s:%d: timerfd_settime() failed" , + file, lineno); + } + +return rval; +}Split off up to here.
I guess you mean that macros should be kept in header (sure).
Quoted 4 lines
Quoted 4 lines
include/tst_safe_timerfd.h new file +4
-0+ +#define SAFE_TIMERFD_CREATE (clockid, flags)\ + safe_timerfd_create(__FILE__, __LINE__, (clockid), (flags)) +
Quoted 8 lines
include/tst_safe_timerfd.h line 73
Quoted 8 lines
include/tst_safe_timerfd.h new file +8
-0+#define SAFE_TIMERFD_GETTIME (fd, curr_value)\ + safe_timerfd_gettime(__FILE__, __LINE__, (fd), (curr_value)) + +#define SAFE_TIMERFD_SETTIME (fd, flags, new_value, old_value)\ + safe_timerfd_settime(__FILE__, __LINE__, (fd), (flags), (new_value), \ + (old_value)) + +#endif /* SAFE_TIMERFD_H__ */
Kind regards,
Petr