Path: blob/main/lib/libc/tests/gen/realpath2_test.c
39500 views
/*1* Copyright (c) 2017 Jan Kokemüller2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <errno.h>28#include <fcntl.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <unistd.h>3334#include <atf-c.h>3536ATF_TC(realpath_buffer_overflow);37ATF_TC_HEAD(realpath_buffer_overflow, tc)38{39atf_tc_set_md_var(tc, "descr",40"Test for out of bounds read from 'left' array "41"(compile realpath.c with '-fsanitize=address')");42}4344ATF_TC_BODY(realpath_buffer_overflow, tc)45{46char path[MAXPATHLEN] = { 0 };47char resb[MAXPATHLEN] = { 0 };48size_t i;4950path[0] = 'a';51path[1] = '/';52for (i = 2; i < sizeof(path) - 1; ++i) {53path[i] = 'a';54}5556ATF_REQUIRE(realpath(path, resb) == NULL);57}5859ATF_TC(realpath_empty_symlink);60ATF_TC_HEAD(realpath_empty_symlink, tc)61{62atf_tc_set_md_var(tc, "descr",63"Test for correct behavior when encountering empty symlinks");64}6566ATF_TC_BODY(realpath_empty_symlink, tc)67{68char path[MAXPATHLEN] = { 0 };69char slnk[MAXPATHLEN] = { 0 };70char resb[MAXPATHLEN] = { 0 };71int fd;7273(void)strlcat(slnk, "empty_symlink", sizeof(slnk));7475ATF_REQUIRE(symlink("", slnk) == 0);7677fd = open("aaa", O_RDONLY | O_CREAT, 0600);7879ATF_REQUIRE(fd >= 0);80ATF_REQUIRE(close(fd) == 0);8182(void)strlcat(path, "empty_symlink", sizeof(path));83(void)strlcat(path, "/aaa", sizeof(path));8485ATF_REQUIRE_ERRNO(ENOENT, realpath(path, resb) == NULL);8687ATF_REQUIRE(unlink("aaa") == 0);88ATF_REQUIRE(unlink(slnk) == 0);89}9091ATF_TP_ADD_TCS(tp)92{9394ATF_TP_ADD_TC(tp, realpath_buffer_overflow);95ATF_TP_ADD_TC(tp, realpath_empty_symlink);9697return atf_no_error();98}99100101