Path: blob/main/contrib/capsicum-test/copy_file_range.cc
39475 views
#include <sys/types.h>1#include <fcntl.h>23#include <string>45#include "capsicum.h"6#include "capsicum-test.h"7#include "syscalls.h"89#define TOPDIR "cap_copy_file_range"10#define INFILE "infile"11#define OUTFILE "outfile"1213/* Test that copy_file_range() checks capabilities correctly.14* When used without offset arguments, copy_file_range() should15* require only CAP_READ on the source and CAP_WRITE on the destination16* file descriptors, respectively.17* When used with offset arguments, copy_file_range() should18* additionally require CAP_SEEK.19*/20class CopyFileRangeTest : public ::testing::Test {21public:22CopyFileRangeTest() {23int rc = mkdir(TmpFile(TOPDIR), 0755);24EXPECT_OK(rc);25if (rc < 0) {26EXPECT_EQ(EEXIST, errno);27}28wd_ = open(TmpFile(TOPDIR), O_DIRECTORY);29EXPECT_OK(wd_);30CreateFile(TmpFile(TOPDIR "/" INFILE));31CreateFile(TmpFile(TOPDIR "/" OUTFILE));32}33~CopyFileRangeTest() {34close(wd_);35unlink(TmpFile(TOPDIR "/" INFILE));36unlink(TmpFile(TOPDIR "/" OUTFILE));37rmdir(TmpFile(TOPDIR));38}3940private:41void CreateFile(const char *filename) {42int fd = open(filename, O_CREAT|O_RDWR, 0644);43const char *contents = "lorem ipsum dolor sit amet";44EXPECT_OK(fd);45for (int i = 0; i < 100; i++) {46EXPECT_OK(write(fd, contents, strlen(contents)));47}48close(fd);49}5051protected:52int wd_;5354int openInFile(cap_rights_t *rights) {55int fd = openat(wd_, INFILE, O_RDONLY);56EXPECT_OK(fd);57EXPECT_OK(cap_rights_limit(fd, rights));58return fd;59}60int openOutFile(cap_rights_t *rights) {61int fd = openat(wd_, OUTFILE, O_WRONLY);62EXPECT_OK(fd);63EXPECT_OK(cap_rights_limit(fd, rights));64return fd;65}66};6768TEST_F(CopyFileRangeTest, WriteReadNeg) {69cap_rights_t rights_in, rights_out;7071cap_rights_init(&rights_in, CAP_WRITE);72cap_rights_init(&rights_out, CAP_READ);7374int fd_in = openInFile(&rights_in);75int fd_out = openOutFile(&rights_out);76off_t off_in = 0, off_out = 0;7778EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));79EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));80EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));81EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));82off_in = 20;83off_out = 20;84EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));85EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));86EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));87close(fd_in);88close(fd_out);89}9091TEST_F(CopyFileRangeTest, ReadReadNeg) {92cap_rights_t rights_in, rights_out;9394cap_rights_init(&rights_in, CAP_READ);95cap_rights_init(&rights_out, CAP_READ);9697int fd_in = openInFile(&rights_in);98int fd_out = openOutFile(&rights_out);99off_t off_in = 0, off_out = 0;100101EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));102EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));103EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));104EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));105off_in = 20;106off_out = 20;107EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));108EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));109EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));110close(fd_in);111close(fd_out);112}113114TEST_F(CopyFileRangeTest, WriteWriteNeg) {115cap_rights_t rights_in, rights_out;116117cap_rights_init(&rights_in, CAP_WRITE);118cap_rights_init(&rights_out, CAP_WRITE);119120int fd_in = openInFile(&rights_in);121int fd_out = openOutFile(&rights_out);122off_t off_in = 0, off_out = 0;123124EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));125EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));126EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));127EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));128off_in = 20;129off_out = 20;130EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));131EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));132EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));133close(fd_in);134close(fd_out);135}136137TEST_F(CopyFileRangeTest, ReadWrite) {138cap_rights_t rights_in, rights_out;139140cap_rights_init(&rights_in, CAP_READ);141cap_rights_init(&rights_out, CAP_WRITE);142143int fd_in = openInFile(&rights_in);144int fd_out = openOutFile(&rights_out);145off_t off_in = 0, off_out = 0;146147EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));148EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));149EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));150EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));151off_in = 20;152off_out = 20;153EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));154EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));155EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));156close(fd_in);157close(fd_out);158}159160TEST_F(CopyFileRangeTest, ReadSeekWrite) {161cap_rights_t rights_in, rights_out;162163cap_rights_init(&rights_in, CAP_READ, CAP_SEEK);164cap_rights_init(&rights_out, CAP_WRITE);165166int fd_in = openInFile(&rights_in);167int fd_out = openOutFile(&rights_out);168off_t off_in = 0, off_out = 0;169170EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));171EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));172EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));173EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));174off_in = 20;175off_out = 20;176EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));177EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));178EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));179close(fd_in);180close(fd_out);181}182183TEST_F(CopyFileRangeTest, ReadWriteSeek) {184cap_rights_t rights_in, rights_out;185186cap_rights_init(&rights_in, CAP_READ);187cap_rights_init(&rights_out, CAP_WRITE, CAP_SEEK);188189int fd_in = openInFile(&rights_in);190int fd_out = openOutFile(&rights_out);191off_t off_in = 0, off_out = 0;192193EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));194EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));195EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));196EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));197off_in = 20;198off_out = 20;199EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));200EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));201EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));202close(fd_in);203close(fd_out);204}205206TEST_F(CopyFileRangeTest, ReadSeekWriteSeek) {207cap_rights_t rights_in, rights_out;208209cap_rights_init(&rights_in, CAP_READ, CAP_SEEK);210cap_rights_init(&rights_out, CAP_WRITE, CAP_SEEK);211212int fd_in = openInFile(&rights_in);213int fd_out = openOutFile(&rights_out);214off_t off_in = 0, off_out = 0;215216EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));217EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));218EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));219EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));220off_in = 20;221off_out = 20;222EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));223EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));224EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));225close(fd_in);226close(fd_out);227}228229230