| @@ -17,11 +17,14 @@ · 16 unmodified lines |
| 17 | #include <net/ipv6.h> | 17 | #include <net/ipv6.h> |
| 18 | #include <linux/kernel.h> | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/user_namespace.h> | 19 | #include <linux/user_namespace.h> |
| | 20 | #include <net/genetlink.h> |
| | 21 | #include <uapi/linux/sunrpc_netlink.h> |
| 20 | #include <trace/events/sunrpc.h> | 22 | #include <trace/events/sunrpc.h> |
| 21 | | 23 | |
| 22 | #define RPCDBG_FACILITY RPCDBG_AUTH | 24 | #define RPCDBG_FACILITY RPCDBG_AUTH |
| 23 | | 25 | |
| 24 | #include "netns.h" | 26 | #include "netns.h" |
| | 27 | #include "netlink.h" |
| 25 | | 28 | |
| 26 | /* | 29 | /* |
| 27 | * AUTHUNIX and AUTHNULL credentials are both handled here. | 30 | * AUTHUNIX and AUTHNULL credentials are both handled here. |
| @@ -1017,12 +1020,255 @@ struct auth_ops svcauth_unix = { · 989 unmodified lines |
| 1017 | .set_client = svcauth_unix_set_client, | 1020 | .set_client = svcauth_unix_set_client, |
| 1018 | }; | 1021 | }; |
| 1019 | | 1022 | |
| | 1023 | static int ip_map_notify(struct cache_detail *cd, struct cache_head *h) |
| | 1024 | { |
| | 1025 | return sunrpc_cache_notify(cd, h, SUNRPC_CACHE_TYPE_IP_MAP); |
| | 1026 | } |
| | 1027 | |
| | 1028 | /** |
| | 1029 | * sunrpc_nl_ip_map_get_reqs_dumpit - dump pending ip_map requests |
| | 1030 | * @skb: reply buffer |
| | 1031 | * @cb: netlink metadata and command arguments |
| | 1032 | * |
| | 1033 | * Walk the ip_map cache's pending request list and create a netlink |
| | 1034 | * message with a nested entry for each cache_request, containing the |
| | 1035 | * seqno, class and addr. |
| | 1036 | * |
| | 1037 | * Uses cb->args[0] as a seqno cursor for dump continuation across |
| | 1038 | * multiple netlink messages. |
| | 1039 | * |
| | 1040 | * Returns the size of the reply or a negative errno. |
| | 1041 | */ |
| | 1042 | int sunrpc_nl_ip_map_get_reqs_dumpit(struct sk_buff *skb, |
| | 1043 | struct netlink_callback *cb) |
| | 1044 | { |
| | 1045 | struct sunrpc_net *sn; |
| | 1046 | struct cache_detail *cd; |
| | 1047 | struct cache_head **items; |
| | 1048 | u64 *seqnos; |
| | 1049 | int cnt, i, emitted; |
| | 1050 | void *hdr; |
| | 1051 | int ret; |
| | 1052 | |
| | 1053 | sn = net_generic(sock_net(skb->sk), sunrpc_net_id); |
| | 1054 | |
| | 1055 | cd = sn->ip_map_cache; |
| | 1056 | if (!cd) |
| | 1057 | return -ENODEV; |
| | 1058 | |
| | 1059 | cnt = sunrpc_cache_requests_count(cd); |
| | 1060 | if (!cnt) |
| | 1061 | return 0; |
| | 1062 | |
| | 1063 | items = kcalloc(cnt, sizeof(*items), GFP_KERNEL); |
| | 1064 | seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL); |
| | 1065 | if (!items || !seqnos) { |
| | 1066 | ret = -ENOMEM; |
| | 1067 | goto out_alloc; |
| | 1068 | } |
| | 1069 | |
| | 1070 | cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt, |
| | 1071 | cb->args[0]); |
| | 1072 | if (!cnt) { |
| | 1073 | ret = 0; |
| | 1074 | goto out_alloc; |
| | 1075 | } |
| | 1076 | |
| | 1077 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, |
| | 1078 | cb->nlh->nlmsg_seq, &sunrpc_nl_family, |
| | 1079 | NLM_F_MULTI, SUNRPC_CMD_IP_MAP_GET_REQS); |
| | 1080 | if (!hdr) { |
| | 1081 | ret = -ENOBUFS; |
| | 1082 | goto out_put; |
| | 1083 | } |
| | 1084 | |
| | 1085 | emitted = 0; |
| | 1086 | for (i = 0; i < cnt; i++) { |
| | 1087 | struct ip_map *im; |
| | 1088 | struct nlattr *nest; |
| | 1089 | char text_addr[40]; |
| | 1090 | |
| | 1091 | im = container_of(items[i], struct ip_map, h); |
| | 1092 | |
| | 1093 | if (ipv6_addr_v4mapped(&im->m_addr)) |
| | 1094 | snprintf(text_addr, 20, "%pI4", |
| | 1095 | &im->m_addr.s6_addr32[3]); |
| | 1096 | else |
| | 1097 | snprintf(text_addr, 40, "%pI6", &im->m_addr); |
| | 1098 | |
| | 1099 | nest = nla_nest_start(skb, SUNRPC_A_IP_MAP_REQS_REQUESTS); |
| | 1100 | if (!nest) |
| | 1101 | break; |
| | 1102 | |
| | 1103 | if (nla_put_u64_64bit(skb, SUNRPC_A_IP_MAP_SEQNO, |
| | 1104 | seqnos[i], 0) || |
| | 1105 | nla_put_string(skb, SUNRPC_A_IP_MAP_CLASS, |
| | 1106 | im->m_class) || |
| | 1107 | nla_put_string(skb, SUNRPC_A_IP_MAP_ADDR, text_addr)) { |
| | 1108 | nla_nest_cancel(skb, nest); |
| | 1109 | break; |
| | 1110 | } |
| | 1111 | |
| | 1112 | nla_nest_end(skb, nest); |
| | 1113 | cb->args[0] = seqnos[i]; |
| | 1114 | emitted++; |
| | 1115 | } |
| | 1116 | |
| | 1117 | if (!emitted) { |
| | 1118 | genlmsg_cancel(skb, hdr); |
| | 1119 | ret = -EMSGSIZE; |
| | 1120 | goto out_put; |
| | 1121 | } |
| | 1122 | |
| | 1123 | genlmsg_end(skb, hdr); |
| | 1124 | ret = skb->len; |
| | 1125 | out_put: |
| | 1126 | for (i = 0; i < cnt; i++) |
| | 1127 | cache_put(items[i], cd); |
| | 1128 | out_alloc: |
| | 1129 | kfree(seqnos); |
| | 1130 | kfree(items); |
| | 1131 | return ret; |
| | 1132 | } |
| | 1133 | |
| | 1134 | /** |
| | 1135 | * sunrpc_nl_parse_one_ip_map - parse one ip_map entry from netlink |
| | 1136 | * @cd: cache_detail for the ip_map cache |
| | 1137 | * @attr: nested attribute containing ip_map fields |
| | 1138 | * |
| | 1139 | * Parses one ip_map entry from a netlink message and updates the |
| | 1140 | * cache. Mirrors the logic in ip_map_parse(). |
| | 1141 | * |
| | 1142 | * Returns 0 on success or a negative errno. |
| | 1143 | */ |
| | 1144 | static int sunrpc_nl_parse_one_ip_map(struct cache_detail *cd, |
| | 1145 | struct nlattr *attr) |
| | 1146 | { |
| | 1147 | struct nlattr *tb[SUNRPC_A_IP_MAP_EXPIRY + 1]; |
| | 1148 | union { |
| | 1149 | struct sockaddr sa; |
| | 1150 | struct sockaddr_in s4; |
| | 1151 | struct sockaddr_in6 s6; |
| | 1152 | } address; |
| | 1153 | struct sockaddr_in6 sin6; |
| | 1154 | struct ip_map *ipmp; |
| | 1155 | struct auth_domain *dom = NULL; |
| | 1156 | struct unix_domain *udom = NULL; |
| | 1157 | struct timespec64 boot; |
| | 1158 | time64_t expiry; |
| | 1159 | char class[8]; |
| | 1160 | int err; |
| | 1161 | int len; |
| | 1162 | |
| | 1163 | err = nla_parse_nested(tb, SUNRPC_A_IP_MAP_EXPIRY, attr, |
| | 1164 | sunrpc_ip_map_nl_policy, NULL); |
| | 1165 | if (err) |
| | 1166 | return err; |
| | 1167 | |
| | 1168 | /* class (required) */ |
| | 1169 | if (!tb[SUNRPC_A_IP_MAP_CLASS]) |
| | 1170 | return -EINVAL; |
| | 1171 | len = nla_len(tb[SUNRPC_A_IP_MAP_CLASS]); |
| | 1172 | if (len <= 0 || len > sizeof(class)) |
| | 1173 | return -EINVAL; |
| | 1174 | nla_strscpy(class, tb[SUNRPC_A_IP_MAP_CLASS], sizeof(class)); |
| | 1175 | |
| | 1176 | /* addr (required) */ |
| | 1177 | if (!tb[SUNRPC_A_IP_MAP_ADDR]) |
| | 1178 | return -EINVAL; |
| | 1179 | if (rpc_pton(cd->net, nla_data(tb[SUNRPC_A_IP_MAP_ADDR]), |
| | 1180 | nla_len(tb[SUNRPC_A_IP_MAP_ADDR]) - 1, |
| | 1181 | &address.sa, sizeof(address)) == 0) |
| | 1182 | return -EINVAL; |
| | 1183 | |
| | 1184 | switch (address.sa.sa_family) { |
| | 1185 | case AF_INET: |
| | 1186 | sin6.sin6_family = AF_INET6; |
| | 1187 | ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr, |
| | 1188 | &sin6.sin6_addr); |
| | 1189 | break; |
| | 1190 | #if IS_ENABLED(CONFIG_IPV6) |
| | 1191 | case AF_INET6: |
| | 1192 | memcpy(&sin6, &address.s6, sizeof(sin6)); |
| | 1193 | break; |
| | 1194 | #endif |
| | 1195 | default: |
| | 1196 | return -EINVAL; |
| | 1197 | } |
| | 1198 | |
| | 1199 | /* expiry (required, wallclock seconds) */ |
| | 1200 | if (!tb[SUNRPC_A_IP_MAP_EXPIRY]) |
| | 1201 | return -EINVAL; |
| | 1202 | getboottime64(&boot); |
| | 1203 | expiry = nla_get_u64(tb[SUNRPC_A_IP_MAP_EXPIRY]) - boot.tv_sec; |
| | 1204 | |
| | 1205 | /* domain name or negative */ |
| | 1206 | if (tb[SUNRPC_A_IP_MAP_NEGATIVE]) { |
| | 1207 | udom = NULL; |
| | 1208 | } else if (tb[SUNRPC_A_IP_MAP_DOMAIN]) { |
| | 1209 | dom = unix_domain_find(nla_data(tb[SUNRPC_A_IP_MAP_DOMAIN])); |
| | 1210 | if (!dom) |
| | 1211 | return -ENOENT; |
| | 1212 | udom = container_of(dom, struct unix_domain, h); |
| | 1213 | } else { |
| | 1214 | return -EINVAL; |
| | 1215 | } |
| | 1216 | |
| | 1217 | ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr); |
| | 1218 | if (ipmp) |
| | 1219 | err = __ip_map_update(cd, ipmp, udom, expiry); |
| | 1220 | else |
| | 1221 | err = -ENOMEM; |
| | 1222 | |
| | 1223 | if (dom) |
| | 1224 | auth_domain_put(dom); |
| | 1225 | |
| | 1226 | cache_flush(); |
| | 1227 | return err; |
| | 1228 | } |
| | 1229 | |
| | 1230 | /** |
| | 1231 | * sunrpc_nl_ip_map_set_reqs_doit - respond to ip_map requests |
| | 1232 | * @skb: reply buffer |
| | 1233 | * @info: netlink metadata and command arguments |
| | 1234 | * |
| | 1235 | * Parse one or more ip_map cache responses from userspace and |
| | 1236 | * update the ip_map cache accordingly. |
| | 1237 | * |
| | 1238 | * Returns 0 on success or a negative errno. |
| | 1239 | */ |
| | 1240 | int sunrpc_nl_ip_map_set_reqs_doit(struct sk_buff *skb, |
| | 1241 | struct genl_info *info) |
| | 1242 | { |
| | 1243 | struct sunrpc_net *sn; |
| | 1244 | struct cache_detail *cd; |
| | 1245 | const struct nlattr *attr; |
| | 1246 | int rem, ret = 0; |
| | 1247 | |
| | 1248 | sn = net_generic(genl_info_net(info), sunrpc_net_id); |
| | 1249 | |
| | 1250 | cd = sn->ip_map_cache; |
| | 1251 | if (!cd) |
| | 1252 | return -ENODEV; |
| | 1253 | |
| | 1254 | nlmsg_for_each_attr_type(attr, SUNRPC_A_IP_MAP_REQS_REQUESTS, |
| | 1255 | info->nlhdr, GENL_HDRLEN, rem) { |
| | 1256 | ret = sunrpc_nl_parse_one_ip_map(cd, |
| | 1257 | (struct nlattr *)attr); |
| | 1258 | if (ret) |
| | 1259 | break; |
| | 1260 | } |
| | 1261 | |
| | 1262 | return ret; |
| | 1263 | } |
| | 1264 | |
| 1020 | static const struct cache_detail ip_map_cache_template = { | 1265 | static const struct cache_detail ip_map_cache_template = { |
| 1021 | .owner = THIS_MODULE, | 1266 | .owner = THIS_MODULE, |
| 1022 | .hash_size = IP_HASHMAX, | 1267 | .hash_size = IP_HASHMAX, |
| 1023 | .name = "auth.unix.ip", | 1268 | .name = "auth.unix.ip", |
| 1024 | .cache_put = ip_map_put, | 1269 | .cache_put = ip_map_put, |
| 1025 | .cache_upcall = ip_map_upcall, | 1270 | .cache_upcall = ip_map_upcall, |
| | 1271 | .cache_notify = ip_map_notify, |
| 1026 | .cache_request = ip_map_request, | 1272 | .cache_request = ip_map_request, |
| 1027 | .cache_parse = ip_map_parse, | 1273 | .cache_parse = ip_map_parse, |
| 1028 | .cache_show = ip_map_show, | 1274 | .cache_show = ip_map_show, |