Skip to content

Commit

Permalink
Test rename[at] syscalls with absolute path (#22)
Browse files Browse the repository at this point in the history
Check rename[at] doesn't unexpectedly require any rights for the
directory file descriptor if the path is an absolute path.
  • Loading branch information
ScottieY authored and daviddrysdale committed Oct 13, 2017
1 parent 439ca99 commit 7a79785
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: capsicum-test smoketest mini-me mini-me.noexec mini-me.setuid $(EXTRA_PROGS)
OBJECTS=capsicum-test-main.o capsicum-test.o capability-fd.o fexecve.o procdesc.o capmode.o fcntl.o ioctl.o openat.o sysctl.o select.o mqueue.o socket.o sctp.o capability-fd-pair.o linux.o overhead.o
OBJECTS=capsicum-test-main.o capsicum-test.o capability-fd.o fexecve.o procdesc.o capmode.o fcntl.o ioctl.o openat.o sysctl.o select.o mqueue.o socket.o sctp.o capability-fd-pair.o linux.o overhead.o rename.o

GTEST_DIR=gtest-1.6.0
GTEST_INCS=-I$(GTEST_DIR)/include -I$(GTEST_DIR)
Expand Down
49 changes: 49 additions & 0 deletions rename.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <fcntl.h>
#include <sys/stat.h>

#include "./capsicum-test.h"

// There was a Capsicum-related regression in FreeBSD renameat,
// which affects certain cases independent of Capsicum or capability mode
//
// added to test the renameat syscall for the case that
// - the "to" file already exists
// - the "to" file is specified by an absolute path
// - the "to" file descriptor is used
// (this descriptor should be ignored if absolute path is provided)
//
// details at: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222258


const char * create_tmp_src(const char* filename) {
const char *src_path = TmpFile(filename);
int src_fd = open(src_path, O_CREAT|O_RDWR, 0644);
close(src_fd);
return src_path;
}

TEST(Rename, AbsDesignationSame) {
const char *src_path = create_tmp_src("rename_test");
EXPECT_OK(rename(src_path, src_path));
unlink(src_path);
}

TEST(RenameAt, AbsDesignationSame) {
const char *src_path = create_tmp_src("renameat_test");
const char *dir_path = TmpFile("renameat_test_dir");

EXPECT_OK(mkdir(dir_path, 0755));
// random temporary directory descriptor
int dfd = open(dir_path, O_DIRECTORY);

// Various rename from/to the same absolute path; in each case the source
// and dest directory FDs should be irrelevant.
EXPECT_OK(renameat(AT_FDCWD, src_path, AT_FDCWD, src_path));
EXPECT_OK(renameat(AT_FDCWD, src_path, dfd, src_path));
EXPECT_OK(renameat(dfd, src_path, AT_FDCWD, src_path));
EXPECT_OK(renameat(dfd, src_path, dfd, src_path));

close(dfd);
rmdir(dir_path);
unlink(src_path);
}

0 comments on commit 7a79785

Please sign in to comment.