This source file includes following definitions.
- dns_query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/cred.h>
41 #include <linux/dns_resolver.h>
42 #include <linux/err.h>
43 #include <net/net_namespace.h>
44
45 #include <keys/dns_resolver-type.h>
46 #include <keys/user-type.h>
47
48 #include "internal.h"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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)
78 {
79 struct key *rkey;
80 struct user_key_payload *upayload;
81 const struct cred *saved_cred;
82 size_t typelen, desclen;
83 char *desc, *cp;
84 int ret, len;
85
86 kenter("%s,%*.*s,%zu,%s",
87 type, (int)namelen, (int)namelen, name, namelen, options);
88
89 if (!name || namelen == 0)
90 return -EINVAL;
91
92
93 typelen = 0;
94 desclen = 0;
95 if (type) {
96 typelen = strlen(type);
97 if (typelen < 1)
98 return -EINVAL;
99 desclen += typelen + 1;
100 }
101
102 if (namelen < 3 || namelen > 255)
103 return -EINVAL;
104 desclen += namelen + 1;
105
106 desc = kmalloc(desclen, GFP_KERNEL);
107 if (!desc)
108 return -ENOMEM;
109
110 cp = desc;
111 if (type) {
112 memcpy(cp, type, typelen);
113 cp += typelen;
114 *cp++ = ':';
115 }
116 memcpy(cp, name, namelen);
117 cp += namelen;
118 *cp = '\0';
119
120 if (!options)
121 options = "";
122 kdebug("call request_key(,%s,%s)", desc, options);
123
124
125
126
127 saved_cred = override_creds(dns_resolver_cache);
128 rkey = request_key_net(&key_type_dns_resolver, desc, net, options);
129 revert_creds(saved_cred);
130 kfree(desc);
131 if (IS_ERR(rkey)) {
132 ret = PTR_ERR(rkey);
133 goto out;
134 }
135
136 down_read(&rkey->sem);
137 set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
138 rkey->perm |= KEY_USR_VIEW;
139
140 ret = key_validate(rkey);
141 if (ret < 0)
142 goto put;
143
144
145 ret = PTR_ERR(rkey->payload.data[dns_key_error]);
146 if (ret)
147 goto put;
148
149 upayload = user_key_payload_locked(rkey);
150 len = upayload->datalen;
151
152 if (_result) {
153 ret = -ENOMEM;
154 *_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
155 if (!*_result)
156 goto put;
157 }
158
159 if (_expiry)
160 *_expiry = rkey->expiry;
161
162 ret = len;
163 put:
164 up_read(&rkey->sem);
165 if (invalidate)
166 key_invalidate(rkey);
167 key_put(rkey);
168 out:
169 kleave(" = %d", ret);
170 return ret;
171 }
172 EXPORT_SYMBOL(dns_query);