Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

DNS: If the DNS server returns an error, allow that to be cached [ver #2]

If the DNS server returns an error, allow that to be cached in the DNS resolver
key in lieu of a value. Userspace passes the desired error number as an option
in the payload:

"#dnserror=<number>"

Userspace must map h_errno from the name resolution routines to an appropriate
Linux error before passing it up. Something like the following mapping is
recommended:

[HOST_NOT_FOUND] = ENODATA,
[TRY_AGAIN] = EAGAIN,
[NO_RECOVERY] = ECONNREFUSED,
[NO_DATA] = ENODATA,

in lieu of Linux errors specifically for representing name service errors. The
filesystem must map these errors appropropriately before passing them to
userspace. AFS is made to map ENODATA and EAGAIN to EDESTADDRREQ for the
return to userspace; ECONNREFUSED is allowed to stand as is.

The error can be seen in /proc/keys as a negative number after the description
of the key. Compare, for example, the following key entries:

2f97238c I--Q-- 1 53s 3f010000 0 0 dns_resol afsdb:grand.centrall.org: -61
338bfbbe I--Q-- 1 59m 3f010000 0 0 dns_resol afsdb:grand.central.org: 37

If the error option is supplied in the payload, the main part of the payload is
discarded. The key should have an expiry time set by userspace.

Signed-off-by: Wang Lei <wang840925@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Jeff Layton <jlayton@redhat.com>
Signed-off-by: Steve French <sfrench@us.ibm.com>

authored by

Wang Lei and committed by
Steve French
4a2d7892 c2e8139c

+96 -5
+4
fs/afs/cell.c
··· 73 73 if (!vllist || strlen(vllist) < 7) { 74 74 ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL); 75 75 if (ret < 0) { 76 + if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY) 77 + /* translate these errors into something 78 + * userspace might understand */ 79 + ret = -EDESTADDRREQ; 76 80 _leave(" = %d", ret); 77 81 return ERR_PTR(ret); 78 82 }
+87 -5
net/dns_resolver/dns_key.c
··· 29 29 #include <linux/kernel.h> 30 30 #include <linux/keyctl.h> 31 31 #include <linux/err.h> 32 + #include <linux/seq_file.h> 32 33 #include <keys/dns_resolver-type.h> 33 34 #include <keys/user-type.h> 34 35 #include "internal.h" ··· 43 42 MODULE_PARM_DESC(debug, "DNS Resolver debugging mask"); 44 43 45 44 const struct cred *dns_resolver_cache; 45 + 46 + #define DNS_ERRORNO_OPTION "dnserror" 46 47 47 48 /* 48 49 * Instantiate a user defined key for dns_resolver. ··· 62 59 dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen) 63 60 { 64 61 struct user_key_payload *upayload; 62 + unsigned long derrno; 65 63 int ret; 66 64 size_t result_len = 0; 67 - const char *data = _data, *opt; 65 + const char *data = _data, *end, *opt; 68 66 69 67 kenter("%%%d,%s,'%s',%zu", 70 68 key->serial, key->description, data, datalen); ··· 75 71 datalen--; 76 72 77 73 /* deal with any options embedded in the data */ 74 + end = data + datalen; 78 75 opt = memchr(data, '#', datalen); 79 76 if (!opt) { 80 - kdebug("no options currently supported"); 81 - return -EINVAL; 77 + /* no options: the entire data is the result */ 78 + kdebug("no options"); 79 + result_len = datalen; 80 + } else { 81 + const char *next_opt; 82 + 83 + result_len = opt - data; 84 + opt++; 85 + kdebug("options: '%s'", opt); 86 + do { 87 + const char *eq; 88 + int opt_len, opt_nlen, opt_vlen, tmp; 89 + 90 + next_opt = memchr(opt, '#', end - opt) ?: end; 91 + opt_len = next_opt - opt; 92 + if (!opt_len) { 93 + printk(KERN_WARNING 94 + "Empty option to dns_resolver key %d\n", 95 + key->serial); 96 + return -EINVAL; 97 + } 98 + 99 + eq = memchr(opt, '=', opt_len) ?: end; 100 + opt_nlen = eq - opt; 101 + eq++; 102 + opt_vlen = next_opt - eq; /* will be -1 if no value */ 103 + 104 + tmp = opt_vlen >= 0 ? opt_vlen : 0; 105 + kdebug("option '%*.*s' val '%*.*s'", 106 + opt_nlen, opt_nlen, opt, tmp, tmp, eq); 107 + 108 + /* see if it's an error number representing a DNS error 109 + * that's to be recorded as the result in this key */ 110 + if (opt_nlen == sizeof(DNS_ERRORNO_OPTION) - 1 && 111 + memcmp(opt, DNS_ERRORNO_OPTION, opt_nlen) == 0) { 112 + kdebug("dns error number option"); 113 + if (opt_vlen <= 0) 114 + goto bad_option_value; 115 + 116 + ret = strict_strtoul(eq, 10, &derrno); 117 + if (ret < 0) 118 + goto bad_option_value; 119 + 120 + if (derrno < 1 || derrno > 511) 121 + goto bad_option_value; 122 + 123 + kdebug("dns error no. = %lu", derrno); 124 + key->type_data.x[0] = -derrno; 125 + continue; 126 + } 127 + 128 + bad_option_value: 129 + printk(KERN_WARNING 130 + "Option '%*.*s' to dns_resolver key %d:" 131 + " bad/missing value\n", 132 + opt_nlen, opt_nlen, opt, key->serial); 133 + return -EINVAL; 134 + } while (opt = next_opt + 1, opt < end); 82 135 } 83 136 84 - result_len = datalen; 137 + /* don't cache the result if we're caching an error saying there's no 138 + * result */ 139 + if (key->type_data.x[0]) { 140 + kleave(" = 0 [h_error %ld]", key->type_data.x[0]); 141 + return 0; 142 + } 143 + 144 + kdebug("store result"); 85 145 ret = key_payload_reserve(key, result_len); 86 146 if (ret < 0) 87 147 return -EINVAL; ··· 203 135 return ret; 204 136 } 205 137 138 + /* 139 + * Describe a DNS key 140 + */ 141 + static void dns_resolver_describe(const struct key *key, struct seq_file *m) 142 + { 143 + int err = key->type_data.x[0]; 144 + 145 + seq_puts(m, key->description); 146 + if (err) 147 + seq_printf(m, ": %d", err); 148 + else 149 + seq_printf(m, ": %u", key->datalen); 150 + } 151 + 206 152 struct key_type key_type_dns_resolver = { 207 153 .name = "dns_resolver", 208 154 .instantiate = dns_resolver_instantiate, 209 155 .match = dns_resolver_match, 210 156 .revoke = user_revoke, 211 157 .destroy = user_destroy, 212 - .describe = user_describe, 158 + .describe = dns_resolver_describe, 213 159 .read = user_read, 214 160 }; 215 161
+5
net/dns_resolver/dns_query.c
··· 136 136 if (ret < 0) 137 137 goto put; 138 138 139 + /* If the DNS server gave an error, return that to the caller */ 140 + ret = rkey->type_data.x[0]; 141 + if (ret) 142 + goto put; 143 + 139 144 upayload = rcu_dereference_protected(rkey->payload.data, 140 145 lockdep_is_held(&rkey->sem)); 141 146 len = upayload->datalen;