Skip to content

Commit d98c317

Browse files
dhowellskuba-moo
authored andcommitted
afs: Use rxgk RESPONSE to pass token for callback channel
Implement in kafs the hook for adding appdata into a RESPONSE packet generated in response to an RxGK CHALLENGE packet, and include the key for securing the callback channel so that notifications from the fileserver get encrypted. This will be necessary when more complex notifications are used that convey changed data around. Signed-off-by: David Howells <[email protected]> cc: Marc Dionne <[email protected]> cc: Simon Horman <[email protected]> cc: [email protected] Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d03539d commit d98c317

File tree

5 files changed

+276
-1
lines changed

5 files changed

+276
-1
lines changed

fs/afs/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ config AFS_FS
55
select AF_RXRPC
66
select DNS_RESOLVER
77
select NETFS_SUPPORT
8+
select CRYPTO_KRB5
89
help
910
If you say Y here, you will get an experimental Andrew File System
1011
driver. It currently only supports unsecured read-only AFS access.

fs/afs/cm_security.c

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@
88
#include <linux/slab.h>
99
#include <crypto/krb5.h>
1010
#include "internal.h"
11+
#include "afs_cm.h"
1112
#include "afs_fs.h"
1213
#include "protocol_yfs.h"
1314
#define RXRPC_TRACE_ONLY_DEFINE_ENUMS
1415
#include <trace/events/rxrpc.h>
1516

17+
#define RXGK_SERVER_ENC_TOKEN 1036U // 0x40c
18+
#define xdr_round_up(x) (round_up((x), sizeof(__be32)))
19+
#define xdr_len_object(x) (4 + round_up((x), sizeof(__be32)))
20+
21+
#ifdef CONFIG_RXGK
22+
static int afs_create_yfs_cm_token(struct sk_buff *challenge,
23+
struct afs_server *server);
24+
#endif
25+
1626
/*
1727
* Respond to an RxGK challenge, adding appdata.
1828
*/
1929
static int afs_respond_to_challenge(struct sk_buff *challenge)
2030
{
2131
#ifdef CONFIG_RXGK
2232
struct krb5_buffer appdata = {};
33+
struct afs_server *server;
2334
#endif
2435
struct rxrpc_peer *peer;
2536
unsigned long peer_data;
@@ -55,7 +66,23 @@ static int afs_respond_to_challenge(struct sk_buff *challenge)
5566

5667
#ifdef CONFIG_RXGK
5768
case RXRPC_SECURITY_RXGK:
69+
return rxgk_kernel_respond_to_challenge(challenge, &appdata);
70+
5871
case RXRPC_SECURITY_YFS_RXGK:
72+
switch (service_id) {
73+
case FS_SERVICE:
74+
case YFS_FS_SERVICE:
75+
server = (struct afs_server *)peer_data;
76+
if (!server->cm_rxgk_appdata.data) {
77+
mutex_lock(&server->cm_token_lock);
78+
if (!server->cm_rxgk_appdata.data)
79+
afs_create_yfs_cm_token(challenge, server);
80+
mutex_unlock(&server->cm_token_lock);
81+
}
82+
if (server->cm_rxgk_appdata.data)
83+
appdata = server->cm_rxgk_appdata;
84+
break;
85+
}
5986
return rxgk_kernel_respond_to_challenge(challenge, &appdata);
6087
#endif
6188

@@ -83,3 +110,231 @@ void afs_process_oob_queue(struct work_struct *work)
83110
rxrpc_kernel_free_oob(oob);
84111
}
85112
}
113+
114+
#ifdef CONFIG_RXGK
115+
/*
116+
* Create a securities keyring for the cache manager and attach a key to it for
117+
* the RxGK tokens we want to use to secure the callback connection back from
118+
* the fileserver.
119+
*/
120+
int afs_create_token_key(struct afs_net *net, struct socket *socket)
121+
{
122+
const struct krb5_enctype *krb5;
123+
struct key *ring;
124+
key_ref_t key;
125+
char K0[32], *desc;
126+
int ret;
127+
128+
ring = keyring_alloc("kafs",
129+
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
130+
KEY_POS_SEARCH | KEY_POS_WRITE |
131+
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH,
132+
KEY_ALLOC_NOT_IN_QUOTA,
133+
NULL, NULL);
134+
if (IS_ERR(ring))
135+
return PTR_ERR(ring);
136+
137+
ret = rxrpc_sock_set_security_keyring(socket->sk, ring);
138+
if (ret < 0)
139+
goto out;
140+
141+
ret = -ENOPKG;
142+
krb5 = crypto_krb5_find_enctype(KRB5_ENCTYPE_AES128_CTS_HMAC_SHA1_96);
143+
if (!krb5)
144+
goto out;
145+
146+
if (WARN_ON_ONCE(krb5->key_len > sizeof(K0)))
147+
goto out;
148+
149+
ret = -ENOMEM;
150+
desc = kasprintf(GFP_KERNEL, "%u:%u:%u:%u",
151+
YFS_CM_SERVICE, RXRPC_SECURITY_YFS_RXGK, 1, krb5->etype);
152+
if (!desc)
153+
goto out;
154+
155+
wait_for_random_bytes();
156+
get_random_bytes(K0, krb5->key_len);
157+
158+
key = key_create(make_key_ref(ring, true),
159+
"rxrpc_s", desc,
160+
K0, krb5->key_len,
161+
KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_USR_VIEW,
162+
KEY_ALLOC_NOT_IN_QUOTA);
163+
kfree(desc);
164+
if (IS_ERR(key)) {
165+
ret = PTR_ERR(key);
166+
goto out;
167+
}
168+
169+
net->fs_cm_token_key = key_ref_to_ptr(key);
170+
ret = 0;
171+
out:
172+
key_put(ring);
173+
return ret;
174+
}
175+
176+
/*
177+
* Create an YFS RxGK GSS token to use as a ticket to the specified fileserver.
178+
*/
179+
static int afs_create_yfs_cm_token(struct sk_buff *challenge,
180+
struct afs_server *server)
181+
{
182+
const struct krb5_enctype *conn_krb5, *token_krb5;
183+
const struct krb5_buffer *token_key;
184+
struct crypto_aead *aead;
185+
struct scatterlist sg;
186+
struct afs_net *net = server->cell->net;
187+
const struct key *key = net->fs_cm_token_key;
188+
size_t keysize, uuidsize, authsize, toksize, encsize, contsize, adatasize, offset;
189+
__be32 caps[1] = {
190+
[0] = htonl(AFS_CAP_ERROR_TRANSLATION),
191+
};
192+
__be32 *xdr;
193+
void *appdata, *K0, *encbase;
194+
u32 enctype;
195+
int ret;
196+
197+
if (!key)
198+
return -ENOKEY;
199+
200+
/* Assume that the fileserver is happy to use the same encoding type as
201+
* we were told to use by the token obtained by the user.
202+
*/
203+
enctype = rxgk_kernel_query_challenge(challenge);
204+
205+
conn_krb5 = crypto_krb5_find_enctype(enctype);
206+
if (!conn_krb5)
207+
return -ENOPKG;
208+
token_krb5 = key->payload.data[0];
209+
token_key = (const struct krb5_buffer *)&key->payload.data[2];
210+
211+
/* struct rxgk_key {
212+
* afs_uint32 enctype;
213+
* opaque key<>;
214+
* };
215+
*/
216+
keysize = 4 + xdr_len_object(conn_krb5->key_len);
217+
218+
/* struct RXGK_AuthName {
219+
* afs_int32 kind;
220+
* opaque data<AUTHDATAMAX>;
221+
* opaque display<AUTHPRINTABLEMAX>;
222+
* };
223+
*/
224+
uuidsize = sizeof(server->uuid);
225+
authsize = 4 + xdr_len_object(uuidsize) + xdr_len_object(0);
226+
227+
/* struct RXGK_Token {
228+
* rxgk_key K0;
229+
* RXGK_Level level;
230+
* rxgkTime starttime;
231+
* afs_int32 lifetime;
232+
* afs_int32 bytelife;
233+
* rxgkTime expirationtime;
234+
* struct RXGK_AuthName identities<>;
235+
* };
236+
*/
237+
toksize = keysize + 8 + 4 + 4 + 8 + xdr_len_object(authsize);
238+
239+
offset = 0;
240+
encsize = crypto_krb5_how_much_buffer(token_krb5, KRB5_ENCRYPT_MODE, toksize, &offset);
241+
242+
/* struct RXGK_TokenContainer {
243+
* afs_int32 kvno;
244+
* afs_int32 enctype;
245+
* opaque encrypted_token<>;
246+
* };
247+
*/
248+
contsize = 4 + 4 + xdr_len_object(encsize);
249+
250+
/* struct YFSAppData {
251+
* opr_uuid initiatorUuid;
252+
* opr_uuid acceptorUuid;
253+
* Capabilities caps;
254+
* afs_int32 enctype;
255+
* opaque callbackKey<>;
256+
* opaque callbackToken<>;
257+
* };
258+
*/
259+
adatasize = 16 + 16 +
260+
xdr_len_object(sizeof(caps)) +
261+
4 +
262+
xdr_len_object(conn_krb5->key_len) +
263+
xdr_len_object(contsize);
264+
265+
ret = -ENOMEM;
266+
appdata = kzalloc(adatasize, GFP_KERNEL);
267+
if (!appdata)
268+
goto out;
269+
xdr = appdata;
270+
271+
memcpy(xdr, &net->uuid, 16); /* appdata.initiatorUuid */
272+
xdr += 16 / 4;
273+
memcpy(xdr, &server->uuid, 16); /* appdata.acceptorUuid */
274+
xdr += 16 / 4;
275+
*xdr++ = htonl(ARRAY_SIZE(caps)); /* appdata.caps.len */
276+
memcpy(xdr, &caps, sizeof(caps)); /* appdata.caps */
277+
xdr += ARRAY_SIZE(caps);
278+
*xdr++ = htonl(conn_krb5->etype); /* appdata.enctype */
279+
280+
*xdr++ = htonl(conn_krb5->key_len); /* appdata.callbackKey.len */
281+
K0 = xdr;
282+
get_random_bytes(K0, conn_krb5->key_len); /* appdata.callbackKey.data */
283+
xdr += xdr_round_up(conn_krb5->key_len) / 4;
284+
285+
*xdr++ = htonl(contsize); /* appdata.callbackToken.len */
286+
*xdr++ = htonl(1); /* cont.kvno */
287+
*xdr++ = htonl(token_krb5->etype); /* cont.enctype */
288+
*xdr++ = htonl(encsize); /* cont.encrypted_token.len */
289+
290+
encbase = xdr;
291+
xdr += offset / 4;
292+
*xdr++ = htonl(conn_krb5->etype); /* token.K0.enctype */
293+
*xdr++ = htonl(conn_krb5->key_len); /* token.K0.key.len */
294+
memcpy(xdr, K0, conn_krb5->key_len); /* token.K0.key.data */
295+
xdr += xdr_round_up(conn_krb5->key_len) / 4;
296+
297+
*xdr++ = htonl(RXRPC_SECURITY_ENCRYPT); /* token.level */
298+
*xdr++ = htonl(0); /* token.starttime */
299+
*xdr++ = htonl(0); /* " */
300+
*xdr++ = htonl(0); /* token.lifetime */
301+
*xdr++ = htonl(0); /* token.bytelife */
302+
*xdr++ = htonl(0); /* token.expirationtime */
303+
*xdr++ = htonl(0); /* " */
304+
*xdr++ = htonl(1); /* token.identities.count */
305+
*xdr++ = htonl(0); /* token.identities[0].kind */
306+
*xdr++ = htonl(uuidsize); /* token.identities[0].data.len */
307+
memcpy(xdr, &server->uuid, uuidsize);
308+
xdr += xdr_round_up(uuidsize) / 4;
309+
*xdr++ = htonl(0); /* token.identities[0].display.len */
310+
311+
xdr = encbase + xdr_round_up(encsize);
312+
313+
if ((unsigned long)xdr - (unsigned long)appdata != adatasize)
314+
pr_err("Appdata size incorrect %lx != %zx\n",
315+
(unsigned long)xdr - (unsigned long)appdata, adatasize);
316+
317+
aead = crypto_krb5_prepare_encryption(token_krb5, token_key, RXGK_SERVER_ENC_TOKEN,
318+
GFP_KERNEL);
319+
if (IS_ERR(aead)) {
320+
ret = PTR_ERR(aead);
321+
goto out_token;
322+
}
323+
324+
sg_init_one(&sg, encbase, encsize);
325+
ret = crypto_krb5_encrypt(token_krb5, aead, &sg, 1, encsize, offset, toksize, false);
326+
if (ret < 0)
327+
goto out_aead;
328+
329+
server->cm_rxgk_appdata.len = adatasize;
330+
server->cm_rxgk_appdata.data = appdata;
331+
appdata = NULL;
332+
333+
out_aead:
334+
crypto_free_aead(aead);
335+
out_token:
336+
kfree(appdata);
337+
out:
338+
return ret;
339+
}
340+
#endif /* CONFIG_RXGK */

fs/afs/internal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/uuid.h>
2121
#include <linux/mm_types.h>
2222
#include <linux/dns_resolver.h>
23+
#include <crypto/krb5.h>
2324
#include <net/net_namespace.h>
2425
#include <net/netns/generic.h>
2526
#include <net/sock.h>
@@ -308,6 +309,7 @@ struct afs_net {
308309
struct list_head fs_probe_slow; /* List of afs_server to probe at 5m intervals */
309310
struct hlist_head fs_proc; /* procfs servers list */
310311

312+
struct key *fs_cm_token_key; /* Key for creating CM tokens */
311313
struct work_struct fs_prober;
312314
struct timer_list fs_probe_timer;
313315
atomic_t servers_outstanding;
@@ -543,6 +545,8 @@ struct afs_server {
543545
struct list_head volumes; /* RCU list of afs_server_entry objects */
544546
struct work_struct destroyer; /* Work item to try and destroy a server */
545547
struct timer_list timer; /* Management timer */
548+
struct mutex cm_token_lock; /* Lock governing creation of appdata */
549+
struct krb5_buffer cm_rxgk_appdata; /* Appdata to be included in RESPONSE packet */
546550
time64_t unuse_time; /* Time at which last unused */
547551
unsigned long flags;
548552
#define AFS_SERVER_FL_RESPONDING 0 /* The server is responding */
@@ -1065,6 +1069,14 @@ extern bool afs_cm_incoming_call(struct afs_call *);
10651069
* cm_security.c
10661070
*/
10671071
void afs_process_oob_queue(struct work_struct *work);
1072+
#ifdef CONFIG_RXGK
1073+
int afs_create_token_key(struct afs_net *net, struct socket *socket);
1074+
#else
1075+
static inline int afs_create_token_key(struct afs_net *net, struct socket *socket)
1076+
{
1077+
return 0;
1078+
}
1079+
#endif
10681080

10691081
/*
10701082
* dir.c

fs/afs/rxrpc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ int afs_open_socket(struct afs_net *net)
7878
if (ret < 0)
7979
goto error_2;
8080

81+
ret = afs_create_token_key(net, socket);
82+
if (ret < 0)
83+
pr_err("Couldn't create RxGK CM key: %d\n", ret);
84+
8185
ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
8286
if (ret == -EADDRINUSE) {
8387
srx.transport.sin6.sin6_port = 0;
@@ -140,6 +144,7 @@ void afs_close_socket(struct afs_net *net)
140144
flush_workqueue(afs_async_calls);
141145
net->socket->sk->sk_user_data = NULL;
142146
sock_release(net->socket);
147+
key_put(net->fs_cm_token_key);
143148

144149
_debug("dework");
145150
_leave("");
@@ -820,7 +825,7 @@ static int afs_deliver_cm_op_id(struct afs_call *call)
820825
trace_afs_cb_call(call);
821826
call->work.func = call->type->work;
822827

823-
/* pass responsibility for the remainer of this message off to the
828+
/* pass responsibility for the remainder of this message off to the
824829
* cache manager op */
825830
return call->type->deliver(call);
826831
}

fs/afs/server.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static struct afs_server *afs_alloc_server(struct afs_cell *cell, const uuid_t *
131131
timer_setup(&server->timer, afs_server_timer, 0);
132132
INIT_LIST_HEAD(&server->volumes);
133133
init_waitqueue_head(&server->probe_wq);
134+
mutex_init(&server->cm_token_lock);
134135
INIT_LIST_HEAD(&server->probe_link);
135136
INIT_HLIST_NODE(&server->proc_link);
136137
spin_lock_init(&server->probe_lock);
@@ -396,6 +397,7 @@ static void afs_server_rcu(struct rcu_head *rcu)
396397
afs_put_endpoint_state(rcu_access_pointer(server->endpoint_state),
397398
afs_estate_trace_put_server);
398399
afs_put_cell(server->cell, afs_cell_trace_put_server);
400+
kfree(server->cm_rxgk_appdata.data);
399401
kfree(server);
400402
}
401403

0 commit comments

Comments
 (0)