Path: blob/main/lib/libc/tests/secure/fortify_strings_test.c
39553 views
/* @generated by `generate-fortify-tests.lua "strings"` */12#define _FORTIFY_SOURCE 23#define TMPFILE_SIZE (1024 * 32)45#include <sys/param.h>6#include <sys/jail.h>7#include <sys/random.h>8#include <sys/resource.h>9#include <sys/select.h>10#include <sys/socket.h>11#include <sys/time.h>12#include <sys/uio.h>13#include <sys/wait.h>14#include <dirent.h>15#include <errno.h>16#include <fcntl.h>17#include <limits.h>18#include <poll.h>19#include <signal.h>20#include <stdio.h>21#include <stdlib.h>22#include <string.h>23#include <strings.h>24#include <sysexits.h>25#include <unistd.h>26#include <wchar.h>27#include <atf-c.h>2829static FILE * __unused30new_fp(size_t __len)31{32static char fpbuf[LINE_MAX];33FILE *fp;3435ATF_REQUIRE(__len <= sizeof(fpbuf));3637memset(fpbuf, 'A', sizeof(fpbuf) - 1);38fpbuf[sizeof(fpbuf) - 1] = '\0';3940fp = fmemopen(fpbuf, sizeof(fpbuf), "rb");41ATF_REQUIRE(fp != NULL);4243return (fp);44}4546/*47* Create a new symlink to use for readlink(2) style tests, we'll just use a48* random target name to have something interesting to look at.49*/50static const char * __unused51new_symlink(size_t __len)52{53static const char linkname[] = "link";54char target[MAXNAMLEN];55int error;5657ATF_REQUIRE(__len <= sizeof(target));5859arc4random_buf(target, sizeof(target));6061error = unlink(linkname);62ATF_REQUIRE(error == 0 || errno == ENOENT);6364error = symlink(target, linkname);65ATF_REQUIRE(error == 0);6667return (linkname);68}6970/*71* For our purposes, first descriptor will be the reader; we'll send both72* raw data and a control message over it so that the result can be used for73* any of our recv*() tests.74*/75static void __unused76new_socket(int sock[2])77{78unsigned char ctrl[CMSG_SPACE(sizeof(int))] = { 0 };79static char sockbuf[256];80ssize_t rv;81size_t total = 0;82struct msghdr hdr = { 0 };83struct cmsghdr *cmsg;84int error, fd;8586error = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);87ATF_REQUIRE(error == 0);8889while (total != sizeof(sockbuf)) {90rv = send(sock[1], &sockbuf[total], sizeof(sockbuf) - total, 0);9192ATF_REQUIRE_MSG(rv > 0,93"expected bytes sent, got %zd with %zu left (size %zu, total %zu)",94rv, sizeof(sockbuf) - total, sizeof(sockbuf), total);95ATF_REQUIRE_MSG(total + (size_t)rv <= sizeof(sockbuf),96"%zd exceeds total %zu", rv, sizeof(sockbuf));97total += rv;98}99100hdr.msg_control = ctrl;101hdr.msg_controllen = sizeof(ctrl);102103cmsg = CMSG_FIRSTHDR(&hdr);104cmsg->cmsg_level = SOL_SOCKET;105cmsg->cmsg_type = SCM_RIGHTS;106cmsg->cmsg_len = CMSG_LEN(sizeof(fd));107fd = STDIN_FILENO;108memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));109110error = sendmsg(sock[1], &hdr, 0);111ATF_REQUIRE(error != -1);112}113114/*115* Constructs a tmpfile that we can use for testing read(2) and friends.116*/117static int __unused118new_tmpfile(void)119{120char buf[1024];121ssize_t rv;122size_t written;123int fd;124125fd = open("tmpfile", O_RDWR | O_CREAT | O_TRUNC, 0644);126ATF_REQUIRE(fd >= 0);127128written = 0;129while (written < TMPFILE_SIZE) {130rv = write(fd, buf, sizeof(buf));131ATF_REQUIRE(rv > 0);132133written += rv;134}135136ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));137return (fd);138}139140static void141disable_coredumps(void)142{143struct rlimit rl = { 0 };144145if (setrlimit(RLIMIT_CORE, &rl) == -1)146_exit(EX_OSERR);147}148149/*150* Replaces stdin with a file that we can actually read from, for tests where151* we want a FILE * or fd that we can get data from.152*/153static void __unused154replace_stdin(void)155{156int fd;157158fd = new_tmpfile();159160(void)dup2(fd, STDIN_FILENO);161if (fd != STDIN_FILENO)162close(fd);163}164165ATF_TC(bcopy_before_end);166ATF_TC_HEAD(bcopy_before_end, tc)167{168}169ATF_TC_BODY(bcopy_before_end, tc)170{171#define BUF &__stack.__buf172struct {173uint8_t padding_l;174unsigned char __buf[42];175uint8_t padding_r;176} __stack;177const size_t __bufsz __unused = sizeof(__stack.__buf);178const size_t __len = 42 - 1;179const size_t __idx __unused = __len - 1;180char src[__len + 10];181182bcopy(src, __stack.__buf, __len);183#undef BUF184185}186187ATF_TC(bcopy_end);188ATF_TC_HEAD(bcopy_end, tc)189{190}191ATF_TC_BODY(bcopy_end, tc)192{193#define BUF &__stack.__buf194struct {195uint8_t padding_l;196unsigned char __buf[42];197uint8_t padding_r;198} __stack;199const size_t __bufsz __unused = sizeof(__stack.__buf);200const size_t __len = 42;201const size_t __idx __unused = __len - 1;202char src[__len + 10];203204bcopy(src, __stack.__buf, __len);205#undef BUF206207}208209ATF_TC(bcopy_heap_before_end);210ATF_TC_HEAD(bcopy_heap_before_end, tc)211{212}213ATF_TC_BODY(bcopy_heap_before_end, tc)214{215#define BUF __stack.__buf216struct {217uint8_t padding_l;218unsigned char * __buf;219uint8_t padding_r;220} __stack;221const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);222const size_t __len = 42 - 1;223const size_t __idx __unused = __len - 1;224char src[__len + 10];225226__stack.__buf = malloc(__bufsz);227228bcopy(src, __stack.__buf, __len);229#undef BUF230231}232233ATF_TC(bcopy_heap_end);234ATF_TC_HEAD(bcopy_heap_end, tc)235{236}237ATF_TC_BODY(bcopy_heap_end, tc)238{239#define BUF __stack.__buf240struct {241uint8_t padding_l;242unsigned char * __buf;243uint8_t padding_r;244} __stack;245const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);246const size_t __len = 42;247const size_t __idx __unused = __len - 1;248char src[__len + 10];249250__stack.__buf = malloc(__bufsz);251252bcopy(src, __stack.__buf, __len);253#undef BUF254255}256257ATF_TC(bcopy_heap_after_end);258ATF_TC_HEAD(bcopy_heap_after_end, tc)259{260}261ATF_TC_BODY(bcopy_heap_after_end, tc)262{263#define BUF __stack.__buf264struct {265uint8_t padding_l;266unsigned char * __buf;267uint8_t padding_r;268} __stack;269const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);270const size_t __len = 42 + 1;271const size_t __idx __unused = __len - 1;272pid_t __child;273int __status;274char src[__len + 10];275276__child = fork();277ATF_REQUIRE(__child >= 0);278if (__child > 0)279goto monitor;280281/* Child */282disable_coredumps();283__stack.__buf = malloc(__bufsz);284285bcopy(src, __stack.__buf, __len);286_exit(EX_SOFTWARE); /* Should have aborted. */287288monitor:289while (waitpid(__child, &__status, 0) != __child) {290ATF_REQUIRE_EQ(EINTR, errno);291}292293if (!WIFSIGNALED(__status)) {294switch (WEXITSTATUS(__status)) {295case EX_SOFTWARE:296atf_tc_fail("FORTIFY_SOURCE failed to abort");297break;298case EX_OSERR:299atf_tc_fail("setrlimit(2) failed");300break;301default:302atf_tc_fail("child exited with status %d",303WEXITSTATUS(__status));304}305} else {306ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));307}308#undef BUF309310}311312ATF_TC(bzero_before_end);313ATF_TC_HEAD(bzero_before_end, tc)314{315}316ATF_TC_BODY(bzero_before_end, tc)317{318#define BUF &__stack.__buf319struct {320uint8_t padding_l;321unsigned char __buf[42];322uint8_t padding_r;323} __stack;324const size_t __bufsz __unused = sizeof(__stack.__buf);325const size_t __len = 42 - 1;326const size_t __idx __unused = __len - 1;327328bzero(__stack.__buf, __len);329#undef BUF330331}332333ATF_TC(bzero_end);334ATF_TC_HEAD(bzero_end, tc)335{336}337ATF_TC_BODY(bzero_end, tc)338{339#define BUF &__stack.__buf340struct {341uint8_t padding_l;342unsigned char __buf[42];343uint8_t padding_r;344} __stack;345const size_t __bufsz __unused = sizeof(__stack.__buf);346const size_t __len = 42;347const size_t __idx __unused = __len - 1;348349bzero(__stack.__buf, __len);350#undef BUF351352}353354ATF_TC(bzero_heap_before_end);355ATF_TC_HEAD(bzero_heap_before_end, tc)356{357}358ATF_TC_BODY(bzero_heap_before_end, tc)359{360#define BUF __stack.__buf361struct {362uint8_t padding_l;363unsigned char * __buf;364uint8_t padding_r;365} __stack;366const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);367const size_t __len = 42 - 1;368const size_t __idx __unused = __len - 1;369370__stack.__buf = malloc(__bufsz);371372bzero(__stack.__buf, __len);373#undef BUF374375}376377ATF_TC(bzero_heap_end);378ATF_TC_HEAD(bzero_heap_end, tc)379{380}381ATF_TC_BODY(bzero_heap_end, tc)382{383#define BUF __stack.__buf384struct {385uint8_t padding_l;386unsigned char * __buf;387uint8_t padding_r;388} __stack;389const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);390const size_t __len = 42;391const size_t __idx __unused = __len - 1;392393__stack.__buf = malloc(__bufsz);394395bzero(__stack.__buf, __len);396#undef BUF397398}399400ATF_TC(bzero_heap_after_end);401ATF_TC_HEAD(bzero_heap_after_end, tc)402{403}404ATF_TC_BODY(bzero_heap_after_end, tc)405{406#define BUF __stack.__buf407struct {408uint8_t padding_l;409unsigned char * __buf;410uint8_t padding_r;411} __stack;412const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);413const size_t __len = 42 + 1;414const size_t __idx __unused = __len - 1;415pid_t __child;416int __status;417418__child = fork();419ATF_REQUIRE(__child >= 0);420if (__child > 0)421goto monitor;422423/* Child */424disable_coredumps();425__stack.__buf = malloc(__bufsz);426427bzero(__stack.__buf, __len);428_exit(EX_SOFTWARE); /* Should have aborted. */429430monitor:431while (waitpid(__child, &__status, 0) != __child) {432ATF_REQUIRE_EQ(EINTR, errno);433}434435if (!WIFSIGNALED(__status)) {436switch (WEXITSTATUS(__status)) {437case EX_SOFTWARE:438atf_tc_fail("FORTIFY_SOURCE failed to abort");439break;440case EX_OSERR:441atf_tc_fail("setrlimit(2) failed");442break;443default:444atf_tc_fail("child exited with status %d",445WEXITSTATUS(__status));446}447} else {448ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));449}450#undef BUF451452}453454ATF_TC(explicit_bzero_before_end);455ATF_TC_HEAD(explicit_bzero_before_end, tc)456{457}458ATF_TC_BODY(explicit_bzero_before_end, tc)459{460#define BUF &__stack.__buf461struct {462uint8_t padding_l;463unsigned char __buf[42];464uint8_t padding_r;465} __stack;466const size_t __bufsz __unused = sizeof(__stack.__buf);467const size_t __len = 42 - 1;468const size_t __idx __unused = __len - 1;469470explicit_bzero(__stack.__buf, __len);471#undef BUF472473}474475ATF_TC(explicit_bzero_end);476ATF_TC_HEAD(explicit_bzero_end, tc)477{478}479ATF_TC_BODY(explicit_bzero_end, tc)480{481#define BUF &__stack.__buf482struct {483uint8_t padding_l;484unsigned char __buf[42];485uint8_t padding_r;486} __stack;487const size_t __bufsz __unused = sizeof(__stack.__buf);488const size_t __len = 42;489const size_t __idx __unused = __len - 1;490491explicit_bzero(__stack.__buf, __len);492#undef BUF493494}495496ATF_TC(explicit_bzero_heap_before_end);497ATF_TC_HEAD(explicit_bzero_heap_before_end, tc)498{499}500ATF_TC_BODY(explicit_bzero_heap_before_end, tc)501{502#define BUF __stack.__buf503struct {504uint8_t padding_l;505unsigned char * __buf;506uint8_t padding_r;507} __stack;508const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);509const size_t __len = 42 - 1;510const size_t __idx __unused = __len - 1;511512__stack.__buf = malloc(__bufsz);513514explicit_bzero(__stack.__buf, __len);515#undef BUF516517}518519ATF_TC(explicit_bzero_heap_end);520ATF_TC_HEAD(explicit_bzero_heap_end, tc)521{522}523ATF_TC_BODY(explicit_bzero_heap_end, tc)524{525#define BUF __stack.__buf526struct {527uint8_t padding_l;528unsigned char * __buf;529uint8_t padding_r;530} __stack;531const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);532const size_t __len = 42;533const size_t __idx __unused = __len - 1;534535__stack.__buf = malloc(__bufsz);536537explicit_bzero(__stack.__buf, __len);538#undef BUF539540}541542ATF_TC(explicit_bzero_heap_after_end);543ATF_TC_HEAD(explicit_bzero_heap_after_end, tc)544{545}546ATF_TC_BODY(explicit_bzero_heap_after_end, tc)547{548#define BUF __stack.__buf549struct {550uint8_t padding_l;551unsigned char * __buf;552uint8_t padding_r;553} __stack;554const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);555const size_t __len = 42 + 1;556const size_t __idx __unused = __len - 1;557pid_t __child;558int __status;559560__child = fork();561ATF_REQUIRE(__child >= 0);562if (__child > 0)563goto monitor;564565/* Child */566disable_coredumps();567__stack.__buf = malloc(__bufsz);568569explicit_bzero(__stack.__buf, __len);570_exit(EX_SOFTWARE); /* Should have aborted. */571572monitor:573while (waitpid(__child, &__status, 0) != __child) {574ATF_REQUIRE_EQ(EINTR, errno);575}576577if (!WIFSIGNALED(__status)) {578switch (WEXITSTATUS(__status)) {579case EX_SOFTWARE:580atf_tc_fail("FORTIFY_SOURCE failed to abort");581break;582case EX_OSERR:583atf_tc_fail("setrlimit(2) failed");584break;585default:586atf_tc_fail("child exited with status %d",587WEXITSTATUS(__status));588}589} else {590ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));591}592#undef BUF593594}595596ATF_TP_ADD_TCS(tp)597{598ATF_TP_ADD_TC(tp, bcopy_before_end);599ATF_TP_ADD_TC(tp, bcopy_end);600ATF_TP_ADD_TC(tp, bcopy_heap_before_end);601ATF_TP_ADD_TC(tp, bcopy_heap_end);602ATF_TP_ADD_TC(tp, bcopy_heap_after_end);603ATF_TP_ADD_TC(tp, bzero_before_end);604ATF_TP_ADD_TC(tp, bzero_end);605ATF_TP_ADD_TC(tp, bzero_heap_before_end);606ATF_TP_ADD_TC(tp, bzero_heap_end);607ATF_TP_ADD_TC(tp, bzero_heap_after_end);608ATF_TP_ADD_TC(tp, explicit_bzero_before_end);609ATF_TP_ADD_TC(tp, explicit_bzero_end);610ATF_TP_ADD_TC(tp, explicit_bzero_heap_before_end);611ATF_TP_ADD_TC(tp, explicit_bzero_heap_end);612ATF_TP_ADD_TC(tp, explicit_bzero_heap_after_end);613return (atf_no_error());614}615616617