2016-10-03 21:36:09 +03:00
/*
* Check decoding of add_key syscall .
*
2016-10-30 23:44:50 +03:00
* Copyright ( c ) 2016 Eugene Syromyatnikov < evgsyr @ gmail . com >
2018-06-14 14:00:00 +03:00
* Copyright ( c ) 2016 - 2018 The strace developers .
2016-10-03 21:36:09 +03:00
* All rights reserved .
*
2018-12-10 03:00:00 +03:00
* SPDX - License - Identifier : GPL - 2.0 - or - later
2016-10-03 21:36:09 +03:00
*/
# include "tests.h"
# include <asm/unistd.h>
2016-12-20 03:06:15 +03:00
# include "scno.h"
2016-10-03 21:36:09 +03:00
# ifdef __NR_add_key
# include <inttypes.h>
# include <stdio.h>
# include <unistd.h>
void
print_val_str ( const void * ptr , const char * str )
{
if ( str )
printf ( " %s, " , str ) ;
else
printf ( " %p, " , ptr ) ;
}
void
do_add_key ( const char * type , const char * type_str , const char * desc ,
const char * desc_str , const char * payload , const char * payload_str ,
size_t plen , int32_t keyring , const char * keyring_str )
{
long rc = syscall ( __NR_add_key , type , desc , payload , plen , keyring ) ;
const char * errstr = sprintrc ( rc ) ;
printf ( " add_key( " ) ;
print_val_str ( type , type_str ) ;
print_val_str ( desc , desc_str ) ;
print_val_str ( payload , payload_str ) ;
printf ( " %zu, " , plen ) ;
if ( keyring_str )
printf ( " %s " , keyring_str ) ;
else
printf ( " %d " , keyring ) ;
printf ( " ) = %s \n " , errstr ) ;
}
int
main ( void )
{
static const char unterminated1 [ ] = { ' \1 ' , ' \2 ' , ' \3 ' , ' \4 ' , ' \5 ' } ;
static const char unterminated2 [ ] = { ' \6 ' , ' \7 ' , ' \10 ' , ' \11 ' , ' \12 ' } ;
2017-06-18 01:23:09 +03:00
static const char unterminated3 [ ] = { ' \16 ' , ' \17 ' , ' \20 ' , ' \21 ' , ' \22 ' } ;
2016-10-03 21:36:09 +03:00
char * bogus_type = tail_memdup ( unterminated1 , sizeof ( unterminated1 ) ) ;
char * bogus_desc = tail_memdup ( unterminated2 , sizeof ( unterminated2 ) ) ;
char * bogus_payload = tail_memdup ( unterminated3 , sizeof ( unterminated3 ) ) ;
unsigned i ;
unsigned j ;
unsigned k ;
unsigned l ;
struct {
const char * type ;
const char * str ;
} types [ ] = {
{ ARG_STR ( NULL ) } ,
2016-10-30 22:27:07 +03:00
{ bogus_type + sizeof ( unterminated1 ) , NULL } ,
2016-10-03 21:36:09 +03:00
{ bogus_type , NULL } ,
{ ARG_STR ( " \20 \21 \22 \23 \24 " ) } ,
{ ARG_STR ( " user " ) } ,
} ;
struct {
const char * desc ;
const char * str ;
} descs [ ] = {
{ ARG_STR ( NULL ) } ,
2016-10-30 22:27:07 +03:00
{ bogus_desc + sizeof ( unterminated2 ) , NULL } ,
2016-10-03 21:36:09 +03:00
{ bogus_desc , NULL } ,
{ ARG_STR ( " \25 \26 \27 \30 \31 " ) } ,
{ ARG_STR ( " desc " ) } ,
2018-06-06 13:10:43 +03:00
{ " overly long description " , STRINGIFY ( " overly long " ) " ... " } ,
2016-10-03 21:36:09 +03:00
} ;
struct {
const char * pload ;
const char * str ;
size_t plen ;
} payloads [ ] = {
{ ARG_STR ( NULL ) , 0 } ,
2016-10-30 22:27:07 +03:00
{ bogus_payload + sizeof ( unterminated3 ) , NULL ,
2016-10-03 21:36:09 +03:00
( size_t ) 0xdeadbeefbadc0dedULL } ,
2018-06-06 13:10:43 +03:00
{ bogus_payload , STRINGIFY ( " " ) , 0 } ,
{ bogus_payload , STRINGIFY ( " \16 \17 \20 \21 \22 " ) , 5 } ,
2016-10-03 21:36:09 +03:00
{ bogus_payload , NULL , 10 } ,
2018-06-06 13:10:43 +03:00
{ " overly long payload " , STRINGIFY ( " overly long " ) " ... " , 15 } ,
2016-10-03 21:36:09 +03:00
} ;
struct {
uint32_t keyring ;
const char * str ;
} keyrings [ ] = {
{ ARG_STR ( 0 ) } ,
{ ARG_STR ( 1234567890 ) } ,
{ ARG_STR ( - 1234567890 ) } ,
{ - 1 , " KEY_SPEC_THREAD_KEYRING " } ,
} ;
for ( i = 0 ; i < ARRAY_SIZE ( types ) ; i + + )
for ( j = 0 ; j < ARRAY_SIZE ( descs ) ; j + + )
for ( k = 0 ; k < ARRAY_SIZE ( payloads ) ; k + + )
for ( l = 0 ; l < ARRAY_SIZE ( keyrings ) ; l + + )
do_add_key ( types [ i ] . type , types [ i ] . str ,
descs [ j ] . desc , descs [ j ] . str ,
payloads [ k ] . pload ,
payloads [ k ] . str ,
payloads [ k ] . plen ,
keyrings [ l ] . keyring ,
keyrings [ l ] . str ) ;
puts ( " +++ exited with 0 +++ " ) ;
return 0 ;
}
# else
SKIP_MAIN_UNDEFINED ( " __NR_add_key " ) ;
# endif