Commit 4de6caa270afaa381dd3373e9e6d148b1090e0ec

Authored by Andy Adamson
Committed by Trond Myklebust
1 parent f1ff0c27fd

SUNRPC new rpc_credops to test credential expiry

This patch provides the RPC layer helper functions to allow NFS to manage
data in the face of expired credentials - such as avoiding buffered WRITEs
and COMMITs when the gss context will expire before the WRITEs are flushed
and COMMITs are sent.

These helper functions enable checking the expiration of an underlying
credential key for a generic rpc credential, e.g. the gss_cred gss context
gc_expiry which for Kerberos is set to the remaining TGT lifetime.

A new rpc_authops key_timeout is only defined for the generic auth.
A new rpc_credops crkey_to_expire is only defined for the generic cred.
A new rpc_credops crkey_timeout is only defined for the gss cred.

Set a credential key expiry watermark, RPC_KEY_EXPIRE_TIMEO set to 240 seconds
as a default and can be set via a module parameter as we need to ensure there
is time for any dirty data to be flushed.

If key_timeout is called on a credential with an underlying credential key that
will expire within watermark seconds, we set the RPC_CRED_KEY_EXPIRE_SOON
flag in the generic_cred acred so that the NFS layer can clean up prior to
key expiration.

Checking a generic credential's underlying credential involves a cred lookup.
To avoid this lookup in the normal case when the underlying credential has
a key that is valid (before the watermark), a notify flag is set in
the generic credential the first time the key_timeout is called. The
generic credential then stops checking the underlying credential key expiry, and
the underlying credential (gss_cred) match routine then checks the key
expiration upon each normal use and sets a flag in the associated generic
credential only when the key expiration is within the watermark.
This in turn signals the generic credential key_timeout to perform the extra
credential lookup thereafter.

Signed-off-by: Andy Adamson <andros@netapp.com>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>

Showing 4 changed files with 169 additions and 2 deletions Side-by-side Diff

include/linux/sunrpc/auth.h
... ... @@ -24,12 +24,21 @@
24 24  
25 25 struct rpcsec_gss_info;
26 26  
  27 +/* auth_cred ac_flags bits */
  28 +enum {
  29 + RPC_CRED_NO_CRKEY_TIMEOUT = 0, /* underlying cred has no key timeout */
  30 + RPC_CRED_KEY_EXPIRE_SOON = 1, /* underlying cred key will expire soon */
  31 + RPC_CRED_NOTIFY_TIMEOUT = 2, /* nofity generic cred when underlying
  32 + key will expire soon */
  33 +};
  34 +
27 35 /* Work around the lack of a VFS credential */
28 36 struct auth_cred {
29 37 kuid_t uid;
30 38 kgid_t gid;
31 39 struct group_info *group_info;
32 40 const char *principal;
  41 + unsigned long ac_flags;
33 42 unsigned char machine_cred : 1;
34 43 };
35 44  
... ... @@ -111,6 +120,8 @@
111 120 rpc_authflavor_t (*info2flavor)(struct rpcsec_gss_info *);
112 121 int (*flavor2info)(rpc_authflavor_t,
113 122 struct rpcsec_gss_info *);
  123 + int (*key_timeout)(struct rpc_auth *,
  124 + struct rpc_cred *);
114 125 };
115 126  
116 127 struct rpc_credops {
... ... @@ -127,6 +138,8 @@
127 138 void *, __be32 *, void *);
128 139 int (*crunwrap_resp)(struct rpc_task *, kxdrdproc_t,
129 140 void *, __be32 *, void *);
  141 + int (*crkey_timeout)(struct rpc_cred *);
  142 + bool (*crkey_to_expire)(struct rpc_cred *);
130 143 };
131 144  
132 145 extern const struct rpc_authops authunix_ops;
... ... @@ -166,6 +179,9 @@
166 179 int rpcauth_init_credcache(struct rpc_auth *);
167 180 void rpcauth_destroy_credcache(struct rpc_auth *);
168 181 void rpcauth_clear_credcache(struct rpc_cred_cache *);
  182 +int rpcauth_key_timeout_notify(struct rpc_auth *,
  183 + struct rpc_cred *);
  184 +bool rpcauth_cred_key_to_expire(struct rpc_cred *);
169 185  
170 186 static inline
171 187 struct rpc_cred * get_rpccred(struct rpc_cred *cred)
... ... @@ -343,6 +343,27 @@
343 343 EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
344 344  
345 345 /*
  346 + * Setup a credential key lifetime timeout notification
  347 + */
  348 +int
  349 +rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
  350 +{
  351 + if (!cred->cr_auth->au_ops->key_timeout)
  352 + return 0;
  353 + return cred->cr_auth->au_ops->key_timeout(auth, cred);
  354 +}
  355 +EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
  356 +
  357 +bool
  358 +rpcauth_cred_key_to_expire(struct rpc_cred *cred)
  359 +{
  360 + if (!cred->cr_ops->crkey_to_expire)
  361 + return false;
  362 + return cred->cr_ops->crkey_to_expire(cred);
  363 +}
  364 +EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
  365 +
  366 +/*
346 367 * Destroy a list of credentials
347 368 */
348 369 static inline
net/sunrpc/auth_generic.c
... ... @@ -89,6 +89,7 @@
89 89 gcred->acred.uid = acred->uid;
90 90 gcred->acred.gid = acred->gid;
91 91 gcred->acred.group_info = acred->group_info;
  92 + gcred->acred.ac_flags = 0;
92 93 if (gcred->acred.group_info != NULL)
93 94 get_group_info(gcred->acred.group_info);
94 95 gcred->acred.machine_cred = acred->machine_cred;
95 96  
... ... @@ -182,11 +183,78 @@
182 183 rpcauth_destroy_credcache(&generic_auth);
183 184 }
184 185  
  186 +/*
  187 + * Test the the current time (now) against the underlying credential key expiry
  188 + * minus a timeout and setup notification.
  189 + *
  190 + * The normal case:
  191 + * If 'now' is before the key expiry minus RPC_KEY_EXPIRE_TIMEO, set
  192 + * the RPC_CRED_NOTIFY_TIMEOUT flag to setup the underlying credential
  193 + * rpc_credops crmatch routine to notify this generic cred when it's key
  194 + * expiration is within RPC_KEY_EXPIRE_TIMEO, and return 0.
  195 + *
  196 + * The error case:
  197 + * If the underlying cred lookup fails, return -EACCES.
  198 + *
  199 + * The 'almost' error case:
  200 + * If 'now' is within key expiry minus RPC_KEY_EXPIRE_TIMEO, but not within
  201 + * key expiry minus RPC_KEY_EXPIRE_FAIL, set the RPC_CRED_EXPIRE_SOON bit
  202 + * on the acred ac_flags and return 0.
  203 + */
  204 +static int
  205 +generic_key_timeout(struct rpc_auth *auth, struct rpc_cred *cred)
  206 +{
  207 + struct auth_cred *acred = &container_of(cred, struct generic_cred,
  208 + gc_base)->acred;
  209 + struct rpc_cred *tcred;
  210 + int ret = 0;
  211 +
  212 +
  213 + /* Fast track for non crkey_timeout (no key) underlying credentials */
  214 + if (test_bit(RPC_CRED_NO_CRKEY_TIMEOUT, &acred->ac_flags))
  215 + return 0;
  216 +
  217 + /* Fast track for the normal case */
  218 + if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags))
  219 + return 0;
  220 +
  221 + /* lookup_cred either returns a valid referenced rpc_cred, or PTR_ERR */
  222 + tcred = auth->au_ops->lookup_cred(auth, acred, 0);
  223 + if (IS_ERR(tcred))
  224 + return -EACCES;
  225 +
  226 + if (!tcred->cr_ops->crkey_timeout) {
  227 + set_bit(RPC_CRED_NO_CRKEY_TIMEOUT, &acred->ac_flags);
  228 + ret = 0;
  229 + goto out_put;
  230 + }
  231 +
  232 + /* Test for the almost error case */
  233 + ret = tcred->cr_ops->crkey_timeout(tcred);
  234 + if (ret != 0) {
  235 + set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
  236 + ret = 0;
  237 + } else {
  238 + /* In case underlying cred key has been reset */
  239 + if (test_and_clear_bit(RPC_CRED_KEY_EXPIRE_SOON,
  240 + &acred->ac_flags))
  241 + dprintk("RPC: UID %d Credential key reset\n",
  242 + tcred->cr_uid);
  243 + /* set up fasttrack for the normal case */
  244 + set_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
  245 + }
  246 +
  247 +out_put:
  248 + put_rpccred(tcred);
  249 + return ret;
  250 +}
  251 +
185 252 static const struct rpc_authops generic_auth_ops = {
186 253 .owner = THIS_MODULE,
187 254 .au_name = "Generic",
188 255 .lookup_cred = generic_lookup_cred,
189 256 .crcreate = generic_create_cred,
  257 + .key_timeout = generic_key_timeout,
190 258 };
191 259  
192 260 static struct rpc_auth generic_auth = {
193 261  
... ... @@ -194,10 +262,24 @@
194 262 .au_count = ATOMIC_INIT(0),
195 263 };
196 264  
  265 +static bool generic_key_to_expire(struct rpc_cred *cred)
  266 +{
  267 + struct auth_cred *acred = &container_of(cred, struct generic_cred,
  268 + gc_base)->acred;
  269 + bool ret;
  270 +
  271 + get_rpccred(cred);
  272 + ret = test_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
  273 + put_rpccred(cred);
  274 +
  275 + return ret;
  276 +}
  277 +
197 278 static const struct rpc_credops generic_credops = {
198 279 .cr_name = "Generic cred",
199 280 .crdestroy = generic_destroy_cred,
200 281 .crbind = generic_bind_cred,
201 282 .crmatch = generic_match,
  283 + .crkey_to_expire = generic_key_to_expire,
202 284 };
net/sunrpc/auth_gss/auth_gss.c
... ... @@ -63,6 +63,9 @@
63 63 #define GSS_RETRY_EXPIRED 5
64 64 static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
65 65  
  66 +#define GSS_KEY_EXPIRE_TIMEO 240
  67 +static unsigned int gss_key_expire_timeo = GSS_KEY_EXPIRE_TIMEO;
  68 +
66 69 #ifdef RPC_DEBUG
67 70 # define RPCDBG_FACILITY RPCDBG_AUTH
68 71 #endif
69 72  
70 73  
... ... @@ -1295,10 +1298,32 @@
1295 1298 return err;
1296 1299 }
1297 1300  
  1301 +/*
  1302 + * Returns -EACCES if GSS context is NULL or will expire within the
  1303 + * timeout (miliseconds)
  1304 + */
1298 1305 static int
  1306 +gss_key_timeout(struct rpc_cred *rc)
  1307 +{
  1308 + struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  1309 + unsigned long now = jiffies;
  1310 + unsigned long expire;
  1311 +
  1312 + if (gss_cred->gc_ctx == NULL)
  1313 + return -EACCES;
  1314 +
  1315 + expire = gss_cred->gc_ctx->gc_expiry - (gss_key_expire_timeo * HZ);
  1316 +
  1317 + if (time_after(now, expire))
  1318 + return -EACCES;
  1319 + return 0;
  1320 +}
  1321 +
  1322 +static int
1299 1323 gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
1300 1324 {
1301 1325 struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
  1326 + int ret;
1302 1327  
1303 1328 if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
1304 1329 goto out;
1305 1330  
... ... @@ -1311,11 +1336,26 @@
1311 1336 if (acred->principal != NULL) {
1312 1337 if (gss_cred->gc_principal == NULL)
1313 1338 return 0;
1314   - return strcmp(acred->principal, gss_cred->gc_principal) == 0;
  1339 + ret = strcmp(acred->principal, gss_cred->gc_principal) == 0;
  1340 + goto check_expire;
1315 1341 }
1316 1342 if (gss_cred->gc_principal != NULL)
1317 1343 return 0;
1318   - return uid_eq(rc->cr_uid, acred->uid);
  1344 + ret = uid_eq(rc->cr_uid, acred->uid);
  1345 +
  1346 +check_expire:
  1347 + if (ret == 0)
  1348 + return ret;
  1349 +
  1350 + /* Notify acred users of GSS context expiration timeout */
  1351 + if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags) &&
  1352 + (gss_key_timeout(rc) != 0)) {
  1353 + /* test will now be done from generic cred */
  1354 + test_and_clear_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
  1355 + /* tell NFS layer that key will expire soon */
  1356 + set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
  1357 + }
  1358 + return ret;
1319 1359 }
1320 1360  
1321 1361 /*
... ... @@ -1842,6 +1882,7 @@
1842 1882 .crvalidate = gss_validate,
1843 1883 .crwrap_req = gss_wrap_req,
1844 1884 .crunwrap_resp = gss_unwrap_resp,
  1885 + .crkey_timeout = gss_key_timeout,
1845 1886 };
1846 1887  
1847 1888 static const struct rpc_credops gss_nullops = {
... ... @@ -1928,6 +1969,13 @@
1928 1969 uint, 0644);
1929 1970 MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1930 1971 "the RPC engine retries an expired credential");
  1972 +
  1973 +module_param_named(key_expire_timeo,
  1974 + gss_key_expire_timeo,
  1975 + uint, 0644);
  1976 +MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a "
  1977 + "credential keys lifetime where the NFS layer cleans up "
  1978 + "prior to key expiration");
1931 1979  
1932 1980 module_init(init_rpcsec_gss)
1933 1981 module_exit(exit_rpcsec_gss)