2016-09-21 06:12:37 +03:00
/*
* Check decoding of quotactl syscall .
*
2016-10-30 23:44:50 +03:00
* Copyright ( c ) 2016 Eugene Syromyatnikov < evgsyr @ gmail . com >
2016-09-21 06:12:37 +03:00
* Copyright ( c ) 2016 Dmitry V . Levin < ldv @ altlinux . org >
2018-08-13 01:22:32 +03:00
* Copyright ( c ) 2016 - 2018 The strace developers .
2016-09-21 06:12:37 +03:00
* All rights reserved .
*
2018-12-10 03:00:00 +03:00
* SPDX - License - Identifier : GPL - 2.0 - or - later
2016-09-21 06:12:37 +03:00
*/
# include "tests.h"
# include <asm/unistd.h>
# if defined(__NR_quotactl) && \
( defined ( HAVE_LINUX_QUOTA_H ) | | defined ( HAVE_SYS_QUOTA_H ) )
# include <inttypes.h>
# include <stdint.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include "quotactl.h"
# ifndef HAVE_LINUX_QUOTA_H
/* Some dirty hacks in order to make sys/quota.h usable as a backup */
# define if_dqblk dqblk
# define if_nextdqblk nextdqblk
# define if_dqinfo dqinfo
# endif /* !HAVE_LINUX_QUOTA_H */
# ifndef Q_GETNEXTQUOTA
# define Q_GETNEXTQUOTA 0x800009
struct if_nextdqblk {
uint64_t dqb_bhardlimit ;
uint64_t dqb_bsoftlimit ;
uint64_t dqb_curspace ;
uint64_t dqb_ihardlimit ;
uint64_t dqb_isoftlimit ;
uint64_t dqb_curinodes ;
uint64_t dqb_btime ;
uint64_t dqb_itime ;
uint32_t dqb_valid ;
uint32_t dqb_id ;
} ;
# endif /* !Q_GETNEXTQUOTA */
# include "xlat.h"
# include "xlat / quota_formats.h"
# include "xlat / if_dqblk_valid.h"
# include "xlat / if_dqinfo_flags.h"
# include "xlat / if_dqinfo_valid.h"
void
print_dqblk ( long rc , void * ptr , void * arg )
{
struct if_dqblk * db = ptr ;
long out_arg = ( long ) arg ;
2018-07-18 13:00:58 +03:00
if ( ( ( rc < 0 ) & & out_arg ) | | ( out_arg > 1 ) ) {
2016-09-21 06:12:37 +03:00
printf ( " %p " , db ) ;
return ;
}
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " { " , * db , dqb_bhardlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_bsoftlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_curspace ) ;
PRINT_FIELD_U ( " , " , * db , dqb_ihardlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_isoftlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_curinodes ) ;
2016-09-21 06:12:37 +03:00
# if VERBOSE
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " , " , * db , dqb_btime ) ;
PRINT_FIELD_U ( " , " , * db , dqb_itime ) ;
2016-09-21 06:12:37 +03:00
printf ( " , dqb_valid= " ) ;
printflags ( if_dqblk_valid , db - > dqb_valid , " QIF_??? " ) ;
# else
printf ( " , ... " ) ;
# endif /* !VERBOSE */
printf ( " } " ) ;
}
void
print_nextdqblk ( long rc , void * ptr , void * arg )
{
struct if_nextdqblk * db = ptr ;
long out_arg = ( long ) arg ;
2018-07-18 13:00:58 +03:00
if ( ( ( rc < 0 ) & & out_arg ) | | ( out_arg > 1 ) ) {
2016-09-21 06:12:37 +03:00
printf ( " %p " , db ) ;
return ;
}
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " { " , * db , dqb_bhardlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_bsoftlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_curspace ) ;
PRINT_FIELD_U ( " , " , * db , dqb_ihardlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_isoftlimit ) ;
PRINT_FIELD_U ( " , " , * db , dqb_curinodes ) ;
2016-09-21 06:12:37 +03:00
# if VERBOSE
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " , " , * db , dqb_btime ) ;
PRINT_FIELD_U ( " , " , * db , dqb_itime ) ;
2016-09-21 06:12:37 +03:00
printf ( " , dqb_valid= " ) ;
printflags ( if_dqblk_valid , db - > dqb_valid , " QIF_??? " ) ;
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " , " , * db , dqb_id ) ;
2016-09-21 06:12:37 +03:00
# else
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " , " , * db , dqb_id ) ;
2016-09-21 06:12:37 +03:00
printf ( " , ... " ) ;
# endif /* !VERBOSE */
printf ( " } " ) ;
}
void
print_dqinfo ( long rc , void * ptr , void * arg )
{
struct if_dqinfo * di = ptr ;
long out_arg = ( long ) arg ;
2018-07-18 13:00:58 +03:00
if ( ( ( rc < 0 ) & & out_arg ) | | ( out_arg > 1 ) ) {
2016-09-21 06:12:37 +03:00
printf ( " %p " , di ) ;
return ;
}
2017-07-01 16:14:49 +03:00
PRINT_FIELD_U ( " { " , * di , dqi_bgrace ) ;
PRINT_FIELD_U ( " , " , * di , dqi_igrace ) ;
2016-09-21 06:12:37 +03:00
printf ( " , dqi_flags= " ) ;
printflags ( if_dqinfo_flags , di - > dqi_flags , " DQF_??? " ) ;
printf ( " , dqi_valid= " ) ;
printflags ( if_dqinfo_valid , di - > dqi_valid , " IIF_??? " ) ;
printf ( " } " ) ;
}
2018-07-12 12:32:11 +03:00
void
print_dqfmt ( long rc , void * ptr , void * arg )
{
uint32_t * fmtval = ptr ;
long out_arg = ( long ) arg ;
const char * fmtstr ;
2018-07-18 13:00:58 +03:00
if ( ( ( rc < 0 ) & & out_arg ) | | ( out_arg > 1 ) ) {
2018-07-12 12:32:11 +03:00
printf ( " %p " , fmtval ) ;
return ;
}
printf ( " [ " ) ;
switch ( * fmtval ) {
case 1 :
fmtstr = " QFMT_VFS_OLD " ;
break ;
case 2 :
fmtstr = " QFMT_VFS_V0 " ;
break ;
case 3 :
fmtstr = " QFMT_OCFS2 " ;
break ;
case 4 :
fmtstr = " QFMT_VFS_V1 " ;
break ;
default :
printf ( " %#x /* QFMT_VFS_??? */] " , * fmtval ) ;
return ;
}
printf ( " %s] " , fmtstr ) ;
}
2016-09-21 06:12:37 +03:00
2018-07-12 12:32:12 +03:00
2016-09-21 06:12:37 +03:00
int
main ( void )
{
2016-10-30 22:28:37 +03:00
char * bogus_special = ( char * ) tail_alloc ( 1 ) + 1 ;
void * bogus_addr = ( char * ) tail_alloc ( 1 ) + 1 ;
2016-09-21 06:12:37 +03:00
char bogus_special_str [ sizeof ( void * ) * 2 + sizeof ( " 0x " ) ] ;
char unterminated_str [ sizeof ( void * ) * 2 + sizeof ( " 0x " ) ] ;
2018-07-12 12:32:12 +03:00
static char invalid_cmd_str [ 1024 ] ;
static char invalid_id_str [ 1024 ] ;
2016-09-21 06:12:37 +03:00
char * unterminated = tail_memdup ( unterminated_data ,
sizeof ( unterminated_data ) ) ;
2017-03-16 16:46:36 +03:00
TAIL_ALLOC_OBJECT_CONST_PTR ( struct if_dqblk , dqblk ) ;
TAIL_ALLOC_OBJECT_CONST_PTR ( struct if_dqinfo , dqinfo ) ;
TAIL_ALLOC_OBJECT_CONST_PTR ( uint32_t , fmt ) ;
TAIL_ALLOC_OBJECT_CONST_PTR ( struct if_nextdqblk , nextdqblk ) ;
2016-09-21 06:12:37 +03:00
snprintf ( bogus_special_str , sizeof ( bogus_special_str ) , " %p " ,
bogus_special ) ;
snprintf ( unterminated_str , sizeof ( unterminated_str ) , " %p " ,
unterminated ) ;
/* Invalid commands */
2018-07-12 12:32:12 +03:00
snprintf ( invalid_cmd_str , sizeof ( invalid_cmd_str ) ,
" QCMD(%#x /* Q_??? */, %#x /* ???QUOTA */) " ,
QCMD_CMD ( bogus_cmd ) , QCMD_TYPE ( bogus_cmd ) ) ;
check_quota ( CQF_NONE , bogus_cmd , invalid_cmd_str ,
bogus_special , bogus_special_str , bogus_id , bogus_addr ) ;
2016-09-21 06:12:37 +03:00
2018-07-12 12:32:12 +03:00
snprintf ( invalid_cmd_str , sizeof ( invalid_cmd_str ) ,
" QCMD(0 /* Q_??? */, USRQUOTA) " ) ;
check_quota ( CQF_ADDR_STR , 0 , invalid_cmd_str ,
ARG_STR ( NULL ) , - 1 , ARG_STR ( NULL ) ) ;
2016-09-21 06:12:37 +03:00
/* Q_QUOTAON */
check_quota ( CQF_ID_STR | CQF_ADDR_STR ,
ARG_STR ( QCMD ( Q_QUOTAON , USRQUOTA ) ) ,
ARG_STR ( " /dev/bogus/ " ) , ARG_STR ( QFMT_VFS_OLD ) ,
ARG_STR ( " /tmp/bogus/ " ) ) ;
2018-07-12 12:32:12 +03:00
snprintf ( invalid_cmd_str , sizeof ( invalid_cmd_str ) ,
" QCMD(Q_QUOTAON, %#x /* ???QUOTA */) " ,
QCMD_TYPE ( QCMD ( Q_QUOTAON , 0xfacefeed ) ) ) ;
snprintf ( invalid_id_str , sizeof ( invalid_id_str ) ,
" %#x /* QFMT_VFS_??? */ " , bogus_id ) ;
check_quota ( CQF_ID_STR , QCMD ( Q_QUOTAON , 0xfacefeed ) ,
invalid_cmd_str , bogus_dev , bogus_dev_str ,
bogus_id , invalid_id_str , bogus_addr ) ;
2016-09-21 06:12:37 +03:00
/* Q_QUOTAOFF */
check_quota ( CQF_ID_SKIP | CQF_ADDR_SKIP ,
ARG_STR ( QCMD ( Q_QUOTAOFF , USRQUOTA ) ) ,
bogus_special , bogus_special_str ) ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_SKIP ,
ARG_STR ( QCMD ( Q_QUOTAOFF , GRPQUOTA ) ) ,
ARG_STR ( " /dev/bogus/ " ) ) ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_SKIP ,
ARG_STR ( QCMD ( Q_QUOTAOFF , PRJQUOTA ) ) , ARG_STR ( NULL ) ) ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_SKIP ,
QCMD ( Q_QUOTAOFF , 3 ) , " QCMD(Q_QUOTAOFF, 0x3 /* ???QUOTA */) " ,
ARG_STR ( NULL ) ) ;
/* Q_GETQUOTA */
/* Trying our best to get successful result */
check_quota ( CQF_ADDR_CB , ARG_STR ( QCMD ( Q_GETQUOTA , USRQUOTA ) ) ,
ARG_STR ( " /dev/sda1 " ) , getuid ( ) , dqblk , print_dqblk ,
( intptr_t ) 1 ) ;
check_quota ( CQF_ADDR_CB , ARG_STR ( QCMD ( Q_GETQUOTA , GRPQUOTA ) ) ,
2018-07-18 13:00:58 +03:00
ARG_STR ( NULL ) , - 1 , dqblk , print_dqblk , ( intptr_t ) 1 ) ;
2016-09-21 06:12:37 +03:00
/* Q_GETNEXTQUOTA */
check_quota ( CQF_ADDR_CB , ARG_STR ( QCMD ( Q_GETNEXTQUOTA , USRQUOTA ) ) ,
ARG_STR ( " /dev/sda1 " ) , 0 , nextdqblk , print_nextdqblk ,
( intptr_t ) 1 ) ;
/* Q_SETQUOTA */
2017-01-05 01:17:26 +03:00
fill_memory ( dqblk , sizeof ( * dqblk ) ) ;
2016-09-21 06:12:37 +03:00
check_quota ( CQF_NONE , ARG_STR ( QCMD ( Q_SETQUOTA , PRJQUOTA ) ) ,
bogus_special , bogus_special_str , 0 , bogus_addr ) ;
check_quota ( CQF_ADDR_CB , ARG_STR ( QCMD ( Q_SETQUOTA , PRJQUOTA ) ) ,
ARG_STR ( " /dev/bogus/ " ) , 3141592653U , dqblk , print_dqblk ,
( intptr_t ) 0 ) ;
/* Q_GETINFO */
check_quota ( CQF_ID_SKIP | CQF_ADDR_CB ,
ARG_STR ( QCMD ( Q_GETINFO , GRPQUOTA ) ) ,
ARG_STR ( " /dev/sda1 " ) , dqinfo , print_dqinfo , ( intptr_t ) 1 ) ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_CB ,
ARG_STR ( QCMD ( Q_GETINFO , GRPQUOTA ) ) ,
bogus_special , bogus_special_str , dqinfo ,
2018-07-18 13:00:58 +03:00
print_dqinfo , ( intptr_t ) 1 ) ;
2016-09-21 06:12:37 +03:00
/* Q_SETINFO */
2017-01-05 01:17:26 +03:00
fill_memory ( dqinfo , sizeof ( * dqinfo ) ) ;
2016-09-21 06:12:37 +03:00
/* In order to check flag printing correctness */
dqinfo - > dqi_flags = 0xdeadabcd ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_STR ,
ARG_STR ( QCMD ( Q_SETINFO , PRJQUOTA ) ) ,
bogus_special , bogus_special_str , ARG_STR ( NULL ) ) ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_CB ,
ARG_STR ( QCMD ( Q_SETINFO , USRQUOTA ) ) ,
ARG_STR ( " /dev/bogus/ " ) , dqinfo , print_dqinfo , ( intptr_t ) 0 ) ;
/* Q_GETFMT */
check_quota ( CQF_ID_SKIP | CQF_ADDR_STR ,
ARG_STR ( QCMD ( Q_GETFMT , PRJQUOTA ) ) ,
bogus_special , bogus_special_str , ARG_STR ( NULL ) ) ;
check_quota ( CQF_ID_SKIP ,
ARG_STR ( QCMD ( Q_GETFMT , USRQUOTA ) ) ,
unterminated , unterminated_str , fmt + 1 ) ;
2018-07-12 12:32:11 +03:00
check_quota ( CQF_ID_SKIP | CQF_ADDR_CB ,
ARG_STR ( QCMD ( Q_GETFMT , GRPQUOTA ) ) ,
ARG_STR ( " /dev/sda1 " ) , fmt , print_dqfmt , ( uintptr_t ) 1 ) ;
/* Try to check valid quota format */
* fmt = QFMT_VFS_OLD ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_CB ,
2016-09-21 06:12:37 +03:00
ARG_STR ( QCMD ( Q_GETFMT , GRPQUOTA ) ) ,
2018-07-12 12:32:11 +03:00
ARG_STR ( " /dev/sda1 " ) , fmt , print_dqfmt , ( uintptr_t ) 1 ) ;
2016-09-21 06:12:37 +03:00
/* Q_SYNC */
check_quota ( CQF_ID_SKIP | CQF_ADDR_SKIP ,
ARG_STR ( QCMD ( Q_SYNC , USRQUOTA ) ) ,
bogus_special , bogus_special_str ) ;
check_quota ( CQF_ID_SKIP | CQF_ADDR_SKIP ,
QCMD ( Q_SYNC , 0xfff ) , " QCMD(Q_SYNC, 0xff /* ???QUOTA */) " ,
ARG_STR ( NULL ) ) ;
puts ( " +++ exited with 0 +++ " ) ;
return 0 ;
}
# else
SKIP_MAIN_UNDEFINED ( " __NR_quotactl && "
" (HAVE_LINUX_QUOTA_H || HAVE_SYS_QUOTA_H) " ) ;
# endif