Path: blob/main/lib/libc/tests/secure/fortify_unistd_test.c
39553 views
/* @generated by `generate-fortify-tests.lua "unistd"` */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}164165#define JAIL_HOSTNAME "host.example.com"166#define JAIL_DOMAINNAME "example.com"167static void168dhost_jail(void)169{170struct iovec iov[4];171int jid;172173iov[0].iov_base = __DECONST(char *, "host.hostname");174iov[0].iov_len = sizeof("host.hostname");175iov[1].iov_base = __DECONST(char *, JAIL_HOSTNAME);176iov[1].iov_len = sizeof(JAIL_HOSTNAME);177iov[2].iov_base = __DECONST(char *, "host.domainname");178iov[2].iov_len = sizeof("host.domainname");179iov[3].iov_base = __DECONST(char *, JAIL_DOMAINNAME);180iov[3].iov_len = sizeof(JAIL_DOMAINNAME);181182jid = jail_set(iov, nitems(iov), JAIL_CREATE | JAIL_ATTACH);183ATF_REQUIRE_MSG(jid > 0, "Jail creation failed: %s", strerror(errno));184}185186ATF_TC(getcwd_before_end);187ATF_TC_HEAD(getcwd_before_end, tc)188{189}190ATF_TC_BODY(getcwd_before_end, tc)191{192#define BUF &__stack.__buf193struct {194uint8_t padding_l;195unsigned char __buf[8];196uint8_t padding_r;197} __stack;198const size_t __bufsz __unused = sizeof(__stack.__buf);199const size_t __len = 8 - 1;200const size_t __idx __unused = __len - 1;201202getcwd(__stack.__buf, __len);203#undef BUF204205}206207ATF_TC(getcwd_end);208ATF_TC_HEAD(getcwd_end, tc)209{210}211ATF_TC_BODY(getcwd_end, tc)212{213#define BUF &__stack.__buf214struct {215uint8_t padding_l;216unsigned char __buf[8];217uint8_t padding_r;218} __stack;219const size_t __bufsz __unused = sizeof(__stack.__buf);220const size_t __len = 8;221const size_t __idx __unused = __len - 1;222223getcwd(__stack.__buf, __len);224#undef BUF225226}227228ATF_TC(getcwd_heap_before_end);229ATF_TC_HEAD(getcwd_heap_before_end, tc)230{231}232ATF_TC_BODY(getcwd_heap_before_end, tc)233{234#define BUF __stack.__buf235struct {236uint8_t padding_l;237unsigned char * __buf;238uint8_t padding_r;239} __stack;240const size_t __bufsz __unused = sizeof(*__stack.__buf) * (8);241const size_t __len = 8 - 1;242const size_t __idx __unused = __len - 1;243244__stack.__buf = malloc(__bufsz);245246getcwd(__stack.__buf, __len);247#undef BUF248249}250251ATF_TC(getcwd_heap_end);252ATF_TC_HEAD(getcwd_heap_end, tc)253{254}255ATF_TC_BODY(getcwd_heap_end, tc)256{257#define BUF __stack.__buf258struct {259uint8_t padding_l;260unsigned char * __buf;261uint8_t padding_r;262} __stack;263const size_t __bufsz __unused = sizeof(*__stack.__buf) * (8);264const size_t __len = 8;265const size_t __idx __unused = __len - 1;266267__stack.__buf = malloc(__bufsz);268269getcwd(__stack.__buf, __len);270#undef BUF271272}273274ATF_TC(getcwd_heap_after_end);275ATF_TC_HEAD(getcwd_heap_after_end, tc)276{277}278ATF_TC_BODY(getcwd_heap_after_end, tc)279{280#define BUF __stack.__buf281struct {282uint8_t padding_l;283unsigned char * __buf;284uint8_t padding_r;285} __stack;286const size_t __bufsz __unused = sizeof(*__stack.__buf) * (8);287const size_t __len = 8 + 1;288const size_t __idx __unused = __len - 1;289pid_t __child;290int __status;291292__child = fork();293ATF_REQUIRE(__child >= 0);294if (__child > 0)295goto monitor;296297/* Child */298disable_coredumps();299__stack.__buf = malloc(__bufsz);300301getcwd(__stack.__buf, __len);302_exit(EX_SOFTWARE); /* Should have aborted. */303304monitor:305while (waitpid(__child, &__status, 0) != __child) {306ATF_REQUIRE_EQ(EINTR, errno);307}308309if (!WIFSIGNALED(__status)) {310switch (WEXITSTATUS(__status)) {311case EX_SOFTWARE:312atf_tc_fail("FORTIFY_SOURCE failed to abort");313break;314case EX_OSERR:315atf_tc_fail("setrlimit(2) failed");316break;317default:318atf_tc_fail("child exited with status %d",319WEXITSTATUS(__status));320}321} else {322ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));323}324#undef BUF325326}327328ATF_TC(getgrouplist_before_end);329ATF_TC_HEAD(getgrouplist_before_end, tc)330{331}332ATF_TC_BODY(getgrouplist_before_end, tc)333{334#define BUF &__stack.__buf335struct {336uint8_t padding_l;337gid_t __buf[4];338uint8_t padding_r;339} __stack;340const size_t __bufsz __unused = sizeof(__stack.__buf);341const size_t __len = 4 - 1;342const size_t __idx __unused = __len - 1;343int intlen = (int)__len;344345getgrouplist("root", 0, __stack.__buf, &intlen);346#undef BUF347348}349350ATF_TC(getgrouplist_end);351ATF_TC_HEAD(getgrouplist_end, tc)352{353}354ATF_TC_BODY(getgrouplist_end, tc)355{356#define BUF &__stack.__buf357struct {358uint8_t padding_l;359gid_t __buf[4];360uint8_t padding_r;361} __stack;362const size_t __bufsz __unused = sizeof(__stack.__buf);363const size_t __len = 4;364const size_t __idx __unused = __len - 1;365int intlen = (int)__len;366367getgrouplist("root", 0, __stack.__buf, &intlen);368#undef BUF369370}371372ATF_TC(getgrouplist_heap_before_end);373ATF_TC_HEAD(getgrouplist_heap_before_end, tc)374{375}376ATF_TC_BODY(getgrouplist_heap_before_end, tc)377{378#define BUF __stack.__buf379struct {380uint8_t padding_l;381gid_t * __buf;382uint8_t padding_r;383} __stack;384const size_t __bufsz __unused = sizeof(*__stack.__buf) * (4);385const size_t __len = 4 - 1;386const size_t __idx __unused = __len - 1;387int intlen = (int)__len;388389__stack.__buf = malloc(__bufsz);390391getgrouplist("root", 0, __stack.__buf, &intlen);392#undef BUF393394}395396ATF_TC(getgrouplist_heap_end);397ATF_TC_HEAD(getgrouplist_heap_end, tc)398{399}400ATF_TC_BODY(getgrouplist_heap_end, tc)401{402#define BUF __stack.__buf403struct {404uint8_t padding_l;405gid_t * __buf;406uint8_t padding_r;407} __stack;408const size_t __bufsz __unused = sizeof(*__stack.__buf) * (4);409const size_t __len = 4;410const size_t __idx __unused = __len - 1;411int intlen = (int)__len;412413__stack.__buf = malloc(__bufsz);414415getgrouplist("root", 0, __stack.__buf, &intlen);416#undef BUF417418}419420ATF_TC(getgrouplist_heap_after_end);421ATF_TC_HEAD(getgrouplist_heap_after_end, tc)422{423}424ATF_TC_BODY(getgrouplist_heap_after_end, tc)425{426#define BUF __stack.__buf427struct {428uint8_t padding_l;429gid_t * __buf;430uint8_t padding_r;431} __stack;432const size_t __bufsz __unused = sizeof(*__stack.__buf) * (4);433const size_t __len = 4 + 1;434const size_t __idx __unused = __len - 1;435pid_t __child;436int __status;437int intlen = (int)__len;438439__child = fork();440ATF_REQUIRE(__child >= 0);441if (__child > 0)442goto monitor;443444/* Child */445disable_coredumps();446__stack.__buf = malloc(__bufsz);447448getgrouplist("root", 0, __stack.__buf, &intlen);449_exit(EX_SOFTWARE); /* Should have aborted. */450451monitor:452while (waitpid(__child, &__status, 0) != __child) {453ATF_REQUIRE_EQ(EINTR, errno);454}455456if (!WIFSIGNALED(__status)) {457switch (WEXITSTATUS(__status)) {458case EX_SOFTWARE:459atf_tc_fail("FORTIFY_SOURCE failed to abort");460break;461case EX_OSERR:462atf_tc_fail("setrlimit(2) failed");463break;464default:465atf_tc_fail("child exited with status %d",466WEXITSTATUS(__status));467}468} else {469ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));470}471#undef BUF472473}474475ATF_TC(getgroups_before_end);476ATF_TC_HEAD(getgroups_before_end, tc)477{478}479ATF_TC_BODY(getgroups_before_end, tc)480{481#define BUF &__stack.__buf482struct {483uint8_t padding_l;484gid_t __buf[4];485uint8_t padding_r;486} __stack;487const size_t __bufsz __unused = sizeof(__stack.__buf);488const size_t __len = 4 - 1;489const size_t __idx __unused = __len - 1;490491getgroups(__len, __stack.__buf);492#undef BUF493494}495496ATF_TC(getgroups_end);497ATF_TC_HEAD(getgroups_end, tc)498{499}500ATF_TC_BODY(getgroups_end, tc)501{502#define BUF &__stack.__buf503struct {504uint8_t padding_l;505gid_t __buf[4];506uint8_t padding_r;507} __stack;508const size_t __bufsz __unused = sizeof(__stack.__buf);509const size_t __len = 4;510const size_t __idx __unused = __len - 1;511512getgroups(__len, __stack.__buf);513#undef BUF514515}516517ATF_TC(getgroups_heap_before_end);518ATF_TC_HEAD(getgroups_heap_before_end, tc)519{520}521ATF_TC_BODY(getgroups_heap_before_end, tc)522{523#define BUF __stack.__buf524struct {525uint8_t padding_l;526gid_t * __buf;527uint8_t padding_r;528} __stack;529const size_t __bufsz __unused = sizeof(*__stack.__buf) * (4);530const size_t __len = 4 - 1;531const size_t __idx __unused = __len - 1;532533__stack.__buf = malloc(__bufsz);534535getgroups(__len, __stack.__buf);536#undef BUF537538}539540ATF_TC(getgroups_heap_end);541ATF_TC_HEAD(getgroups_heap_end, tc)542{543}544ATF_TC_BODY(getgroups_heap_end, tc)545{546#define BUF __stack.__buf547struct {548uint8_t padding_l;549gid_t * __buf;550uint8_t padding_r;551} __stack;552const size_t __bufsz __unused = sizeof(*__stack.__buf) * (4);553const size_t __len = 4;554const size_t __idx __unused = __len - 1;555556__stack.__buf = malloc(__bufsz);557558getgroups(__len, __stack.__buf);559#undef BUF560561}562563ATF_TC(getgroups_heap_after_end);564ATF_TC_HEAD(getgroups_heap_after_end, tc)565{566}567ATF_TC_BODY(getgroups_heap_after_end, tc)568{569#define BUF __stack.__buf570struct {571uint8_t padding_l;572gid_t * __buf;573uint8_t padding_r;574} __stack;575const size_t __bufsz __unused = sizeof(*__stack.__buf) * (4);576const size_t __len = 4 + 1;577const size_t __idx __unused = __len - 1;578pid_t __child;579int __status;580581__child = fork();582ATF_REQUIRE(__child >= 0);583if (__child > 0)584goto monitor;585586/* Child */587disable_coredumps();588__stack.__buf = malloc(__bufsz);589590getgroups(__len, __stack.__buf);591_exit(EX_SOFTWARE); /* Should have aborted. */592593monitor:594while (waitpid(__child, &__status, 0) != __child) {595ATF_REQUIRE_EQ(EINTR, errno);596}597598if (!WIFSIGNALED(__status)) {599switch (WEXITSTATUS(__status)) {600case EX_SOFTWARE:601atf_tc_fail("FORTIFY_SOURCE failed to abort");602break;603case EX_OSERR:604atf_tc_fail("setrlimit(2) failed");605break;606default:607atf_tc_fail("child exited with status %d",608WEXITSTATUS(__status));609}610} else {611ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));612}613#undef BUF614615}616617ATF_TC(getloginclass_before_end);618ATF_TC_HEAD(getloginclass_before_end, tc)619{620}621ATF_TC_BODY(getloginclass_before_end, tc)622{623#define BUF &__stack.__buf624struct {625uint8_t padding_l;626unsigned char __buf[42];627uint8_t padding_r;628} __stack;629const size_t __bufsz __unused = sizeof(__stack.__buf);630const size_t __len = 42 - 1;631const size_t __idx __unused = __len - 1;632633getloginclass(__stack.__buf, __len);634#undef BUF635636}637638ATF_TC(getloginclass_end);639ATF_TC_HEAD(getloginclass_end, tc)640{641}642ATF_TC_BODY(getloginclass_end, tc)643{644#define BUF &__stack.__buf645struct {646uint8_t padding_l;647unsigned char __buf[42];648uint8_t padding_r;649} __stack;650const size_t __bufsz __unused = sizeof(__stack.__buf);651const size_t __len = 42;652const size_t __idx __unused = __len - 1;653654getloginclass(__stack.__buf, __len);655#undef BUF656657}658659ATF_TC(getloginclass_heap_before_end);660ATF_TC_HEAD(getloginclass_heap_before_end, tc)661{662}663ATF_TC_BODY(getloginclass_heap_before_end, tc)664{665#define BUF __stack.__buf666struct {667uint8_t padding_l;668unsigned char * __buf;669uint8_t padding_r;670} __stack;671const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);672const size_t __len = 42 - 1;673const size_t __idx __unused = __len - 1;674675__stack.__buf = malloc(__bufsz);676677getloginclass(__stack.__buf, __len);678#undef BUF679680}681682ATF_TC(getloginclass_heap_end);683ATF_TC_HEAD(getloginclass_heap_end, tc)684{685}686ATF_TC_BODY(getloginclass_heap_end, tc)687{688#define BUF __stack.__buf689struct {690uint8_t padding_l;691unsigned char * __buf;692uint8_t padding_r;693} __stack;694const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);695const size_t __len = 42;696const size_t __idx __unused = __len - 1;697698__stack.__buf = malloc(__bufsz);699700getloginclass(__stack.__buf, __len);701#undef BUF702703}704705ATF_TC(getloginclass_heap_after_end);706ATF_TC_HEAD(getloginclass_heap_after_end, tc)707{708}709ATF_TC_BODY(getloginclass_heap_after_end, tc)710{711#define BUF __stack.__buf712struct {713uint8_t padding_l;714unsigned char * __buf;715uint8_t padding_r;716} __stack;717const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);718const size_t __len = 42 + 1;719const size_t __idx __unused = __len - 1;720pid_t __child;721int __status;722723__child = fork();724ATF_REQUIRE(__child >= 0);725if (__child > 0)726goto monitor;727728/* Child */729disable_coredumps();730__stack.__buf = malloc(__bufsz);731732getloginclass(__stack.__buf, __len);733_exit(EX_SOFTWARE); /* Should have aborted. */734735monitor:736while (waitpid(__child, &__status, 0) != __child) {737ATF_REQUIRE_EQ(EINTR, errno);738}739740if (!WIFSIGNALED(__status)) {741switch (WEXITSTATUS(__status)) {742case EX_SOFTWARE:743atf_tc_fail("FORTIFY_SOURCE failed to abort");744break;745case EX_OSERR:746atf_tc_fail("setrlimit(2) failed");747break;748default:749atf_tc_fail("child exited with status %d",750WEXITSTATUS(__status));751}752} else {753ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));754}755#undef BUF756757}758759ATF_TC(pread_before_end);760ATF_TC_HEAD(pread_before_end, tc)761{762}763ATF_TC_BODY(pread_before_end, tc)764{765#define BUF &__stack.__buf766struct {767uint8_t padding_l;768unsigned char __buf[41];769uint8_t padding_r;770} __stack;771const size_t __bufsz __unused = sizeof(__stack.__buf);772const size_t __len = 41 - 1;773const size_t __idx __unused = __len - 1;774int fd;775776fd = new_tmpfile(); /* Cannot fail */777778pread(fd, __stack.__buf, __len, 0);779#undef BUF780781}782783ATF_TC(pread_end);784ATF_TC_HEAD(pread_end, tc)785{786}787ATF_TC_BODY(pread_end, tc)788{789#define BUF &__stack.__buf790struct {791uint8_t padding_l;792unsigned char __buf[41];793uint8_t padding_r;794} __stack;795const size_t __bufsz __unused = sizeof(__stack.__buf);796const size_t __len = 41;797const size_t __idx __unused = __len - 1;798int fd;799800fd = new_tmpfile(); /* Cannot fail */801802pread(fd, __stack.__buf, __len, 0);803#undef BUF804805}806807ATF_TC(pread_heap_before_end);808ATF_TC_HEAD(pread_heap_before_end, tc)809{810}811ATF_TC_BODY(pread_heap_before_end, tc)812{813#define BUF __stack.__buf814struct {815uint8_t padding_l;816unsigned char * __buf;817uint8_t padding_r;818} __stack;819const size_t __bufsz __unused = sizeof(*__stack.__buf) * (41);820const size_t __len = 41 - 1;821const size_t __idx __unused = __len - 1;822int fd;823824__stack.__buf = malloc(__bufsz);825fd = new_tmpfile(); /* Cannot fail */826827pread(fd, __stack.__buf, __len, 0);828#undef BUF829830}831832ATF_TC(pread_heap_end);833ATF_TC_HEAD(pread_heap_end, tc)834{835}836ATF_TC_BODY(pread_heap_end, tc)837{838#define BUF __stack.__buf839struct {840uint8_t padding_l;841unsigned char * __buf;842uint8_t padding_r;843} __stack;844const size_t __bufsz __unused = sizeof(*__stack.__buf) * (41);845const size_t __len = 41;846const size_t __idx __unused = __len - 1;847int fd;848849__stack.__buf = malloc(__bufsz);850fd = new_tmpfile(); /* Cannot fail */851852pread(fd, __stack.__buf, __len, 0);853#undef BUF854855}856857ATF_TC(pread_heap_after_end);858ATF_TC_HEAD(pread_heap_after_end, tc)859{860}861ATF_TC_BODY(pread_heap_after_end, tc)862{863#define BUF __stack.__buf864struct {865uint8_t padding_l;866unsigned char * __buf;867uint8_t padding_r;868} __stack;869const size_t __bufsz __unused = sizeof(*__stack.__buf) * (41);870const size_t __len = 41 + 1;871const size_t __idx __unused = __len - 1;872pid_t __child;873int __status;874int fd;875876__child = fork();877ATF_REQUIRE(__child >= 0);878if (__child > 0)879goto monitor;880881/* Child */882disable_coredumps();883__stack.__buf = malloc(__bufsz);884fd = new_tmpfile(); /* Cannot fail */885886pread(fd, __stack.__buf, __len, 0);887_exit(EX_SOFTWARE); /* Should have aborted. */888889monitor:890while (waitpid(__child, &__status, 0) != __child) {891ATF_REQUIRE_EQ(EINTR, errno);892}893894if (!WIFSIGNALED(__status)) {895switch (WEXITSTATUS(__status)) {896case EX_SOFTWARE:897atf_tc_fail("FORTIFY_SOURCE failed to abort");898break;899case EX_OSERR:900atf_tc_fail("setrlimit(2) failed");901break;902default:903atf_tc_fail("child exited with status %d",904WEXITSTATUS(__status));905}906} else {907ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));908}909#undef BUF910911}912913ATF_TC(read_before_end);914ATF_TC_HEAD(read_before_end, tc)915{916}917ATF_TC_BODY(read_before_end, tc)918{919#define BUF &__stack.__buf920struct {921uint8_t padding_l;922unsigned char __buf[41];923uint8_t padding_r;924} __stack;925const size_t __bufsz __unused = sizeof(__stack.__buf);926const size_t __len = 41 - 1;927const size_t __idx __unused = __len - 1;928int fd;929930fd = new_tmpfile(); /* Cannot fail */931932read(fd, __stack.__buf, __len);933#undef BUF934935}936937ATF_TC(read_end);938ATF_TC_HEAD(read_end, tc)939{940}941ATF_TC_BODY(read_end, tc)942{943#define BUF &__stack.__buf944struct {945uint8_t padding_l;946unsigned char __buf[41];947uint8_t padding_r;948} __stack;949const size_t __bufsz __unused = sizeof(__stack.__buf);950const size_t __len = 41;951const size_t __idx __unused = __len - 1;952int fd;953954fd = new_tmpfile(); /* Cannot fail */955956read(fd, __stack.__buf, __len);957#undef BUF958959}960961ATF_TC(read_heap_before_end);962ATF_TC_HEAD(read_heap_before_end, tc)963{964}965ATF_TC_BODY(read_heap_before_end, tc)966{967#define BUF __stack.__buf968struct {969uint8_t padding_l;970unsigned char * __buf;971uint8_t padding_r;972} __stack;973const size_t __bufsz __unused = sizeof(*__stack.__buf) * (41);974const size_t __len = 41 - 1;975const size_t __idx __unused = __len - 1;976int fd;977978__stack.__buf = malloc(__bufsz);979fd = new_tmpfile(); /* Cannot fail */980981read(fd, __stack.__buf, __len);982#undef BUF983984}985986ATF_TC(read_heap_end);987ATF_TC_HEAD(read_heap_end, tc)988{989}990ATF_TC_BODY(read_heap_end, tc)991{992#define BUF __stack.__buf993struct {994uint8_t padding_l;995unsigned char * __buf;996uint8_t padding_r;997} __stack;998const size_t __bufsz __unused = sizeof(*__stack.__buf) * (41);999const size_t __len = 41;1000const size_t __idx __unused = __len - 1;1001int fd;10021003__stack.__buf = malloc(__bufsz);1004fd = new_tmpfile(); /* Cannot fail */10051006read(fd, __stack.__buf, __len);1007#undef BUF10081009}10101011ATF_TC(read_heap_after_end);1012ATF_TC_HEAD(read_heap_after_end, tc)1013{1014}1015ATF_TC_BODY(read_heap_after_end, tc)1016{1017#define BUF __stack.__buf1018struct {1019uint8_t padding_l;1020unsigned char * __buf;1021uint8_t padding_r;1022} __stack;1023const size_t __bufsz __unused = sizeof(*__stack.__buf) * (41);1024const size_t __len = 41 + 1;1025const size_t __idx __unused = __len - 1;1026pid_t __child;1027int __status;1028int fd;10291030__child = fork();1031ATF_REQUIRE(__child >= 0);1032if (__child > 0)1033goto monitor;10341035/* Child */1036disable_coredumps();1037__stack.__buf = malloc(__bufsz);1038fd = new_tmpfile(); /* Cannot fail */10391040read(fd, __stack.__buf, __len);1041_exit(EX_SOFTWARE); /* Should have aborted. */10421043monitor:1044while (waitpid(__child, &__status, 0) != __child) {1045ATF_REQUIRE_EQ(EINTR, errno);1046}10471048if (!WIFSIGNALED(__status)) {1049switch (WEXITSTATUS(__status)) {1050case EX_SOFTWARE:1051atf_tc_fail("FORTIFY_SOURCE failed to abort");1052break;1053case EX_OSERR:1054atf_tc_fail("setrlimit(2) failed");1055break;1056default:1057atf_tc_fail("child exited with status %d",1058WEXITSTATUS(__status));1059}1060} else {1061ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1062}1063#undef BUF10641065}10661067ATF_TC(readlink_before_end);1068ATF_TC_HEAD(readlink_before_end, tc)1069{1070}1071ATF_TC_BODY(readlink_before_end, tc)1072{1073#define BUF &__stack.__buf1074struct {1075uint8_t padding_l;1076unsigned char __buf[42];1077uint8_t padding_r;1078} __stack;1079const size_t __bufsz __unused = sizeof(__stack.__buf);1080const size_t __len = 42 - 1;1081const size_t __idx __unused = __len - 1;1082const char *path;10831084path = new_symlink(__len); /* Cannot fail */10851086readlink(path, __stack.__buf, __len);1087#undef BUF10881089}10901091ATF_TC(readlink_end);1092ATF_TC_HEAD(readlink_end, tc)1093{1094}1095ATF_TC_BODY(readlink_end, tc)1096{1097#define BUF &__stack.__buf1098struct {1099uint8_t padding_l;1100unsigned char __buf[42];1101uint8_t padding_r;1102} __stack;1103const size_t __bufsz __unused = sizeof(__stack.__buf);1104const size_t __len = 42;1105const size_t __idx __unused = __len - 1;1106const char *path;11071108path = new_symlink(__len); /* Cannot fail */11091110readlink(path, __stack.__buf, __len);1111#undef BUF11121113}11141115ATF_TC(readlink_heap_before_end);1116ATF_TC_HEAD(readlink_heap_before_end, tc)1117{1118}1119ATF_TC_BODY(readlink_heap_before_end, tc)1120{1121#define BUF __stack.__buf1122struct {1123uint8_t padding_l;1124unsigned char * __buf;1125uint8_t padding_r;1126} __stack;1127const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1128const size_t __len = 42 - 1;1129const size_t __idx __unused = __len - 1;1130const char *path;11311132__stack.__buf = malloc(__bufsz);1133path = new_symlink(__len); /* Cannot fail */11341135readlink(path, __stack.__buf, __len);1136#undef BUF11371138}11391140ATF_TC(readlink_heap_end);1141ATF_TC_HEAD(readlink_heap_end, tc)1142{1143}1144ATF_TC_BODY(readlink_heap_end, tc)1145{1146#define BUF __stack.__buf1147struct {1148uint8_t padding_l;1149unsigned char * __buf;1150uint8_t padding_r;1151} __stack;1152const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1153const size_t __len = 42;1154const size_t __idx __unused = __len - 1;1155const char *path;11561157__stack.__buf = malloc(__bufsz);1158path = new_symlink(__len); /* Cannot fail */11591160readlink(path, __stack.__buf, __len);1161#undef BUF11621163}11641165ATF_TC(readlink_heap_after_end);1166ATF_TC_HEAD(readlink_heap_after_end, tc)1167{1168}1169ATF_TC_BODY(readlink_heap_after_end, tc)1170{1171#define BUF __stack.__buf1172struct {1173uint8_t padding_l;1174unsigned char * __buf;1175uint8_t padding_r;1176} __stack;1177const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1178const size_t __len = 42 + 1;1179const size_t __idx __unused = __len - 1;1180pid_t __child;1181int __status;1182const char *path;11831184__child = fork();1185ATF_REQUIRE(__child >= 0);1186if (__child > 0)1187goto monitor;11881189/* Child */1190disable_coredumps();1191__stack.__buf = malloc(__bufsz);1192path = new_symlink(__len); /* Cannot fail */11931194readlink(path, __stack.__buf, __len);1195_exit(EX_SOFTWARE); /* Should have aborted. */11961197monitor:1198while (waitpid(__child, &__status, 0) != __child) {1199ATF_REQUIRE_EQ(EINTR, errno);1200}12011202if (!WIFSIGNALED(__status)) {1203switch (WEXITSTATUS(__status)) {1204case EX_SOFTWARE:1205atf_tc_fail("FORTIFY_SOURCE failed to abort");1206break;1207case EX_OSERR:1208atf_tc_fail("setrlimit(2) failed");1209break;1210default:1211atf_tc_fail("child exited with status %d",1212WEXITSTATUS(__status));1213}1214} else {1215ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1216}1217#undef BUF12181219}12201221ATF_TC(readlinkat_before_end);1222ATF_TC_HEAD(readlinkat_before_end, tc)1223{1224}1225ATF_TC_BODY(readlinkat_before_end, tc)1226{1227#define BUF &__stack.__buf1228struct {1229uint8_t padding_l;1230unsigned char __buf[42];1231uint8_t padding_r;1232} __stack;1233const size_t __bufsz __unused = sizeof(__stack.__buf);1234const size_t __len = 42 - 1;1235const size_t __idx __unused = __len - 1;1236const char *path;12371238path = new_symlink(__len); /* Cannot fail */12391240readlinkat(AT_FDCWD, path, __stack.__buf, __len);1241#undef BUF12421243}12441245ATF_TC(readlinkat_end);1246ATF_TC_HEAD(readlinkat_end, tc)1247{1248}1249ATF_TC_BODY(readlinkat_end, tc)1250{1251#define BUF &__stack.__buf1252struct {1253uint8_t padding_l;1254unsigned char __buf[42];1255uint8_t padding_r;1256} __stack;1257const size_t __bufsz __unused = sizeof(__stack.__buf);1258const size_t __len = 42;1259const size_t __idx __unused = __len - 1;1260const char *path;12611262path = new_symlink(__len); /* Cannot fail */12631264readlinkat(AT_FDCWD, path, __stack.__buf, __len);1265#undef BUF12661267}12681269ATF_TC(readlinkat_heap_before_end);1270ATF_TC_HEAD(readlinkat_heap_before_end, tc)1271{1272}1273ATF_TC_BODY(readlinkat_heap_before_end, tc)1274{1275#define BUF __stack.__buf1276struct {1277uint8_t padding_l;1278unsigned char * __buf;1279uint8_t padding_r;1280} __stack;1281const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1282const size_t __len = 42 - 1;1283const size_t __idx __unused = __len - 1;1284const char *path;12851286__stack.__buf = malloc(__bufsz);1287path = new_symlink(__len); /* Cannot fail */12881289readlinkat(AT_FDCWD, path, __stack.__buf, __len);1290#undef BUF12911292}12931294ATF_TC(readlinkat_heap_end);1295ATF_TC_HEAD(readlinkat_heap_end, tc)1296{1297}1298ATF_TC_BODY(readlinkat_heap_end, tc)1299{1300#define BUF __stack.__buf1301struct {1302uint8_t padding_l;1303unsigned char * __buf;1304uint8_t padding_r;1305} __stack;1306const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1307const size_t __len = 42;1308const size_t __idx __unused = __len - 1;1309const char *path;13101311__stack.__buf = malloc(__bufsz);1312path = new_symlink(__len); /* Cannot fail */13131314readlinkat(AT_FDCWD, path, __stack.__buf, __len);1315#undef BUF13161317}13181319ATF_TC(readlinkat_heap_after_end);1320ATF_TC_HEAD(readlinkat_heap_after_end, tc)1321{1322}1323ATF_TC_BODY(readlinkat_heap_after_end, tc)1324{1325#define BUF __stack.__buf1326struct {1327uint8_t padding_l;1328unsigned char * __buf;1329uint8_t padding_r;1330} __stack;1331const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1332const size_t __len = 42 + 1;1333const size_t __idx __unused = __len - 1;1334pid_t __child;1335int __status;1336const char *path;13371338__child = fork();1339ATF_REQUIRE(__child >= 0);1340if (__child > 0)1341goto monitor;13421343/* Child */1344disable_coredumps();1345__stack.__buf = malloc(__bufsz);1346path = new_symlink(__len); /* Cannot fail */13471348readlinkat(AT_FDCWD, path, __stack.__buf, __len);1349_exit(EX_SOFTWARE); /* Should have aborted. */13501351monitor:1352while (waitpid(__child, &__status, 0) != __child) {1353ATF_REQUIRE_EQ(EINTR, errno);1354}13551356if (!WIFSIGNALED(__status)) {1357switch (WEXITSTATUS(__status)) {1358case EX_SOFTWARE:1359atf_tc_fail("FORTIFY_SOURCE failed to abort");1360break;1361case EX_OSERR:1362atf_tc_fail("setrlimit(2) failed");1363break;1364default:1365atf_tc_fail("child exited with status %d",1366WEXITSTATUS(__status));1367}1368} else {1369ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1370}1371#undef BUF13721373}13741375ATF_TC(getdomainname_before_end);1376ATF_TC_HEAD(getdomainname_before_end, tc)1377{1378atf_tc_set_md_var(tc, "require.user", "root");1379}1380ATF_TC_BODY(getdomainname_before_end, tc)1381{1382#define BUF &__stack.__buf1383struct {1384uint8_t padding_l;1385unsigned char __buf[12];1386uint8_t padding_r;1387} __stack;1388const size_t __bufsz __unused = sizeof(__stack.__buf);1389const size_t __len = 12 - 1;1390const size_t __idx __unused = __len - 1;13911392dhost_jail();1393getdomainname(__stack.__buf, __len);1394#undef BUF13951396}13971398ATF_TC(getdomainname_end);1399ATF_TC_HEAD(getdomainname_end, tc)1400{1401atf_tc_set_md_var(tc, "require.user", "root");1402}1403ATF_TC_BODY(getdomainname_end, tc)1404{1405#define BUF &__stack.__buf1406struct {1407uint8_t padding_l;1408unsigned char __buf[12];1409uint8_t padding_r;1410} __stack;1411const size_t __bufsz __unused = sizeof(__stack.__buf);1412const size_t __len = 12;1413const size_t __idx __unused = __len - 1;14141415dhost_jail();1416getdomainname(__stack.__buf, __len);1417#undef BUF14181419}14201421ATF_TC(getdomainname_heap_before_end);1422ATF_TC_HEAD(getdomainname_heap_before_end, tc)1423{1424atf_tc_set_md_var(tc, "require.user", "root");1425}1426ATF_TC_BODY(getdomainname_heap_before_end, tc)1427{1428#define BUF __stack.__buf1429struct {1430uint8_t padding_l;1431unsigned char * __buf;1432uint8_t padding_r;1433} __stack;1434const size_t __bufsz __unused = sizeof(*__stack.__buf) * (12);1435const size_t __len = 12 - 1;1436const size_t __idx __unused = __len - 1;14371438dhost_jail();1439__stack.__buf = malloc(__bufsz);14401441getdomainname(__stack.__buf, __len);1442#undef BUF14431444}14451446ATF_TC(getdomainname_heap_end);1447ATF_TC_HEAD(getdomainname_heap_end, tc)1448{1449atf_tc_set_md_var(tc, "require.user", "root");1450}1451ATF_TC_BODY(getdomainname_heap_end, tc)1452{1453#define BUF __stack.__buf1454struct {1455uint8_t padding_l;1456unsigned char * __buf;1457uint8_t padding_r;1458} __stack;1459const size_t __bufsz __unused = sizeof(*__stack.__buf) * (12);1460const size_t __len = 12;1461const size_t __idx __unused = __len - 1;14621463dhost_jail();1464__stack.__buf = malloc(__bufsz);14651466getdomainname(__stack.__buf, __len);1467#undef BUF14681469}14701471ATF_TC(getdomainname_heap_after_end);1472ATF_TC_HEAD(getdomainname_heap_after_end, tc)1473{1474atf_tc_set_md_var(tc, "require.user", "root");1475}1476ATF_TC_BODY(getdomainname_heap_after_end, tc)1477{1478#define BUF __stack.__buf1479struct {1480uint8_t padding_l;1481unsigned char * __buf;1482uint8_t padding_r;1483} __stack;1484const size_t __bufsz __unused = sizeof(*__stack.__buf) * (12);1485const size_t __len = 12 + 1;1486const size_t __idx __unused = __len - 1;1487pid_t __child;1488int __status;14891490dhost_jail();1491__child = fork();1492ATF_REQUIRE(__child >= 0);1493if (__child > 0)1494goto monitor;14951496/* Child */1497disable_coredumps();1498__stack.__buf = malloc(__bufsz);14991500getdomainname(__stack.__buf, __len);1501_exit(EX_SOFTWARE); /* Should have aborted. */15021503monitor:1504while (waitpid(__child, &__status, 0) != __child) {1505ATF_REQUIRE_EQ(EINTR, errno);1506}15071508if (!WIFSIGNALED(__status)) {1509switch (WEXITSTATUS(__status)) {1510case EX_SOFTWARE:1511atf_tc_fail("FORTIFY_SOURCE failed to abort");1512break;1513case EX_OSERR:1514atf_tc_fail("setrlimit(2) failed");1515break;1516default:1517atf_tc_fail("child exited with status %d",1518WEXITSTATUS(__status));1519}1520} else {1521ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1522}1523#undef BUF15241525}15261527ATF_TC(getentropy_before_end);1528ATF_TC_HEAD(getentropy_before_end, tc)1529{1530}1531ATF_TC_BODY(getentropy_before_end, tc)1532{1533#define BUF &__stack.__buf1534struct {1535uint8_t padding_l;1536unsigned char __buf[42];1537uint8_t padding_r;1538} __stack;1539const size_t __bufsz __unused = sizeof(__stack.__buf);1540const size_t __len = 42 - 1;1541const size_t __idx __unused = __len - 1;15421543getentropy(__stack.__buf, __len);1544#undef BUF15451546}15471548ATF_TC(getentropy_end);1549ATF_TC_HEAD(getentropy_end, tc)1550{1551}1552ATF_TC_BODY(getentropy_end, tc)1553{1554#define BUF &__stack.__buf1555struct {1556uint8_t padding_l;1557unsigned char __buf[42];1558uint8_t padding_r;1559} __stack;1560const size_t __bufsz __unused = sizeof(__stack.__buf);1561const size_t __len = 42;1562const size_t __idx __unused = __len - 1;15631564getentropy(__stack.__buf, __len);1565#undef BUF15661567}15681569ATF_TC(getentropy_heap_before_end);1570ATF_TC_HEAD(getentropy_heap_before_end, tc)1571{1572}1573ATF_TC_BODY(getentropy_heap_before_end, tc)1574{1575#define BUF __stack.__buf1576struct {1577uint8_t padding_l;1578unsigned char * __buf;1579uint8_t padding_r;1580} __stack;1581const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1582const size_t __len = 42 - 1;1583const size_t __idx __unused = __len - 1;15841585__stack.__buf = malloc(__bufsz);15861587getentropy(__stack.__buf, __len);1588#undef BUF15891590}15911592ATF_TC(getentropy_heap_end);1593ATF_TC_HEAD(getentropy_heap_end, tc)1594{1595}1596ATF_TC_BODY(getentropy_heap_end, tc)1597{1598#define BUF __stack.__buf1599struct {1600uint8_t padding_l;1601unsigned char * __buf;1602uint8_t padding_r;1603} __stack;1604const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1605const size_t __len = 42;1606const size_t __idx __unused = __len - 1;16071608__stack.__buf = malloc(__bufsz);16091610getentropy(__stack.__buf, __len);1611#undef BUF16121613}16141615ATF_TC(getentropy_heap_after_end);1616ATF_TC_HEAD(getentropy_heap_after_end, tc)1617{1618}1619ATF_TC_BODY(getentropy_heap_after_end, tc)1620{1621#define BUF __stack.__buf1622struct {1623uint8_t padding_l;1624unsigned char * __buf;1625uint8_t padding_r;1626} __stack;1627const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);1628const size_t __len = 42 + 1;1629const size_t __idx __unused = __len - 1;1630pid_t __child;1631int __status;16321633__child = fork();1634ATF_REQUIRE(__child >= 0);1635if (__child > 0)1636goto monitor;16371638/* Child */1639disable_coredumps();1640__stack.__buf = malloc(__bufsz);16411642getentropy(__stack.__buf, __len);1643_exit(EX_SOFTWARE); /* Should have aborted. */16441645monitor:1646while (waitpid(__child, &__status, 0) != __child) {1647ATF_REQUIRE_EQ(EINTR, errno);1648}16491650if (!WIFSIGNALED(__status)) {1651switch (WEXITSTATUS(__status)) {1652case EX_SOFTWARE:1653atf_tc_fail("FORTIFY_SOURCE failed to abort");1654break;1655case EX_OSERR:1656atf_tc_fail("setrlimit(2) failed");1657break;1658default:1659atf_tc_fail("child exited with status %d",1660WEXITSTATUS(__status));1661}1662} else {1663ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1664}1665#undef BUF16661667}16681669ATF_TC(gethostname_before_end);1670ATF_TC_HEAD(gethostname_before_end, tc)1671{1672atf_tc_set_md_var(tc, "require.user", "root");1673}1674ATF_TC_BODY(gethostname_before_end, tc)1675{1676#define BUF &__stack.__buf1677struct {1678uint8_t padding_l;1679unsigned char __buf[17];1680uint8_t padding_r;1681} __stack;1682const size_t __bufsz __unused = sizeof(__stack.__buf);1683const size_t __len = 17 - 1;1684const size_t __idx __unused = __len - 1;16851686dhost_jail();1687gethostname(__stack.__buf, __len);1688#undef BUF16891690}16911692ATF_TC(gethostname_end);1693ATF_TC_HEAD(gethostname_end, tc)1694{1695atf_tc_set_md_var(tc, "require.user", "root");1696}1697ATF_TC_BODY(gethostname_end, tc)1698{1699#define BUF &__stack.__buf1700struct {1701uint8_t padding_l;1702unsigned char __buf[17];1703uint8_t padding_r;1704} __stack;1705const size_t __bufsz __unused = sizeof(__stack.__buf);1706const size_t __len = 17;1707const size_t __idx __unused = __len - 1;17081709dhost_jail();1710gethostname(__stack.__buf, __len);1711#undef BUF17121713}17141715ATF_TC(gethostname_heap_before_end);1716ATF_TC_HEAD(gethostname_heap_before_end, tc)1717{1718atf_tc_set_md_var(tc, "require.user", "root");1719}1720ATF_TC_BODY(gethostname_heap_before_end, tc)1721{1722#define BUF __stack.__buf1723struct {1724uint8_t padding_l;1725unsigned char * __buf;1726uint8_t padding_r;1727} __stack;1728const size_t __bufsz __unused = sizeof(*__stack.__buf) * (17);1729const size_t __len = 17 - 1;1730const size_t __idx __unused = __len - 1;17311732dhost_jail();1733__stack.__buf = malloc(__bufsz);17341735gethostname(__stack.__buf, __len);1736#undef BUF17371738}17391740ATF_TC(gethostname_heap_end);1741ATF_TC_HEAD(gethostname_heap_end, tc)1742{1743atf_tc_set_md_var(tc, "require.user", "root");1744}1745ATF_TC_BODY(gethostname_heap_end, tc)1746{1747#define BUF __stack.__buf1748struct {1749uint8_t padding_l;1750unsigned char * __buf;1751uint8_t padding_r;1752} __stack;1753const size_t __bufsz __unused = sizeof(*__stack.__buf) * (17);1754const size_t __len = 17;1755const size_t __idx __unused = __len - 1;17561757dhost_jail();1758__stack.__buf = malloc(__bufsz);17591760gethostname(__stack.__buf, __len);1761#undef BUF17621763}17641765ATF_TC(gethostname_heap_after_end);1766ATF_TC_HEAD(gethostname_heap_after_end, tc)1767{1768atf_tc_set_md_var(tc, "require.user", "root");1769}1770ATF_TC_BODY(gethostname_heap_after_end, tc)1771{1772#define BUF __stack.__buf1773struct {1774uint8_t padding_l;1775unsigned char * __buf;1776uint8_t padding_r;1777} __stack;1778const size_t __bufsz __unused = sizeof(*__stack.__buf) * (17);1779const size_t __len = 17 + 1;1780const size_t __idx __unused = __len - 1;1781pid_t __child;1782int __status;17831784dhost_jail();1785__child = fork();1786ATF_REQUIRE(__child >= 0);1787if (__child > 0)1788goto monitor;17891790/* Child */1791disable_coredumps();1792__stack.__buf = malloc(__bufsz);17931794gethostname(__stack.__buf, __len);1795_exit(EX_SOFTWARE); /* Should have aborted. */17961797monitor:1798while (waitpid(__child, &__status, 0) != __child) {1799ATF_REQUIRE_EQ(EINTR, errno);1800}18011802if (!WIFSIGNALED(__status)) {1803switch (WEXITSTATUS(__status)) {1804case EX_SOFTWARE:1805atf_tc_fail("FORTIFY_SOURCE failed to abort");1806break;1807case EX_OSERR:1808atf_tc_fail("setrlimit(2) failed");1809break;1810default:1811atf_tc_fail("child exited with status %d",1812WEXITSTATUS(__status));1813}1814} else {1815ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1816}1817#undef BUF18181819}18201821ATF_TC(getlogin_r_before_end);1822ATF_TC_HEAD(getlogin_r_before_end, tc)1823{1824}1825ATF_TC_BODY(getlogin_r_before_end, tc)1826{1827#define BUF &__stack.__buf1828struct {1829uint8_t padding_l;1830unsigned char __buf[MAXLOGNAME + 1];1831uint8_t padding_r;1832} __stack;1833const size_t __bufsz __unused = sizeof(__stack.__buf);1834const size_t __len = MAXLOGNAME + 1 - 1;1835const size_t __idx __unused = __len - 1;18361837getlogin_r(__stack.__buf, __len);1838#undef BUF18391840}18411842ATF_TC(getlogin_r_end);1843ATF_TC_HEAD(getlogin_r_end, tc)1844{1845}1846ATF_TC_BODY(getlogin_r_end, tc)1847{1848#define BUF &__stack.__buf1849struct {1850uint8_t padding_l;1851unsigned char __buf[MAXLOGNAME + 1];1852uint8_t padding_r;1853} __stack;1854const size_t __bufsz __unused = sizeof(__stack.__buf);1855const size_t __len = MAXLOGNAME + 1;1856const size_t __idx __unused = __len - 1;18571858getlogin_r(__stack.__buf, __len);1859#undef BUF18601861}18621863ATF_TC(getlogin_r_heap_before_end);1864ATF_TC_HEAD(getlogin_r_heap_before_end, tc)1865{1866}1867ATF_TC_BODY(getlogin_r_heap_before_end, tc)1868{1869#define BUF __stack.__buf1870struct {1871uint8_t padding_l;1872unsigned char * __buf;1873uint8_t padding_r;1874} __stack;1875const size_t __bufsz __unused = sizeof(*__stack.__buf) * (MAXLOGNAME + 1);1876const size_t __len = MAXLOGNAME + 1 - 1;1877const size_t __idx __unused = __len - 1;18781879__stack.__buf = malloc(__bufsz);18801881getlogin_r(__stack.__buf, __len);1882#undef BUF18831884}18851886ATF_TC(getlogin_r_heap_end);1887ATF_TC_HEAD(getlogin_r_heap_end, tc)1888{1889}1890ATF_TC_BODY(getlogin_r_heap_end, tc)1891{1892#define BUF __stack.__buf1893struct {1894uint8_t padding_l;1895unsigned char * __buf;1896uint8_t padding_r;1897} __stack;1898const size_t __bufsz __unused = sizeof(*__stack.__buf) * (MAXLOGNAME + 1);1899const size_t __len = MAXLOGNAME + 1;1900const size_t __idx __unused = __len - 1;19011902__stack.__buf = malloc(__bufsz);19031904getlogin_r(__stack.__buf, __len);1905#undef BUF19061907}19081909ATF_TC(getlogin_r_heap_after_end);1910ATF_TC_HEAD(getlogin_r_heap_after_end, tc)1911{1912}1913ATF_TC_BODY(getlogin_r_heap_after_end, tc)1914{1915#define BUF __stack.__buf1916struct {1917uint8_t padding_l;1918unsigned char * __buf;1919uint8_t padding_r;1920} __stack;1921const size_t __bufsz __unused = sizeof(*__stack.__buf) * (MAXLOGNAME + 1);1922const size_t __len = MAXLOGNAME + 1 + 1;1923const size_t __idx __unused = __len - 1;1924pid_t __child;1925int __status;19261927__child = fork();1928ATF_REQUIRE(__child >= 0);1929if (__child > 0)1930goto monitor;19311932/* Child */1933disable_coredumps();1934__stack.__buf = malloc(__bufsz);19351936getlogin_r(__stack.__buf, __len);1937_exit(EX_SOFTWARE); /* Should have aborted. */19381939monitor:1940while (waitpid(__child, &__status, 0) != __child) {1941ATF_REQUIRE_EQ(EINTR, errno);1942}19431944if (!WIFSIGNALED(__status)) {1945switch (WEXITSTATUS(__status)) {1946case EX_SOFTWARE:1947atf_tc_fail("FORTIFY_SOURCE failed to abort");1948break;1949case EX_OSERR:1950atf_tc_fail("setrlimit(2) failed");1951break;1952default:1953atf_tc_fail("child exited with status %d",1954WEXITSTATUS(__status));1955}1956} else {1957ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));1958}1959#undef BUF19601961}19621963ATF_TC(ttyname_r_before_end);1964ATF_TC_HEAD(ttyname_r_before_end, tc)1965{1966}1967ATF_TC_BODY(ttyname_r_before_end, tc)1968{1969#define BUF &__stack.__buf1970struct {1971uint8_t padding_l;1972unsigned char __buf[42];1973uint8_t padding_r;1974} __stack;1975const size_t __bufsz __unused = sizeof(__stack.__buf);1976const size_t __len = 42 - 1;1977const size_t __idx __unused = __len - 1;1978int fd;19791980fd = STDIN_FILENO;1981if (!isatty(fd))1982atf_tc_skip("stdin is not an fd");19831984ttyname_r(fd, __stack.__buf, __len);1985#undef BUF19861987}19881989ATF_TC(ttyname_r_end);1990ATF_TC_HEAD(ttyname_r_end, tc)1991{1992}1993ATF_TC_BODY(ttyname_r_end, tc)1994{1995#define BUF &__stack.__buf1996struct {1997uint8_t padding_l;1998unsigned char __buf[42];1999uint8_t padding_r;2000} __stack;2001const size_t __bufsz __unused = sizeof(__stack.__buf);2002const size_t __len = 42;2003const size_t __idx __unused = __len - 1;2004int fd;20052006fd = STDIN_FILENO;2007if (!isatty(fd))2008atf_tc_skip("stdin is not an fd");20092010ttyname_r(fd, __stack.__buf, __len);2011#undef BUF20122013}20142015ATF_TC(ttyname_r_heap_before_end);2016ATF_TC_HEAD(ttyname_r_heap_before_end, tc)2017{2018}2019ATF_TC_BODY(ttyname_r_heap_before_end, tc)2020{2021#define BUF __stack.__buf2022struct {2023uint8_t padding_l;2024unsigned char * __buf;2025uint8_t padding_r;2026} __stack;2027const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);2028const size_t __len = 42 - 1;2029const size_t __idx __unused = __len - 1;2030int fd;20312032fd = STDIN_FILENO;2033if (!isatty(fd))2034atf_tc_skip("stdin is not an fd");20352036__stack.__buf = malloc(__bufsz);20372038ttyname_r(fd, __stack.__buf, __len);2039#undef BUF20402041}20422043ATF_TC(ttyname_r_heap_end);2044ATF_TC_HEAD(ttyname_r_heap_end, tc)2045{2046}2047ATF_TC_BODY(ttyname_r_heap_end, tc)2048{2049#define BUF __stack.__buf2050struct {2051uint8_t padding_l;2052unsigned char * __buf;2053uint8_t padding_r;2054} __stack;2055const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);2056const size_t __len = 42;2057const size_t __idx __unused = __len - 1;2058int fd;20592060fd = STDIN_FILENO;2061if (!isatty(fd))2062atf_tc_skip("stdin is not an fd");20632064__stack.__buf = malloc(__bufsz);20652066ttyname_r(fd, __stack.__buf, __len);2067#undef BUF20682069}20702071ATF_TC(ttyname_r_heap_after_end);2072ATF_TC_HEAD(ttyname_r_heap_after_end, tc)2073{2074}2075ATF_TC_BODY(ttyname_r_heap_after_end, tc)2076{2077#define BUF __stack.__buf2078struct {2079uint8_t padding_l;2080unsigned char * __buf;2081uint8_t padding_r;2082} __stack;2083const size_t __bufsz __unused = sizeof(*__stack.__buf) * (42);2084const size_t __len = 42 + 1;2085const size_t __idx __unused = __len - 1;2086pid_t __child;2087int __status;2088int fd;20892090fd = STDIN_FILENO;2091if (!isatty(fd))2092atf_tc_skip("stdin is not an fd");20932094__child = fork();2095ATF_REQUIRE(__child >= 0);2096if (__child > 0)2097goto monitor;20982099/* Child */2100disable_coredumps();2101__stack.__buf = malloc(__bufsz);21022103ttyname_r(fd, __stack.__buf, __len);2104_exit(EX_SOFTWARE); /* Should have aborted. */21052106monitor:2107while (waitpid(__child, &__status, 0) != __child) {2108ATF_REQUIRE_EQ(EINTR, errno);2109}21102111if (!WIFSIGNALED(__status)) {2112switch (WEXITSTATUS(__status)) {2113case EX_SOFTWARE:2114atf_tc_fail("FORTIFY_SOURCE failed to abort");2115break;2116case EX_OSERR:2117atf_tc_fail("setrlimit(2) failed");2118break;2119default:2120atf_tc_fail("child exited with status %d",2121WEXITSTATUS(__status));2122}2123} else {2124ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));2125}2126#undef BUF21272128}21292130ATF_TP_ADD_TCS(tp)2131{2132ATF_TP_ADD_TC(tp, getcwd_before_end);2133ATF_TP_ADD_TC(tp, getcwd_end);2134ATF_TP_ADD_TC(tp, getcwd_heap_before_end);2135ATF_TP_ADD_TC(tp, getcwd_heap_end);2136ATF_TP_ADD_TC(tp, getcwd_heap_after_end);2137ATF_TP_ADD_TC(tp, getgrouplist_before_end);2138ATF_TP_ADD_TC(tp, getgrouplist_end);2139ATF_TP_ADD_TC(tp, getgrouplist_heap_before_end);2140ATF_TP_ADD_TC(tp, getgrouplist_heap_end);2141ATF_TP_ADD_TC(tp, getgrouplist_heap_after_end);2142ATF_TP_ADD_TC(tp, getgroups_before_end);2143ATF_TP_ADD_TC(tp, getgroups_end);2144ATF_TP_ADD_TC(tp, getgroups_heap_before_end);2145ATF_TP_ADD_TC(tp, getgroups_heap_end);2146ATF_TP_ADD_TC(tp, getgroups_heap_after_end);2147ATF_TP_ADD_TC(tp, getloginclass_before_end);2148ATF_TP_ADD_TC(tp, getloginclass_end);2149ATF_TP_ADD_TC(tp, getloginclass_heap_before_end);2150ATF_TP_ADD_TC(tp, getloginclass_heap_end);2151ATF_TP_ADD_TC(tp, getloginclass_heap_after_end);2152ATF_TP_ADD_TC(tp, pread_before_end);2153ATF_TP_ADD_TC(tp, pread_end);2154ATF_TP_ADD_TC(tp, pread_heap_before_end);2155ATF_TP_ADD_TC(tp, pread_heap_end);2156ATF_TP_ADD_TC(tp, pread_heap_after_end);2157ATF_TP_ADD_TC(tp, read_before_end);2158ATF_TP_ADD_TC(tp, read_end);2159ATF_TP_ADD_TC(tp, read_heap_before_end);2160ATF_TP_ADD_TC(tp, read_heap_end);2161ATF_TP_ADD_TC(tp, read_heap_after_end);2162ATF_TP_ADD_TC(tp, readlink_before_end);2163ATF_TP_ADD_TC(tp, readlink_end);2164ATF_TP_ADD_TC(tp, readlink_heap_before_end);2165ATF_TP_ADD_TC(tp, readlink_heap_end);2166ATF_TP_ADD_TC(tp, readlink_heap_after_end);2167ATF_TP_ADD_TC(tp, readlinkat_before_end);2168ATF_TP_ADD_TC(tp, readlinkat_end);2169ATF_TP_ADD_TC(tp, readlinkat_heap_before_end);2170ATF_TP_ADD_TC(tp, readlinkat_heap_end);2171ATF_TP_ADD_TC(tp, readlinkat_heap_after_end);2172ATF_TP_ADD_TC(tp, getdomainname_before_end);2173ATF_TP_ADD_TC(tp, getdomainname_end);2174ATF_TP_ADD_TC(tp, getdomainname_heap_before_end);2175ATF_TP_ADD_TC(tp, getdomainname_heap_end);2176ATF_TP_ADD_TC(tp, getdomainname_heap_after_end);2177ATF_TP_ADD_TC(tp, getentropy_before_end);2178ATF_TP_ADD_TC(tp, getentropy_end);2179ATF_TP_ADD_TC(tp, getentropy_heap_before_end);2180ATF_TP_ADD_TC(tp, getentropy_heap_end);2181ATF_TP_ADD_TC(tp, getentropy_heap_after_end);2182ATF_TP_ADD_TC(tp, gethostname_before_end);2183ATF_TP_ADD_TC(tp, gethostname_end);2184ATF_TP_ADD_TC(tp, gethostname_heap_before_end);2185ATF_TP_ADD_TC(tp, gethostname_heap_end);2186ATF_TP_ADD_TC(tp, gethostname_heap_after_end);2187ATF_TP_ADD_TC(tp, getlogin_r_before_end);2188ATF_TP_ADD_TC(tp, getlogin_r_end);2189ATF_TP_ADD_TC(tp, getlogin_r_heap_before_end);2190ATF_TP_ADD_TC(tp, getlogin_r_heap_end);2191ATF_TP_ADD_TC(tp, getlogin_r_heap_after_end);2192ATF_TP_ADD_TC(tp, ttyname_r_before_end);2193ATF_TP_ADD_TC(tp, ttyname_r_end);2194ATF_TP_ADD_TC(tp, ttyname_r_heap_before_end);2195ATF_TP_ADD_TC(tp, ttyname_r_heap_end);2196ATF_TP_ADD_TC(tp, ttyname_r_heap_after_end);2197return (atf_no_error());2198}219922002201