1224793Sjonathan/*- 2224793Sjonathan * Copyright (c) 2009-2011 Robert N. M. Watson 3224793Sjonathan * Copyright (c) 2011 Jonathan Anderson 4224793Sjonathan * All rights reserved. 5224793Sjonathan * 6224793Sjonathan * Redistribution and use in source and binary forms, with or without 7224793Sjonathan * modification, are permitted provided that the following conditions 8224793Sjonathan * are met: 9224793Sjonathan * 1. Redistributions of source code must retain the above copyright 10224793Sjonathan * notice, this list of conditions and the following disclaimer. 11224793Sjonathan * 2. Redistributions in binary form must reproduce the above copyright 12224793Sjonathan * notice, this list of conditions and the following disclaimer in the 13224793Sjonathan * documentation and/or other materials provided with the distribution. 14224793Sjonathan * 15224793Sjonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16224793Sjonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17224793Sjonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18224793Sjonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19224793Sjonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20224793Sjonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21224793Sjonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22224793Sjonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23224793Sjonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24224793Sjonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25224793Sjonathan * SUCH DAMAGE. 26224793Sjonathan * 27224793Sjonathan * $FreeBSD$ 28224793Sjonathan */ 29224793Sjonathan 30224793Sjonathan#include <sys/cdefs.h> 31224793Sjonathan__FBSDID("$FreeBSD$"); 32224793Sjonathan 33224793Sjonathan#include <sys/param.h> 34280250Srwatson#include <sys/capsicum.h> 35224793Sjonathan#include <sys/errno.h> 36224793Sjonathan 37224793Sjonathan#include <err.h> 38224793Sjonathan#include <fcntl.h> 39224793Sjonathan#include <stdlib.h> 40224793Sjonathan#include <string.h> 41224793Sjonathan#include <unistd.h> 42224793Sjonathan 43224793Sjonathan#include "cap_test.h" 44224793Sjonathan 45224793Sjonathan/* 46224793Sjonathan * Test openat(2) in a variety of sitations to ensure that it obeys Capsicum 47224793Sjonathan * "strict relative" rules: 48224793Sjonathan * 49224793Sjonathan * 1. Use strict relative lookups in capability mode or when operating 50224793Sjonathan * relative to a capability. 51224793Sjonathan * 2. When performing strict relative lookups, absolute paths (including 52224793Sjonathan * symlinks to absolute paths) are not allowed, nor are paths containing 53224793Sjonathan * '..' components. 54224793Sjonathan */ 55224793Sjonathanint 56224793Sjonathantest_relative(void) 57224793Sjonathan{ 58224793Sjonathan int success = PASSED; 59224793Sjonathan int fd, etc, etc_cap, etc_cap_ro, etc_cap_base, etc_cap_all; 60224793Sjonathan cap_rights_t baserights = CAP_READ | CAP_WRITE | CAP_SEEK | CAP_LOOKUP; 61224793Sjonathan cap_rights_t rights; 62224793Sjonathan 63224793Sjonathan REQUIRE(etc = open("/etc/", O_RDONLY)); 64247605Spjd CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc, &rights); 65247605Spjd CHECK_RIGHTS(rights, CAP_ALL); 66224793Sjonathan 67224793Sjonathan MAKE_CAPABILITY(etc_cap, etc, CAP_READ); 68224793Sjonathan MAKE_CAPABILITY(etc_cap_ro, etc, CAP_READ | CAP_LOOKUP); 69224793Sjonathan MAKE_CAPABILITY(etc_cap_base, etc, baserights); 70224793Sjonathan MAKE_CAPABILITY(etc_cap_all, etc, CAP_MASK_VALID); 71224793Sjonathan 72224793Sjonathan /* 73224793Sjonathan * openat(2) with regular file descriptors in non-capability mode 74224793Sjonathan * should Just Work (tm). 75224793Sjonathan */ 76224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc, "/etc/passwd", O_RDONLY); 77224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, AT_FDCWD, "/etc/passwd", O_RDONLY); 78224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY); 79224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc, "../etc/passwd", O_RDONLY); 80224793Sjonathan 81224793Sjonathan /* 82224793Sjonathan * Lookups relative to capabilities should be strictly relative. 83224793Sjonathan * 84224793Sjonathan * When not in capability mode, we don't actually require CAP_LOOKUP. 85224793Sjonathan */ 86224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY); 87224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY); 88224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY); 89224793Sjonathan 90224793Sjonathan CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY); 91224793Sjonathan CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY); 92224793Sjonathan 93224793Sjonathan /* 94224793Sjonathan * This requires discussion: do we treat a capability with 95224793Sjonathan * CAP_MASK_VALID *exactly* like a non-capability file descriptor 96224793Sjonathan * (currently, the implementation says yes)? 97224793Sjonathan */ 98224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "../etc/passwd", O_RDONLY); 99224793Sjonathan 100224793Sjonathan /* 101224793Sjonathan * A file opened relative to a capability should itself be a capability. 102224793Sjonathan */ 103224793Sjonathan CHECK_SYSCALL_SUCCEEDS(cap_getrights, etc_cap_base, &rights); 104224793Sjonathan 105224793Sjonathan REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY)); 106224793Sjonathan CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights); 107224793Sjonathan CHECK_RIGHTS(rights, baserights); 108224793Sjonathan 109224793Sjonathan /* 110224793Sjonathan * Enter capability mode; now ALL lookups are strictly relative. 111224793Sjonathan */ 112224793Sjonathan REQUIRE(cap_enter()); 113224793Sjonathan 114224793Sjonathan /* 115224793Sjonathan * Relative lookups on regular files or capabilities with CAP_LOOKUP 116224793Sjonathan * ought to succeed. 117224793Sjonathan */ 118224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc, "passwd", O_RDONLY); 119224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_ro, "passwd", O_RDONLY); 120224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_base, "passwd", O_RDONLY); 121224793Sjonathan CHECK_SYSCALL_SUCCEEDS(openat, etc_cap_all, "passwd", O_RDONLY); 122224793Sjonathan 123224793Sjonathan /* 124224793Sjonathan * Lookup relative to capabilities without CAP_LOOKUP should fail. 125224793Sjonathan */ 126224793Sjonathan CHECK_NOTCAPABLE(openat, etc_cap, "passwd", O_RDONLY); 127224793Sjonathan 128224793Sjonathan /* 129224793Sjonathan * Absolute lookups should fail. 130224793Sjonathan */ 131224793Sjonathan CHECK_CAPMODE(openat, AT_FDCWD, "/etc/passwd", O_RDONLY); 132224793Sjonathan CHECK_NOTCAPABLE(openat, etc, "/etc/passwd", O_RDONLY); 133224793Sjonathan 134224793Sjonathan /* 135224793Sjonathan * Lookups containing '..' should fail in capability mode. 136224793Sjonathan */ 137224793Sjonathan CHECK_NOTCAPABLE(openat, etc, "../etc/passwd", O_RDONLY); 138224793Sjonathan CHECK_NOTCAPABLE(openat, etc_cap_ro, "../etc/passwd", O_RDONLY); 139224793Sjonathan CHECK_NOTCAPABLE(openat, etc_cap_base, "../etc/passwd", O_RDONLY); 140224793Sjonathan 141224793Sjonathan REQUIRE(fd = openat(etc, "passwd", O_RDONLY)); 142224793Sjonathan CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights); 143224793Sjonathan 144224793Sjonathan /* 145224793Sjonathan * A file opened relative to a capability should itself be a capability. 146224793Sjonathan */ 147224793Sjonathan REQUIRE(fd = openat(etc_cap_base, "passwd", O_RDONLY)); 148224793Sjonathan CHECK_SYSCALL_SUCCEEDS(cap_getrights, fd, &rights); 149224793Sjonathan CHECK_RIGHTS(rights, baserights); 150224793Sjonathan 151224793Sjonathan return success; 152224793Sjonathan} 153