hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/dns_resolver/dns_query.c
....@@ -1,7 +1,7 @@
11 /* Upcall routine, designed to work as a key type and working through
22 * /sbin/request-key to contact userspace when handling DNS queries.
33 *
4
- * See Documentation/networking/dns_resolver.txt
4
+ * See Documentation/networking/dns_resolver.rst
55 *
66 * Copyright (c) 2007 Igor Mammedov
77 * Author(s): Igor Mammedov (niallain@gmail.com)
....@@ -40,6 +40,7 @@
4040 #include <linux/cred.h>
4141 #include <linux/dns_resolver.h>
4242 #include <linux/err.h>
43
+#include <net/net_namespace.h>
4344
4445 #include <keys/dns_resolver-type.h>
4546 #include <keys/user-type.h>
....@@ -48,12 +49,14 @@
4849
4950 /**
5051 * dns_query - Query the DNS
52
+ * @net: The network namespace to operate in.
5153 * @type: Query type (or NULL for straight host->IP lookup)
5254 * @name: Name to look up
5355 * @namelen: Length of name
5456 * @options: Request options (or NULL if no options)
5557 * @_result: Where to place the returned data (or NULL)
5658 * @_expiry: Where to store the result expiry time (or NULL)
59
+ * @invalidate: Always invalidate the key after use
5760 *
5861 * The data will be returned in the pointer at *result, if provided, and the
5962 * caller is responsible for freeing it.
....@@ -68,8 +71,10 @@
6871 *
6972 * Returns the size of the result on success, -ve error code otherwise.
7073 */
71
-int dns_query(const char *type, const char *name, size_t namelen,
72
- const char *options, char **_result, time64_t *_expiry)
74
+int dns_query(struct net *net,
75
+ const char *type, const char *name, size_t namelen,
76
+ const char *options, char **_result, time64_t *_expiry,
77
+ bool invalidate)
7378 {
7479 struct key *rkey;
7580 struct user_key_payload *upayload;
....@@ -94,8 +99,6 @@
9499 desclen += typelen + 1;
95100 }
96101
97
- if (!namelen)
98
- namelen = strnlen(name, 256);
99102 if (namelen < 3 || namelen > 255)
100103 return -EINVAL;
101104 desclen += namelen + 1;
....@@ -122,7 +125,7 @@
122125 * add_key() to preinstall malicious redirections
123126 */
124127 saved_cred = override_creds(dns_resolver_cache);
125
- rkey = request_key(&key_type_dns_resolver, desc, options);
128
+ rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
126129 revert_creds(saved_cred);
127130 kfree(desc);
128131 if (IS_ERR(rkey)) {
....@@ -148,12 +151,9 @@
148151
149152 if (_result) {
150153 ret = -ENOMEM;
151
- *_result = kmalloc(len + 1, GFP_KERNEL);
154
+ *_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
152155 if (!*_result)
153156 goto put;
154
-
155
- memcpy(*_result, upayload->data, len);
156
- (*_result)[len] = '\0';
157157 }
158158
159159 if (_expiry)
....@@ -162,6 +162,8 @@
162162 ret = len;
163163 put:
164164 up_read(&rkey->sem);
165
+ if (invalidate)
166
+ key_invalidate(rkey);
165167 key_put(rkey);
166168 out:
167169 kleave(" = %d", ret);