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

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>

authored by

Andy Adamson and committed by
Trond Myklebust
4de6caa2 f1ff0c27

+169 -2
+16
include/linux/sunrpc/auth.h
··· 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 ··· 120 111 rpc_authflavor_t (*info2flavor)(struct rpcsec_gss_info *); 121 112 int (*flavor2info)(rpc_authflavor_t, 122 113 struct rpcsec_gss_info *); 114 + int (*key_timeout)(struct rpc_auth *, 115 + struct rpc_cred *); 123 116 }; 124 117 125 118 struct rpc_credops { ··· 138 127 void *, __be32 *, void *); 139 128 int (*crunwrap_resp)(struct rpc_task *, kxdrdproc_t, 140 129 void *, __be32 *, void *); 130 + int (*crkey_timeout)(struct rpc_cred *); 131 + bool (*crkey_to_expire)(struct rpc_cred *); 141 132 }; 142 133 143 134 extern const struct rpc_authops authunix_ops; ··· 179 166 int rpcauth_init_credcache(struct rpc_auth *); 180 167 void rpcauth_destroy_credcache(struct rpc_auth *); 181 168 void rpcauth_clear_credcache(struct rpc_cred_cache *); 169 + int rpcauth_key_timeout_notify(struct rpc_auth *, 170 + struct rpc_cred *); 171 + bool rpcauth_cred_key_to_expire(struct rpc_cred *); 182 172 183 173 static inline 184 174 struct rpc_cred * get_rpccred(struct rpc_cred *cred)
+21
net/sunrpc/auth.c
··· 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
+82
net/sunrpc/auth_generic.c
··· 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; ··· 183 182 rpcauth_destroy_credcache(&generic_auth); 184 183 } 185 184 185 + /* 186 + * Test the the current time (now) against the underlying credential key expiry 187 + * minus a timeout and setup notification. 188 + * 189 + * The normal case: 190 + * If 'now' is before the key expiry minus RPC_KEY_EXPIRE_TIMEO, set 191 + * the RPC_CRED_NOTIFY_TIMEOUT flag to setup the underlying credential 192 + * rpc_credops crmatch routine to notify this generic cred when it's key 193 + * expiration is within RPC_KEY_EXPIRE_TIMEO, and return 0. 194 + * 195 + * The error case: 196 + * If the underlying cred lookup fails, return -EACCES. 197 + * 198 + * The 'almost' error case: 199 + * If 'now' is within key expiry minus RPC_KEY_EXPIRE_TIMEO, but not within 200 + * key expiry minus RPC_KEY_EXPIRE_FAIL, set the RPC_CRED_EXPIRE_SOON bit 201 + * on the acred ac_flags and return 0. 202 + */ 203 + static int 204 + generic_key_timeout(struct rpc_auth *auth, struct rpc_cred *cred) 205 + { 206 + struct auth_cred *acred = &container_of(cred, struct generic_cred, 207 + gc_base)->acred; 208 + struct rpc_cred *tcred; 209 + int ret = 0; 210 + 211 + 212 + /* Fast track for non crkey_timeout (no key) underlying credentials */ 213 + if (test_bit(RPC_CRED_NO_CRKEY_TIMEOUT, &acred->ac_flags)) 214 + return 0; 215 + 216 + /* Fast track for the normal case */ 217 + if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags)) 218 + return 0; 219 + 220 + /* lookup_cred either returns a valid referenced rpc_cred, or PTR_ERR */ 221 + tcred = auth->au_ops->lookup_cred(auth, acred, 0); 222 + if (IS_ERR(tcred)) 223 + return -EACCES; 224 + 225 + if (!tcred->cr_ops->crkey_timeout) { 226 + set_bit(RPC_CRED_NO_CRKEY_TIMEOUT, &acred->ac_flags); 227 + ret = 0; 228 + goto out_put; 229 + } 230 + 231 + /* Test for the almost error case */ 232 + ret = tcred->cr_ops->crkey_timeout(tcred); 233 + if (ret != 0) { 234 + set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags); 235 + ret = 0; 236 + } else { 237 + /* In case underlying cred key has been reset */ 238 + if (test_and_clear_bit(RPC_CRED_KEY_EXPIRE_SOON, 239 + &acred->ac_flags)) 240 + dprintk("RPC: UID %d Credential key reset\n", 241 + tcred->cr_uid); 242 + /* set up fasttrack for the normal case */ 243 + set_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags); 244 + } 245 + 246 + out_put: 247 + put_rpccred(tcred); 248 + return ret; 249 + } 250 + 186 251 static const struct rpc_authops generic_auth_ops = { 187 252 .owner = THIS_MODULE, 188 253 .au_name = "Generic", 189 254 .lookup_cred = generic_lookup_cred, 190 255 .crcreate = generic_create_cred, 256 + .key_timeout = generic_key_timeout, 191 257 }; 192 258 193 259 static struct rpc_auth generic_auth = { ··· 262 194 .au_count = ATOMIC_INIT(0), 263 195 }; 264 196 197 + static bool generic_key_to_expire(struct rpc_cred *cred) 198 + { 199 + struct auth_cred *acred = &container_of(cred, struct generic_cred, 200 + gc_base)->acred; 201 + bool ret; 202 + 203 + get_rpccred(cred); 204 + ret = test_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags); 205 + put_rpccred(cred); 206 + 207 + return ret; 208 + } 209 + 265 210 static const struct rpc_credops generic_credops = { 266 211 .cr_name = "Generic cred", 267 212 .crdestroy = generic_destroy_cred, 268 213 .crbind = generic_bind_cred, 269 214 .crmatch = generic_match, 215 + .crkey_to_expire = generic_key_to_expire, 270 216 };
+50 -2
net/sunrpc/auth_gss/auth_gss.c
··· 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 ··· 1298 1295 return err; 1299 1296 } 1300 1297 1298 + /* 1299 + * Returns -EACCES if GSS context is NULL or will expire within the 1300 + * timeout (miliseconds) 1301 + */ 1302 + static int 1303 + gss_key_timeout(struct rpc_cred *rc) 1304 + { 1305 + struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base); 1306 + unsigned long now = jiffies; 1307 + unsigned long expire; 1308 + 1309 + if (gss_cred->gc_ctx == NULL) 1310 + return -EACCES; 1311 + 1312 + expire = gss_cred->gc_ctx->gc_expiry - (gss_key_expire_timeo * HZ); 1313 + 1314 + if (time_after(now, expire)) 1315 + return -EACCES; 1316 + return 0; 1317 + } 1318 + 1301 1319 static int 1302 1320 gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags) 1303 1321 { 1304 1322 struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base); 1323 + int ret; 1305 1324 1306 1325 if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags)) 1307 1326 goto out; ··· 1336 1311 if (acred->principal != NULL) { 1337 1312 if (gss_cred->gc_principal == NULL) 1338 1313 return 0; 1339 - return strcmp(acred->principal, gss_cred->gc_principal) == 0; 1314 + ret = strcmp(acred->principal, gss_cred->gc_principal) == 0; 1315 + goto check_expire; 1340 1316 } 1341 1317 if (gss_cred->gc_principal != NULL) 1342 1318 return 0; 1343 - return uid_eq(rc->cr_uid, acred->uid); 1319 + ret = uid_eq(rc->cr_uid, acred->uid); 1320 + 1321 + check_expire: 1322 + if (ret == 0) 1323 + return ret; 1324 + 1325 + /* Notify acred users of GSS context expiration timeout */ 1326 + if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags) && 1327 + (gss_key_timeout(rc) != 0)) { 1328 + /* test will now be done from generic cred */ 1329 + test_and_clear_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags); 1330 + /* tell NFS layer that key will expire soon */ 1331 + set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags); 1332 + } 1333 + return ret; 1344 1334 } 1345 1335 1346 1336 /* ··· 1882 1842 .crvalidate = gss_validate, 1883 1843 .crwrap_req = gss_wrap_req, 1884 1844 .crunwrap_resp = gss_unwrap_resp, 1845 + .crkey_timeout = gss_key_timeout, 1885 1846 }; 1886 1847 1887 1848 static const struct rpc_credops gss_nullops = { ··· 1969 1928 uint, 0644); 1970 1929 MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until " 1971 1930 "the RPC engine retries an expired credential"); 1931 + 1932 + module_param_named(key_expire_timeo, 1933 + gss_key_expire_timeo, 1934 + uint, 0644); 1935 + MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a " 1936 + "credential keys lifetime where the NFS layer cleans up " 1937 + "prior to key expiration"); 1972 1938 1973 1939 module_init(init_rpcsec_gss) 1974 1940 module_exit(exit_rpcsec_gss)