exam_1.cc
// Solution to Exam 1, Question 6
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main( int argc, char *argv[], char *envp[] )
{
const char *args[] =
{
{ "/bin/ls" },
{ "-l" },
{ "-a" },
{ "-F" },
{ 0 },
};
const char *env[] =
{
{ "SHELL=/bin/sh" },
{ "USER=vickery" },
{ 0 },
};
struct stat sb;
if ( -1 == stat( "/bin/ls", &sb ) )
{
perror( "stat" );
exit(1);
}
uid_t user = geteuid();
gid_t group = getegid();
if (
( (user == sb.st_uid) && (sb.st_mode & 0100) == 0100 ) ||
( (group == sb.st_gid) && (sb.st_mode & 0010) == 0010 ) ||
( (sb.st_mode & 0001) == 0001 )
)
{
execve( args[0], (char*const*)args, (char*const*)env );
perror( "exec" );
exit( 1 );
}
fprintf( stderr, "permission denied\n" );
exit( 1 );
}