// WARNING in ovs_fragment: an unprivileged user executes an MPLS frame larger // than the MRU it supplies, with output to an internal port of its own datapath. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static int fd; static void put(char *buf, size_t *len, uint16_t type, const void *data, size_t dlen) { struct nlattr *a = (struct nlattr *)(buf + *len); a->nla_type = type; a->nla_len = NLA_HDRLEN + dlen; memcpy((char *)a + NLA_HDRLEN, data, dlen); *len += NLA_ALIGN(a->nla_len); } /* Send one generic netlink request; return the reply in rb and the error. */ static int req(uint16_t fam, uint8_t cmd, uint32_t dp, const char *attrs, size_t alen, char *rb) { char msg[8192] = {0}; struct nlmsghdr *nh = (struct nlmsghdr *)msg; struct genlmsghdr *gh = (struct genlmsghdr *)NLMSG_DATA(nh); size_t off = NLMSG_HDRLEN + GENL_HDRLEN; gh->cmd = cmd; gh->version = 1; if (fam != GENL_ID_CTRL) { ((struct ovs_header *)(msg + off))->dp_ifindex = dp; off += sizeof(struct ovs_header); } memcpy(msg + off, attrs, alen); nh->nlmsg_len = off + alen; nh->nlmsg_type = fam; nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; if (send(fd, msg, nh->nlmsg_len, 0) < 0) return -1; /* A reply, if any, comes before the ACK that NLM_F_ACK asks for. */ for (;;) { if (recv(fd, msg, sizeof(msg), 0) < 0) return -1; if (nh->nlmsg_type == NLMSG_ERROR) return ((struct nlmsgerr *)NLMSG_DATA(nh))->error; memcpy(rb, msg, sizeof(msg)); } } static uint16_t family(const char *name) { char a[64], rb[8192]; size_t al = 0; struct nlattr *na; put(a, &al, CTRL_ATTR_FAMILY_NAME, name, strlen(name) + 1); if (req(GENL_ID_CTRL, CTRL_CMD_GETFAMILY, 0, a, al, rb)) exit(1); for (na = (struct nlattr *)(rb + NLMSG_HDRLEN + GENL_HDRLEN); na->nla_type != CTRL_ATTR_FAMILY_ID; na = (struct nlattr *)((char *)na + NLA_ALIGN(na->nla_len))) ; return *(uint16_t *)((char *)na + NLA_HDRLEN); } static void write_file(const char *path, const char *s) { int f = open(path, O_WRONLY); if (f < 0 || write(f, s, strlen(s)) < 0) exit(1); close(f); } int main(void) { char a[4096], key[256], act[64], rb[8192]; uint8_t frame[1434] = {0}; size_t al = 0, kl = 0, cl = 0; uint32_t pid = 0, type = OVS_VPORT_TYPE_INTERNAL, port = 1, dp; uint16_t fam_dp, fam_vport, fam_packet, et = htons(ETH_P_MPLS_UC), mru = 500; struct ovs_key_ethernet eth = {{0}}; struct ifreq ifr = {0}; char map[32]; snprintf(map, sizeof(map), "0 %d 1", getuid()); if (unshare(CLONE_NEWUSER | CLONE_NEWNET)) return 1; write_file("/proc/self/setgroups", "deny"); write_file("/proc/self/uid_map", map); snprintf(map, sizeof(map), "0 %d 1", getgid()); write_file("/proc/self/gid_map", map); fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); fam_dp = family(OVS_DATAPATH_FAMILY); fam_vport = family(OVS_VPORT_FAMILY); fam_packet = family(OVS_PACKET_FAMILY); put(a, &al, OVS_DP_ATTR_NAME, "dp0", 4); put(a, &al, OVS_DP_ATTR_UPCALL_PID, &pid, 4); printf("datapath: %d\n", req(fam_dp, OVS_DP_CMD_NEW, 0, a, al, rb)); dp = if_nametoindex("dp0"); al = 0; put(a, &al, OVS_VPORT_ATTR_NAME, "ovs0", 5); put(a, &al, OVS_VPORT_ATTR_TYPE, &type, 4); put(a, &al, OVS_VPORT_ATTR_PORT_NO, &port, 4); put(a, &al, OVS_VPORT_ATTR_UPCALL_PID, &pid, 4); printf("vport: %d\n", req(fam_vport, OVS_VPORT_CMD_NEW, dp, a, al, rb)); strcpy(ifr.ifr_name, "ovs0"); ifr.ifr_flags = IFF_UP; ioctl(socket(AF_INET, SOCK_DGRAM, 0), SIOCSIFFLAGS, &ifr); /* An Ethernet frame with an MPLS ethertype, longer than the MRU below. */ frame[0] = 0x02; frame[11] = 0x02; memcpy(frame + 12, &et, 2); memset(frame + 14, 0x41, sizeof(frame) - 14); memcpy(eth.eth_dst, frame, 6); memcpy(eth.eth_src, frame + 6, 6); put(key, &kl, OVS_KEY_ATTR_ETHERNET, ð, sizeof(eth)); put(key, &kl, OVS_KEY_ATTR_ETHERTYPE, &et, 2); put(key, &kl, OVS_KEY_ATTR_IN_PORT, &pid, 4); put(act, &cl, OVS_ACTION_ATTR_OUTPUT, &port, 4); al = 0; put(a, &al, OVS_PACKET_ATTR_PACKET, frame, sizeof(frame)); put(a, &al, OVS_PACKET_ATTR_KEY, key, kl); put(a, &al, OVS_PACKET_ATTR_ACTIONS, act, cl); put(a, &al, OVS_PACKET_ATTR_MRU, &mru, 2); printf("execute: %d\n", req(fam_packet, OVS_PACKET_CMD_EXECUTE, dp, a, al, rb)); return 0; }