1*5c2921b0SApple OSS Distributions #include <errno.h> 2*5c2921b0SApple OSS Distributions #include <stdio.h> 3*5c2921b0SApple OSS Distributions #include <stdlib.h> 4*5c2921b0SApple OSS Distributions #include <string.h> 5*5c2921b0SApple OSS Distributions #include <strings.h> 6*5c2921b0SApple OSS Distributions 7*5c2921b0SApple OSS Distributions #include <net/route.h> 8*5c2921b0SApple OSS Distributions #include <sys/socket.h> 9*5c2921b0SApple OSS Distributions #include <unistd.h> 10*5c2921b0SApple OSS Distributions 11*5c2921b0SApple OSS Distributions #include <darwintest.h> 12*5c2921b0SApple OSS Distributions 13*5c2921b0SApple OSS Distributions #define ROUNDUP32(n) (((n) + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1)) 14*5c2921b0SApple OSS Distributions 15*5c2921b0SApple OSS Distributions T_DECL(route_output_stack_oflow_56033075, "Stack overflow via ma_copy through route_output") 16*5c2921b0SApple OSS Distributions { 17*5c2921b0SApple OSS Distributions int s; 18*5c2921b0SApple OSS Distributions uint8_t buf[ 19*5c2921b0SApple OSS Distributions sizeof(struct rt_msghdr) + 20*5c2921b0SApple OSS Distributions ROUNDUP32(sizeof(struct sockaddr_storage) + 1) + /* RTAX_DST */ 21*5c2921b0SApple OSS Distributions ROUNDUP32(sizeof(struct sockaddr_storage) + 1) + /* RTAX_GATEWAY */ 22*5c2921b0SApple OSS Distributions ROUNDUP32(sizeof(struct sockaddr_storage) + 1) /* RTAX_NETMASK */ 23*5c2921b0SApple OSS Distributions ]; 24*5c2921b0SApple OSS Distributions struct rt_msghdr *rtm = (struct rt_msghdr *)buf; 25*5c2921b0SApple OSS Distributions struct sockaddr *sa; 26*5c2921b0SApple OSS Distributions size_t len; 27*5c2921b0SApple OSS Distributions 28*5c2921b0SApple OSS Distributions bzero(buf, sizeof(buf)); 29*5c2921b0SApple OSS Distributions rtm->rtm_type = RTM_GET; 30*5c2921b0SApple OSS Distributions rtm->rtm_version = RTM_VERSION; 31*5c2921b0SApple OSS Distributions rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 32*5c2921b0SApple OSS Distributions len = sizeof(struct rt_msghdr); 33*5c2921b0SApple OSS Distributions 34*5c2921b0SApple OSS Distributions /* RTAX_DST: */ 35*5c2921b0SApple OSS Distributions sa = (struct sockaddr *)(rtm + 1); 36*5c2921b0SApple OSS Distributions sa->sa_family = AF_INET6; 37*5c2921b0SApple OSS Distributions sa->sa_len = sizeof(struct sockaddr_storage) + 1; 38*5c2921b0SApple OSS Distributions memset(&sa->sa_data[0], 0xff, sa->sa_len); 39*5c2921b0SApple OSS Distributions len += ROUNDUP32(sa->sa_len); 40*5c2921b0SApple OSS Distributions 41*5c2921b0SApple OSS Distributions /* RTAX_GATEWAY: */ 42*5c2921b0SApple OSS Distributions sa = (struct sockaddr *)((void *)buf + len); 43*5c2921b0SApple OSS Distributions sa->sa_family = AF_INET6; 44*5c2921b0SApple OSS Distributions sa->sa_len = sizeof(struct sockaddr_storage) + 1; 45*5c2921b0SApple OSS Distributions memset(&sa->sa_data[0], 0xff, sa->sa_len); 46*5c2921b0SApple OSS Distributions len += ROUNDUP32(sa->sa_len); 47*5c2921b0SApple OSS Distributions 48*5c2921b0SApple OSS Distributions /* RTAX_NETMASK: */ 49*5c2921b0SApple OSS Distributions sa = (struct sockaddr *)((void *)buf + len); 50*5c2921b0SApple OSS Distributions sa->sa_family = AF_INET6; 51*5c2921b0SApple OSS Distributions sa->sa_len = sizeof(struct sockaddr_storage) + 1; 52*5c2921b0SApple OSS Distributions memset(&sa->sa_data[0], 0x41, sa->sa_len); 53*5c2921b0SApple OSS Distributions len += ROUNDUP32(sa->sa_len); 54*5c2921b0SApple OSS Distributions 55*5c2921b0SApple OSS Distributions T_SETUPBEGIN; 56*5c2921b0SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(PF_ROUTE, SOCK_RAW, PF_ROUTE), NULL); 57*5c2921b0SApple OSS Distributions T_SETUPEND; 58*5c2921b0SApple OSS Distributions 59*5c2921b0SApple OSS Distributions /* check we get EINVAL for > sizeof(struct sockaddr_storage): */ 60*5c2921b0SApple OSS Distributions rtm->rtm_msglen = len; 61*5c2921b0SApple OSS Distributions T_ASSERT_EQ(-1, send(s, buf, len, 0), NULL); 62*5c2921b0SApple OSS Distributions T_ASSERT_EQ(EINVAL, errno, NULL); 63*5c2921b0SApple OSS Distributions 64*5c2921b0SApple OSS Distributions /* now check the ok case: */ 65*5c2921b0SApple OSS Distributions len = sizeof(struct rt_msghdr); 66*5c2921b0SApple OSS Distributions 67*5c2921b0SApple OSS Distributions /* RTAX_DST: */ 68*5c2921b0SApple OSS Distributions sa = (struct sockaddr *)(rtm + 1); 69*5c2921b0SApple OSS Distributions sa->sa_family = AF_INET6; 70*5c2921b0SApple OSS Distributions sa->sa_len = sizeof(struct sockaddr_storage); 71*5c2921b0SApple OSS Distributions len += ROUNDUP32(sa->sa_len); 72*5c2921b0SApple OSS Distributions 73*5c2921b0SApple OSS Distributions /* RTAX_GATEWAY: */ 74*5c2921b0SApple OSS Distributions sa = (struct sockaddr *)((void *)buf + len); 75*5c2921b0SApple OSS Distributions sa->sa_family = AF_INET6; 76*5c2921b0SApple OSS Distributions sa->sa_len = sizeof(struct sockaddr_storage); 77*5c2921b0SApple OSS Distributions len += ROUNDUP32(sa->sa_len); 78*5c2921b0SApple OSS Distributions 79*5c2921b0SApple OSS Distributions /* RTAX_NETMASK: */ 80*5c2921b0SApple OSS Distributions sa = (struct sockaddr *)((void *)buf + len); 81*5c2921b0SApple OSS Distributions sa->sa_family = AF_INET6; 82*5c2921b0SApple OSS Distributions sa->sa_len = sizeof(struct sockaddr_storage); 83*5c2921b0SApple OSS Distributions len += ROUNDUP32(sa->sa_len); 84*5c2921b0SApple OSS Distributions 85*5c2921b0SApple OSS Distributions rtm->rtm_msglen = len; 86*5c2921b0SApple OSS Distributions T_ASSERT_EQ(-1, send(s, buf, len, 0), NULL); 87*5c2921b0SApple OSS Distributions T_ASSERT_EQ(ESRCH, errno, NULL); 88*5c2921b0SApple OSS Distributions } 89