]> Git Repo - linux.git/blob - net/rxrpc/rxkad.c
libbpf: checkpatch: Fixed code alignments in ringbuf.c
[linux.git] / net / rxrpc / rxkad.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kerberos-based RxRPC security
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells ([email protected])
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <crypto/skcipher.h>
11 #include <linux/module.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/udp.h>
15 #include <linux/scatterlist.h>
16 #include <linux/ctype.h>
17 #include <linux/slab.h>
18 #include <linux/key-type.h>
19 #include <net/sock.h>
20 #include <net/af_rxrpc.h>
21 #include <keys/rxrpc-type.h>
22 #include "ar-internal.h"
23
24 #define RXKAD_VERSION                   2
25 #define MAXKRB5TICKETLEN                1024
26 #define RXKAD_TKT_TYPE_KERBEROS_V5      256
27 #define ANAME_SZ                        40      /* size of authentication name */
28 #define INST_SZ                         40      /* size of principal's instance */
29 #define REALM_SZ                        40      /* size of principal's auth domain */
30 #define SNAME_SZ                        40      /* size of service name */
31 #define RXKAD_ALIGN                     8
32
33 struct rxkad_level1_hdr {
34         __be32  data_size;      /* true data size (excluding padding) */
35 };
36
37 struct rxkad_level2_hdr {
38         __be32  data_size;      /* true data size (excluding padding) */
39         __be32  checksum;       /* decrypted data checksum */
40 };
41
42 static int rxkad_prime_packet_security(struct rxrpc_connection *conn,
43                                        struct crypto_sync_skcipher *ci);
44
45 /*
46  * this holds a pinned cipher so that keventd doesn't get called by the cipher
47  * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
48  * packets
49  */
50 static struct crypto_sync_skcipher *rxkad_ci;
51 static struct skcipher_request *rxkad_ci_req;
52 static DEFINE_MUTEX(rxkad_ci_mutex);
53
54 /*
55  * Parse the information from a server key
56  *
57  * The data should be the 8-byte secret key.
58  */
59 static int rxkad_preparse_server_key(struct key_preparsed_payload *prep)
60 {
61         struct crypto_skcipher *ci;
62
63         if (prep->datalen != 8)
64                 return -EINVAL;
65
66         memcpy(&prep->payload.data[2], prep->data, 8);
67
68         ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
69         if (IS_ERR(ci)) {
70                 _leave(" = %ld", PTR_ERR(ci));
71                 return PTR_ERR(ci);
72         }
73
74         if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
75                 BUG();
76
77         prep->payload.data[0] = ci;
78         _leave(" = 0");
79         return 0;
80 }
81
82 static void rxkad_free_preparse_server_key(struct key_preparsed_payload *prep)
83 {
84
85         if (prep->payload.data[0])
86                 crypto_free_skcipher(prep->payload.data[0]);
87 }
88
89 static void rxkad_destroy_server_key(struct key *key)
90 {
91         if (key->payload.data[0]) {
92                 crypto_free_skcipher(key->payload.data[0]);
93                 key->payload.data[0] = NULL;
94         }
95 }
96
97 /*
98  * initialise connection security
99  */
100 static int rxkad_init_connection_security(struct rxrpc_connection *conn,
101                                           struct rxrpc_key_token *token)
102 {
103         struct crypto_sync_skcipher *ci;
104         int ret;
105
106         _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key));
107
108         conn->security_ix = token->security_index;
109
110         ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
111         if (IS_ERR(ci)) {
112                 _debug("no cipher");
113                 ret = PTR_ERR(ci);
114                 goto error;
115         }
116
117         if (crypto_sync_skcipher_setkey(ci, token->kad->session_key,
118                                    sizeof(token->kad->session_key)) < 0)
119                 BUG();
120
121         switch (conn->params.security_level) {
122         case RXRPC_SECURITY_PLAIN:
123         case RXRPC_SECURITY_AUTH:
124         case RXRPC_SECURITY_ENCRYPT:
125                 break;
126         default:
127                 ret = -EKEYREJECTED;
128                 goto error;
129         }
130
131         ret = rxkad_prime_packet_security(conn, ci);
132         if (ret < 0)
133                 goto error_ci;
134
135         conn->rxkad.cipher = ci;
136         return 0;
137
138 error_ci:
139         crypto_free_sync_skcipher(ci);
140 error:
141         _leave(" = %d", ret);
142         return ret;
143 }
144
145 /*
146  * Work out how much data we can put in a packet.
147  */
148 static int rxkad_how_much_data(struct rxrpc_call *call, size_t remain,
149                                size_t *_buf_size, size_t *_data_size, size_t *_offset)
150 {
151         size_t shdr, buf_size, chunk;
152
153         switch (call->conn->params.security_level) {
154         default:
155                 buf_size = chunk = min_t(size_t, remain, RXRPC_JUMBO_DATALEN);
156                 shdr = 0;
157                 goto out;
158         case RXRPC_SECURITY_AUTH:
159                 shdr = sizeof(struct rxkad_level1_hdr);
160                 break;
161         case RXRPC_SECURITY_ENCRYPT:
162                 shdr = sizeof(struct rxkad_level2_hdr);
163                 break;
164         }
165
166         buf_size = round_down(RXRPC_JUMBO_DATALEN, RXKAD_ALIGN);
167
168         chunk = buf_size - shdr;
169         if (remain < chunk)
170                 buf_size = round_up(shdr + remain, RXKAD_ALIGN);
171
172 out:
173         *_buf_size = buf_size;
174         *_data_size = chunk;
175         *_offset = shdr;
176         return 0;
177 }
178
179 /*
180  * prime the encryption state with the invariant parts of a connection's
181  * description
182  */
183 static int rxkad_prime_packet_security(struct rxrpc_connection *conn,
184                                        struct crypto_sync_skcipher *ci)
185 {
186         struct skcipher_request *req;
187         struct rxrpc_key_token *token;
188         struct scatterlist sg;
189         struct rxrpc_crypt iv;
190         __be32 *tmpbuf;
191         size_t tmpsize = 4 * sizeof(__be32);
192
193         _enter("");
194
195         if (!conn->params.key)
196                 return 0;
197
198         tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
199         if (!tmpbuf)
200                 return -ENOMEM;
201
202         req = skcipher_request_alloc(&ci->base, GFP_NOFS);
203         if (!req) {
204                 kfree(tmpbuf);
205                 return -ENOMEM;
206         }
207
208         token = conn->params.key->payload.data[0];
209         memcpy(&iv, token->kad->session_key, sizeof(iv));
210
211         tmpbuf[0] = htonl(conn->proto.epoch);
212         tmpbuf[1] = htonl(conn->proto.cid);
213         tmpbuf[2] = 0;
214         tmpbuf[3] = htonl(conn->security_ix);
215
216         sg_init_one(&sg, tmpbuf, tmpsize);
217         skcipher_request_set_sync_tfm(req, ci);
218         skcipher_request_set_callback(req, 0, NULL, NULL);
219         skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
220         crypto_skcipher_encrypt(req);
221         skcipher_request_free(req);
222
223         memcpy(&conn->rxkad.csum_iv, tmpbuf + 2, sizeof(conn->rxkad.csum_iv));
224         kfree(tmpbuf);
225         _leave(" = 0");
226         return 0;
227 }
228
229 /*
230  * Allocate and prepare the crypto request on a call.  For any particular call,
231  * this is called serially for the packets, so no lock should be necessary.
232  */
233 static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call)
234 {
235         struct crypto_skcipher *tfm = &call->conn->rxkad.cipher->base;
236
237         return skcipher_request_alloc(tfm, GFP_NOFS);
238 }
239
240 /*
241  * Clean up the crypto on a call.
242  */
243 static void rxkad_free_call_crypto(struct rxrpc_call *call)
244 {
245 }
246
247 /*
248  * partially encrypt a packet (level 1 security)
249  */
250 static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
251                                     struct rxrpc_txbuf *txb,
252                                     struct skcipher_request *req)
253 {
254         struct rxkad_level1_hdr *hdr = (void *)txb->data;
255         struct rxrpc_crypt iv;
256         struct scatterlist sg;
257         size_t pad;
258         u16 check;
259
260         _enter("");
261
262         check = txb->seq ^ ntohl(txb->wire.callNumber);
263         hdr->data_size = htonl((u32)check << 16 | txb->len);
264
265         txb->len += sizeof(struct rxkad_level1_hdr);
266         pad = txb->len;
267         pad = RXKAD_ALIGN - pad;
268         pad &= RXKAD_ALIGN - 1;
269         if (pad) {
270                 memset(txb->data + txb->offset, 0, pad);
271                 txb->len += pad;
272         }
273
274         /* start the encryption afresh */
275         memset(&iv, 0, sizeof(iv));
276
277         sg_init_one(&sg, txb->data, 8);
278         skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
279         skcipher_request_set_callback(req, 0, NULL, NULL);
280         skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
281         crypto_skcipher_encrypt(req);
282         skcipher_request_zero(req);
283
284         _leave(" = 0");
285         return 0;
286 }
287
288 /*
289  * wholly encrypt a packet (level 2 security)
290  */
291 static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
292                                        struct rxrpc_txbuf *txb,
293                                        struct skcipher_request *req)
294 {
295         const struct rxrpc_key_token *token;
296         struct rxkad_level2_hdr *rxkhdr = (void *)txb->data;
297         struct rxrpc_crypt iv;
298         struct scatterlist sg;
299         size_t pad;
300         u16 check;
301         int ret;
302
303         _enter("");
304
305         check = txb->seq ^ ntohl(txb->wire.callNumber);
306
307         rxkhdr->data_size = htonl(txb->len | (u32)check << 16);
308         rxkhdr->checksum = 0;
309
310         txb->len += sizeof(struct rxkad_level2_hdr);
311         pad = txb->len;
312         pad = RXKAD_ALIGN - pad;
313         pad &= RXKAD_ALIGN - 1;
314         if (pad) {
315                 memset(txb->data + txb->offset, 0, pad);
316                 txb->len += pad;
317         }
318
319         /* encrypt from the session key */
320         token = call->conn->params.key->payload.data[0];
321         memcpy(&iv, token->kad->session_key, sizeof(iv));
322
323         sg_init_one(&sg, txb->data, txb->len);
324         skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
325         skcipher_request_set_callback(req, 0, NULL, NULL);
326         skcipher_request_set_crypt(req, &sg, &sg, txb->len, iv.x);
327         ret = crypto_skcipher_encrypt(req);
328         skcipher_request_zero(req);
329         return ret;
330 }
331
332 /*
333  * checksum an RxRPC packet header
334  */
335 static int rxkad_secure_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
336 {
337         struct skcipher_request *req;
338         struct rxrpc_crypt iv;
339         struct scatterlist sg;
340         union {
341                 __be32 buf[2];
342         } crypto __aligned(8);
343         u32 x, y;
344         int ret;
345
346         _enter("{%d{%x}},{#%u},%u,",
347                call->debug_id, key_serial(call->conn->params.key),
348                txb->seq, txb->len);
349
350         if (!call->conn->rxkad.cipher)
351                 return 0;
352
353         ret = key_validate(call->conn->params.key);
354         if (ret < 0)
355                 return ret;
356
357         req = rxkad_get_call_crypto(call);
358         if (!req)
359                 return -ENOMEM;
360
361         /* continue encrypting from where we left off */
362         memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv));
363
364         /* calculate the security checksum */
365         x = (ntohl(txb->wire.cid) & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
366         x |= txb->seq & 0x3fffffff;
367         crypto.buf[0] = txb->wire.callNumber;
368         crypto.buf[1] = htonl(x);
369
370         sg_init_one(&sg, crypto.buf, 8);
371         skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
372         skcipher_request_set_callback(req, 0, NULL, NULL);
373         skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
374         crypto_skcipher_encrypt(req);
375         skcipher_request_zero(req);
376
377         y = ntohl(crypto.buf[1]);
378         y = (y >> 16) & 0xffff;
379         if (y == 0)
380                 y = 1; /* zero checksums are not permitted */
381         txb->wire.cksum = htons(y);
382
383         switch (call->conn->params.security_level) {
384         case RXRPC_SECURITY_PLAIN:
385                 ret = 0;
386                 break;
387         case RXRPC_SECURITY_AUTH:
388                 ret = rxkad_secure_packet_auth(call, txb, req);
389                 break;
390         case RXRPC_SECURITY_ENCRYPT:
391                 ret = rxkad_secure_packet_encrypt(call, txb, req);
392                 break;
393         default:
394                 ret = -EPERM;
395                 break;
396         }
397
398         skcipher_request_free(req);
399         _leave(" = %d [set %x]", ret, y);
400         return ret;
401 }
402
403 /*
404  * decrypt partial encryption on a packet (level 1 security)
405  */
406 static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
407                                  rxrpc_seq_t seq,
408                                  struct skcipher_request *req)
409 {
410         struct rxkad_level1_hdr sechdr;
411         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
412         struct rxrpc_crypt iv;
413         struct scatterlist sg[16];
414         bool aborted;
415         u32 data_size, buf;
416         u16 check;
417         int ret;
418
419         _enter("");
420
421         if (sp->len < 8) {
422                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H",
423                                              RXKADSEALEDINCON);
424                 goto protocol_error;
425         }
426
427         /* Decrypt the skbuff in-place.  TODO: We really want to decrypt
428          * directly into the target buffer.
429          */
430         sg_init_table(sg, ARRAY_SIZE(sg));
431         ret = skb_to_sgvec(skb, sg, sp->offset, 8);
432         if (unlikely(ret < 0))
433                 return ret;
434
435         /* start the decryption afresh */
436         memset(&iv, 0, sizeof(iv));
437
438         skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
439         skcipher_request_set_callback(req, 0, NULL, NULL);
440         skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
441         crypto_skcipher_decrypt(req);
442         skcipher_request_zero(req);
443
444         /* Extract the decrypted packet length */
445         if (skb_copy_bits(skb, sp->offset, &sechdr, sizeof(sechdr)) < 0) {
446                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1",
447                                              RXKADDATALEN);
448                 goto protocol_error;
449         }
450         sp->offset += sizeof(sechdr);
451         sp->len    -= sizeof(sechdr);
452
453         buf = ntohl(sechdr.data_size);
454         data_size = buf & 0xffff;
455
456         check = buf >> 16;
457         check ^= seq ^ call->call_id;
458         check &= 0xffff;
459         if (check != 0) {
460                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C",
461                                              RXKADSEALEDINCON);
462                 goto protocol_error;
463         }
464
465         if (data_size > sp->len) {
466                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L",
467                                              RXKADDATALEN);
468                 goto protocol_error;
469         }
470         sp->len = data_size;
471
472         _leave(" = 0 [dlen=%x]", data_size);
473         return 0;
474
475 protocol_error:
476         if (aborted)
477                 rxrpc_send_abort_packet(call);
478         return -EPROTO;
479 }
480
481 /*
482  * wholly decrypt a packet (level 2 security)
483  */
484 static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
485                                  rxrpc_seq_t seq,
486                                  struct skcipher_request *req)
487 {
488         const struct rxrpc_key_token *token;
489         struct rxkad_level2_hdr sechdr;
490         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
491         struct rxrpc_crypt iv;
492         struct scatterlist _sg[4], *sg;
493         bool aborted;
494         u32 data_size, buf;
495         u16 check;
496         int nsg, ret;
497
498         _enter(",{%d}", sp->len);
499
500         if (sp->len < 8) {
501                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H",
502                                              RXKADSEALEDINCON);
503                 goto protocol_error;
504         }
505
506         /* Decrypt the skbuff in-place.  TODO: We really want to decrypt
507          * directly into the target buffer.
508          */
509         sg = _sg;
510         nsg = skb_shinfo(skb)->nr_frags + 1;
511         if (nsg <= 4) {
512                 nsg = 4;
513         } else {
514                 sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO);
515                 if (!sg)
516                         goto nomem;
517         }
518
519         sg_init_table(sg, nsg);
520         ret = skb_to_sgvec(skb, sg, sp->offset, sp->len);
521         if (unlikely(ret < 0)) {
522                 if (sg != _sg)
523                         kfree(sg);
524                 return ret;
525         }
526
527         /* decrypt from the session key */
528         token = call->conn->params.key->payload.data[0];
529         memcpy(&iv, token->kad->session_key, sizeof(iv));
530
531         skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
532         skcipher_request_set_callback(req, 0, NULL, NULL);
533         skcipher_request_set_crypt(req, sg, sg, sp->len, iv.x);
534         crypto_skcipher_decrypt(req);
535         skcipher_request_zero(req);
536         if (sg != _sg)
537                 kfree(sg);
538
539         /* Extract the decrypted packet length */
540         if (skb_copy_bits(skb, sp->offset, &sechdr, sizeof(sechdr)) < 0) {
541                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2",
542                                              RXKADDATALEN);
543                 goto protocol_error;
544         }
545         sp->offset += sizeof(sechdr);
546         sp->len    -= sizeof(sechdr);
547
548         buf = ntohl(sechdr.data_size);
549         data_size = buf & 0xffff;
550
551         check = buf >> 16;
552         check ^= seq ^ call->call_id;
553         check &= 0xffff;
554         if (check != 0) {
555                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C",
556                                              RXKADSEALEDINCON);
557                 goto protocol_error;
558         }
559
560         if (data_size > sp->len) {
561                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L",
562                                              RXKADDATALEN);
563                 goto protocol_error;
564         }
565
566         sp->len = data_size;
567         _leave(" = 0 [dlen=%x]", data_size);
568         return 0;
569
570 protocol_error:
571         if (aborted)
572                 rxrpc_send_abort_packet(call);
573         return -EPROTO;
574
575 nomem:
576         _leave(" = -ENOMEM");
577         return -ENOMEM;
578 }
579
580 /*
581  * Verify the security on a received packet and the subpackets therein.
582  */
583 static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb)
584 {
585         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
586         struct skcipher_request *req;
587         struct rxrpc_crypt iv;
588         struct scatterlist sg;
589         union {
590                 __be32 buf[2];
591         } crypto __aligned(8);
592         rxrpc_seq_t seq = sp->hdr.seq;
593         bool aborted;
594         int ret;
595         u16 cksum;
596         u32 x, y;
597
598         _enter("{%d{%x}},{#%u}",
599                call->debug_id, key_serial(call->conn->params.key), seq);
600
601         if (!call->conn->rxkad.cipher)
602                 return 0;
603
604         req = rxkad_get_call_crypto(call);
605         if (!req)
606                 return -ENOMEM;
607
608         /* continue encrypting from where we left off */
609         memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv));
610
611         /* validate the security checksum */
612         x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
613         x |= seq & 0x3fffffff;
614         crypto.buf[0] = htonl(call->call_id);
615         crypto.buf[1] = htonl(x);
616
617         sg_init_one(&sg, crypto.buf, 8);
618         skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
619         skcipher_request_set_callback(req, 0, NULL, NULL);
620         skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
621         crypto_skcipher_encrypt(req);
622         skcipher_request_zero(req);
623
624         y = ntohl(crypto.buf[1]);
625         cksum = (y >> 16) & 0xffff;
626         if (cksum == 0)
627                 cksum = 1; /* zero checksums are not permitted */
628
629         if (cksum != sp->hdr.cksum) {
630                 aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK",
631                                              RXKADSEALEDINCON);
632                 goto protocol_error;
633         }
634
635         switch (call->conn->params.security_level) {
636         case RXRPC_SECURITY_PLAIN:
637                 ret = 0;
638                 break;
639         case RXRPC_SECURITY_AUTH:
640                 ret = rxkad_verify_packet_1(call, skb, seq, req);
641                 break;
642         case RXRPC_SECURITY_ENCRYPT:
643                 ret = rxkad_verify_packet_2(call, skb, seq, req);
644                 break;
645         default:
646                 ret = -ENOANO;
647                 break;
648         }
649
650         skcipher_request_free(req);
651         return ret;
652
653 protocol_error:
654         if (aborted)
655                 rxrpc_send_abort_packet(call);
656         return -EPROTO;
657 }
658
659 /*
660  * issue a challenge
661  */
662 static int rxkad_issue_challenge(struct rxrpc_connection *conn)
663 {
664         struct rxkad_challenge challenge;
665         struct rxrpc_wire_header whdr;
666         struct msghdr msg;
667         struct kvec iov[2];
668         size_t len;
669         u32 serial;
670         int ret;
671
672         _enter("{%d}", conn->debug_id);
673
674         get_random_bytes(&conn->rxkad.nonce, sizeof(conn->rxkad.nonce));
675
676         challenge.version       = htonl(2);
677         challenge.nonce         = htonl(conn->rxkad.nonce);
678         challenge.min_level     = htonl(0);
679         challenge.__padding     = 0;
680
681         msg.msg_name    = &conn->params.peer->srx.transport;
682         msg.msg_namelen = conn->params.peer->srx.transport_len;
683         msg.msg_control = NULL;
684         msg.msg_controllen = 0;
685         msg.msg_flags   = 0;
686
687         whdr.epoch      = htonl(conn->proto.epoch);
688         whdr.cid        = htonl(conn->proto.cid);
689         whdr.callNumber = 0;
690         whdr.seq        = 0;
691         whdr.type       = RXRPC_PACKET_TYPE_CHALLENGE;
692         whdr.flags      = conn->out_clientflag;
693         whdr.userStatus = 0;
694         whdr.securityIndex = conn->security_ix;
695         whdr._rsvd      = 0;
696         whdr.serviceId  = htons(conn->service_id);
697
698         iov[0].iov_base = &whdr;
699         iov[0].iov_len  = sizeof(whdr);
700         iov[1].iov_base = &challenge;
701         iov[1].iov_len  = sizeof(challenge);
702
703         len = iov[0].iov_len + iov[1].iov_len;
704
705         serial = atomic_inc_return(&conn->serial);
706         whdr.serial = htonl(serial);
707         _proto("Tx CHALLENGE %%%u", serial);
708
709         ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
710         if (ret < 0) {
711                 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
712                                     rxrpc_tx_point_rxkad_challenge);
713                 return -EAGAIN;
714         }
715
716         conn->params.peer->last_tx_at = ktime_get_seconds();
717         trace_rxrpc_tx_packet(conn->debug_id, &whdr,
718                               rxrpc_tx_point_rxkad_challenge);
719         _leave(" = 0");
720         return 0;
721 }
722
723 /*
724  * send a Kerberos security response
725  */
726 static int rxkad_send_response(struct rxrpc_connection *conn,
727                                struct rxrpc_host_header *hdr,
728                                struct rxkad_response *resp,
729                                const struct rxkad_key *s2)
730 {
731         struct rxrpc_wire_header whdr;
732         struct msghdr msg;
733         struct kvec iov[3];
734         size_t len;
735         u32 serial;
736         int ret;
737
738         _enter("");
739
740         msg.msg_name    = &conn->params.peer->srx.transport;
741         msg.msg_namelen = conn->params.peer->srx.transport_len;
742         msg.msg_control = NULL;
743         msg.msg_controllen = 0;
744         msg.msg_flags   = 0;
745
746         memset(&whdr, 0, sizeof(whdr));
747         whdr.epoch      = htonl(hdr->epoch);
748         whdr.cid        = htonl(hdr->cid);
749         whdr.type       = RXRPC_PACKET_TYPE_RESPONSE;
750         whdr.flags      = conn->out_clientflag;
751         whdr.securityIndex = hdr->securityIndex;
752         whdr.serviceId  = htons(hdr->serviceId);
753
754         iov[0].iov_base = &whdr;
755         iov[0].iov_len  = sizeof(whdr);
756         iov[1].iov_base = resp;
757         iov[1].iov_len  = sizeof(*resp);
758         iov[2].iov_base = (void *)s2->ticket;
759         iov[2].iov_len  = s2->ticket_len;
760
761         len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
762
763         serial = atomic_inc_return(&conn->serial);
764         whdr.serial = htonl(serial);
765         _proto("Tx RESPONSE %%%u", serial);
766
767         ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
768         if (ret < 0) {
769                 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
770                                     rxrpc_tx_point_rxkad_response);
771                 return -EAGAIN;
772         }
773
774         conn->params.peer->last_tx_at = ktime_get_seconds();
775         _leave(" = 0");
776         return 0;
777 }
778
779 /*
780  * calculate the response checksum
781  */
782 static void rxkad_calc_response_checksum(struct rxkad_response *response)
783 {
784         u32 csum = 1000003;
785         int loop;
786         u8 *p = (u8 *) response;
787
788         for (loop = sizeof(*response); loop > 0; loop--)
789                 csum = csum * 0x10204081 + *p++;
790
791         response->encrypted.checksum = htonl(csum);
792 }
793
794 /*
795  * encrypt the response packet
796  */
797 static int rxkad_encrypt_response(struct rxrpc_connection *conn,
798                                   struct rxkad_response *resp,
799                                   const struct rxkad_key *s2)
800 {
801         struct skcipher_request *req;
802         struct rxrpc_crypt iv;
803         struct scatterlist sg[1];
804
805         req = skcipher_request_alloc(&conn->rxkad.cipher->base, GFP_NOFS);
806         if (!req)
807                 return -ENOMEM;
808
809         /* continue encrypting from where we left off */
810         memcpy(&iv, s2->session_key, sizeof(iv));
811
812         sg_init_table(sg, 1);
813         sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
814         skcipher_request_set_sync_tfm(req, conn->rxkad.cipher);
815         skcipher_request_set_callback(req, 0, NULL, NULL);
816         skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
817         crypto_skcipher_encrypt(req);
818         skcipher_request_free(req);
819         return 0;
820 }
821
822 /*
823  * respond to a challenge packet
824  */
825 static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
826                                       struct sk_buff *skb,
827                                       u32 *_abort_code)
828 {
829         const struct rxrpc_key_token *token;
830         struct rxkad_challenge challenge;
831         struct rxkad_response *resp;
832         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
833         const char *eproto;
834         u32 version, nonce, min_level, abort_code;
835         int ret;
836
837         _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
838
839         eproto = tracepoint_string("chall_no_key");
840         abort_code = RX_PROTOCOL_ERROR;
841         if (!conn->params.key)
842                 goto protocol_error;
843
844         abort_code = RXKADEXPIRED;
845         ret = key_validate(conn->params.key);
846         if (ret < 0)
847                 goto other_error;
848
849         eproto = tracepoint_string("chall_short");
850         abort_code = RXKADPACKETSHORT;
851         if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
852                           &challenge, sizeof(challenge)) < 0)
853                 goto protocol_error;
854
855         version = ntohl(challenge.version);
856         nonce = ntohl(challenge.nonce);
857         min_level = ntohl(challenge.min_level);
858
859         _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
860                sp->hdr.serial, version, nonce, min_level);
861
862         eproto = tracepoint_string("chall_ver");
863         abort_code = RXKADINCONSISTENCY;
864         if (version != RXKAD_VERSION)
865                 goto protocol_error;
866
867         abort_code = RXKADLEVELFAIL;
868         ret = -EACCES;
869         if (conn->params.security_level < min_level)
870                 goto other_error;
871
872         token = conn->params.key->payload.data[0];
873
874         /* build the response packet */
875         resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
876         if (!resp)
877                 return -ENOMEM;
878
879         resp->version                   = htonl(RXKAD_VERSION);
880         resp->encrypted.epoch           = htonl(conn->proto.epoch);
881         resp->encrypted.cid             = htonl(conn->proto.cid);
882         resp->encrypted.securityIndex   = htonl(conn->security_ix);
883         resp->encrypted.inc_nonce       = htonl(nonce + 1);
884         resp->encrypted.level           = htonl(conn->params.security_level);
885         resp->kvno                      = htonl(token->kad->kvno);
886         resp->ticket_len                = htonl(token->kad->ticket_len);
887         resp->encrypted.call_id[0]      = htonl(conn->channels[0].call_counter);
888         resp->encrypted.call_id[1]      = htonl(conn->channels[1].call_counter);
889         resp->encrypted.call_id[2]      = htonl(conn->channels[2].call_counter);
890         resp->encrypted.call_id[3]      = htonl(conn->channels[3].call_counter);
891
892         /* calculate the response checksum and then do the encryption */
893         rxkad_calc_response_checksum(resp);
894         ret = rxkad_encrypt_response(conn, resp, token->kad);
895         if (ret == 0)
896                 ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
897         kfree(resp);
898         return ret;
899
900 protocol_error:
901         trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
902         ret = -EPROTO;
903 other_error:
904         *_abort_code = abort_code;
905         return ret;
906 }
907
908 /*
909  * decrypt the kerberos IV ticket in the response
910  */
911 static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
912                                 struct key *server_key,
913                                 struct sk_buff *skb,
914                                 void *ticket, size_t ticket_len,
915                                 struct rxrpc_crypt *_session_key,
916                                 time64_t *_expiry,
917                                 u32 *_abort_code)
918 {
919         struct skcipher_request *req;
920         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
921         struct rxrpc_crypt iv, key;
922         struct scatterlist sg[1];
923         struct in_addr addr;
924         unsigned int life;
925         const char *eproto;
926         time64_t issue, now;
927         bool little_endian;
928         int ret;
929         u32 abort_code;
930         u8 *p, *q, *name, *end;
931
932         _enter("{%d},{%x}", conn->debug_id, key_serial(server_key));
933
934         *_expiry = 0;
935
936         ASSERT(server_key->payload.data[0] != NULL);
937         ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
938
939         memcpy(&iv, &server_key->payload.data[2], sizeof(iv));
940
941         ret = -ENOMEM;
942         req = skcipher_request_alloc(server_key->payload.data[0], GFP_NOFS);
943         if (!req)
944                 goto temporary_error;
945
946         sg_init_one(&sg[0], ticket, ticket_len);
947         skcipher_request_set_callback(req, 0, NULL, NULL);
948         skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
949         crypto_skcipher_decrypt(req);
950         skcipher_request_free(req);
951
952         p = ticket;
953         end = p + ticket_len;
954
955 #define Z(field)                                        \
956         ({                                              \
957                 u8 *__str = p;                          \
958                 eproto = tracepoint_string("rxkad_bad_"#field); \
959                 q = memchr(p, 0, end - p);              \
960                 if (!q || q - p > (field##_SZ))         \
961                         goto bad_ticket;                \
962                 for (; p < q; p++)                      \
963                         if (!isprint(*p))               \
964                                 goto bad_ticket;        \
965                 p++;                                    \
966                 __str;                                  \
967         })
968
969         /* extract the ticket flags */
970         _debug("KIV FLAGS: %x", *p);
971         little_endian = *p & 1;
972         p++;
973
974         /* extract the authentication name */
975         name = Z(ANAME);
976         _debug("KIV ANAME: %s", name);
977
978         /* extract the principal's instance */
979         name = Z(INST);
980         _debug("KIV INST : %s", name);
981
982         /* extract the principal's authentication domain */
983         name = Z(REALM);
984         _debug("KIV REALM: %s", name);
985
986         eproto = tracepoint_string("rxkad_bad_len");
987         if (end - p < 4 + 8 + 4 + 2)
988                 goto bad_ticket;
989
990         /* get the IPv4 address of the entity that requested the ticket */
991         memcpy(&addr, p, sizeof(addr));
992         p += 4;
993         _debug("KIV ADDR : %pI4", &addr);
994
995         /* get the session key from the ticket */
996         memcpy(&key, p, sizeof(key));
997         p += 8;
998         _debug("KIV KEY  : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
999         memcpy(_session_key, &key, sizeof(key));
1000
1001         /* get the ticket's lifetime */
1002         life = *p++ * 5 * 60;
1003         _debug("KIV LIFE : %u", life);
1004
1005         /* get the issue time of the ticket */
1006         if (little_endian) {
1007                 __le32 stamp;
1008                 memcpy(&stamp, p, 4);
1009                 issue = rxrpc_u32_to_time64(le32_to_cpu(stamp));
1010         } else {
1011                 __be32 stamp;
1012                 memcpy(&stamp, p, 4);
1013                 issue = rxrpc_u32_to_time64(be32_to_cpu(stamp));
1014         }
1015         p += 4;
1016         now = ktime_get_real_seconds();
1017         _debug("KIV ISSUE: %llx [%llx]", issue, now);
1018
1019         /* check the ticket is in date */
1020         if (issue > now) {
1021                 abort_code = RXKADNOAUTH;
1022                 ret = -EKEYREJECTED;
1023                 goto other_error;
1024         }
1025
1026         if (issue < now - life) {
1027                 abort_code = RXKADEXPIRED;
1028                 ret = -EKEYEXPIRED;
1029                 goto other_error;
1030         }
1031
1032         *_expiry = issue + life;
1033
1034         /* get the service name */
1035         name = Z(SNAME);
1036         _debug("KIV SNAME: %s", name);
1037
1038         /* get the service instance name */
1039         name = Z(INST);
1040         _debug("KIV SINST: %s", name);
1041         return 0;
1042
1043 bad_ticket:
1044         trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
1045         abort_code = RXKADBADTICKET;
1046         ret = -EPROTO;
1047 other_error:
1048         *_abort_code = abort_code;
1049         return ret;
1050 temporary_error:
1051         return ret;
1052 }
1053
1054 /*
1055  * decrypt the response packet
1056  */
1057 static void rxkad_decrypt_response(struct rxrpc_connection *conn,
1058                                    struct rxkad_response *resp,
1059                                    const struct rxrpc_crypt *session_key)
1060 {
1061         struct skcipher_request *req = rxkad_ci_req;
1062         struct scatterlist sg[1];
1063         struct rxrpc_crypt iv;
1064
1065         _enter(",,%08x%08x",
1066                ntohl(session_key->n[0]), ntohl(session_key->n[1]));
1067
1068         mutex_lock(&rxkad_ci_mutex);
1069         if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x,
1070                                         sizeof(*session_key)) < 0)
1071                 BUG();
1072
1073         memcpy(&iv, session_key, sizeof(iv));
1074
1075         sg_init_table(sg, 1);
1076         sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
1077         skcipher_request_set_sync_tfm(req, rxkad_ci);
1078         skcipher_request_set_callback(req, 0, NULL, NULL);
1079         skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
1080         crypto_skcipher_decrypt(req);
1081         skcipher_request_zero(req);
1082
1083         mutex_unlock(&rxkad_ci_mutex);
1084
1085         _leave("");
1086 }
1087
1088 /*
1089  * verify a response
1090  */
1091 static int rxkad_verify_response(struct rxrpc_connection *conn,
1092                                  struct sk_buff *skb,
1093                                  u32 *_abort_code)
1094 {
1095         struct rxkad_response *response;
1096         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1097         struct rxrpc_crypt session_key;
1098         struct key *server_key;
1099         const char *eproto;
1100         time64_t expiry;
1101         void *ticket;
1102         u32 abort_code, version, kvno, ticket_len, level;
1103         __be32 csum;
1104         int ret, i;
1105
1106         _enter("{%d}", conn->debug_id);
1107
1108         server_key = rxrpc_look_up_server_security(conn, skb, 0, 0);
1109         if (IS_ERR(server_key)) {
1110                 switch (PTR_ERR(server_key)) {
1111                 case -ENOKEY:
1112                         abort_code = RXKADUNKNOWNKEY;
1113                         break;
1114                 case -EKEYEXPIRED:
1115                         abort_code = RXKADEXPIRED;
1116                         break;
1117                 default:
1118                         abort_code = RXKADNOAUTH;
1119                         break;
1120                 }
1121                 trace_rxrpc_abort(0, "SVK",
1122                                   sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1123                                   abort_code, PTR_ERR(server_key));
1124                 *_abort_code = abort_code;
1125                 return -EPROTO;
1126         }
1127
1128         ret = -ENOMEM;
1129         response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
1130         if (!response)
1131                 goto temporary_error;
1132
1133         eproto = tracepoint_string("rxkad_rsp_short");
1134         abort_code = RXKADPACKETSHORT;
1135         if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1136                           response, sizeof(*response)) < 0)
1137                 goto protocol_error;
1138
1139         version = ntohl(response->version);
1140         ticket_len = ntohl(response->ticket_len);
1141         kvno = ntohl(response->kvno);
1142         _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
1143                sp->hdr.serial, version, kvno, ticket_len);
1144
1145         eproto = tracepoint_string("rxkad_rsp_ver");
1146         abort_code = RXKADINCONSISTENCY;
1147         if (version != RXKAD_VERSION)
1148                 goto protocol_error;
1149
1150         eproto = tracepoint_string("rxkad_rsp_tktlen");
1151         abort_code = RXKADTICKETLEN;
1152         if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
1153                 goto protocol_error;
1154
1155         eproto = tracepoint_string("rxkad_rsp_unkkey");
1156         abort_code = RXKADUNKNOWNKEY;
1157         if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
1158                 goto protocol_error;
1159
1160         /* extract the kerberos ticket and decrypt and decode it */
1161         ret = -ENOMEM;
1162         ticket = kmalloc(ticket_len, GFP_NOFS);
1163         if (!ticket)
1164                 goto temporary_error_free_resp;
1165
1166         eproto = tracepoint_string("rxkad_tkt_short");
1167         abort_code = RXKADPACKETSHORT;
1168         if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header) + sizeof(*response),
1169                           ticket, ticket_len) < 0)
1170
1171         ret = rxkad_decrypt_ticket(conn, server_key, skb, ticket, ticket_len,
1172                                    &session_key, &expiry, _abort_code);
1173         if (ret < 0)
1174                 goto temporary_error_free_ticket;
1175
1176         /* use the session key from inside the ticket to decrypt the
1177          * response */
1178         rxkad_decrypt_response(conn, response, &session_key);
1179
1180         eproto = tracepoint_string("rxkad_rsp_param");
1181         abort_code = RXKADSEALEDINCON;
1182         if (ntohl(response->encrypted.epoch) != conn->proto.epoch)
1183                 goto protocol_error_free;
1184         if (ntohl(response->encrypted.cid) != conn->proto.cid)
1185                 goto protocol_error_free;
1186         if (ntohl(response->encrypted.securityIndex) != conn->security_ix)
1187                 goto protocol_error_free;
1188         csum = response->encrypted.checksum;
1189         response->encrypted.checksum = 0;
1190         rxkad_calc_response_checksum(response);
1191         eproto = tracepoint_string("rxkad_rsp_csum");
1192         if (response->encrypted.checksum != csum)
1193                 goto protocol_error_free;
1194
1195         spin_lock(&conn->bundle->channel_lock);
1196         for (i = 0; i < RXRPC_MAXCALLS; i++) {
1197                 struct rxrpc_call *call;
1198                 u32 call_id = ntohl(response->encrypted.call_id[i]);
1199
1200                 eproto = tracepoint_string("rxkad_rsp_callid");
1201                 if (call_id > INT_MAX)
1202                         goto protocol_error_unlock;
1203
1204                 eproto = tracepoint_string("rxkad_rsp_callctr");
1205                 if (call_id < conn->channels[i].call_counter)
1206                         goto protocol_error_unlock;
1207
1208                 eproto = tracepoint_string("rxkad_rsp_callst");
1209                 if (call_id > conn->channels[i].call_counter) {
1210                         call = rcu_dereference_protected(
1211                                 conn->channels[i].call,
1212                                 lockdep_is_held(&conn->bundle->channel_lock));
1213                         if (call && call->state < RXRPC_CALL_COMPLETE)
1214                                 goto protocol_error_unlock;
1215                         conn->channels[i].call_counter = call_id;
1216                 }
1217         }
1218         spin_unlock(&conn->bundle->channel_lock);
1219
1220         eproto = tracepoint_string("rxkad_rsp_seq");
1221         abort_code = RXKADOUTOFSEQUENCE;
1222         if (ntohl(response->encrypted.inc_nonce) != conn->rxkad.nonce + 1)
1223                 goto protocol_error_free;
1224
1225         eproto = tracepoint_string("rxkad_rsp_level");
1226         abort_code = RXKADLEVELFAIL;
1227         level = ntohl(response->encrypted.level);
1228         if (level > RXRPC_SECURITY_ENCRYPT)
1229                 goto protocol_error_free;
1230         conn->params.security_level = level;
1231
1232         /* create a key to hold the security data and expiration time - after
1233          * this the connection security can be handled in exactly the same way
1234          * as for a client connection */
1235         ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
1236         if (ret < 0)
1237                 goto temporary_error_free_ticket;
1238
1239         kfree(ticket);
1240         kfree(response);
1241         _leave(" = 0");
1242         return 0;
1243
1244 protocol_error_unlock:
1245         spin_unlock(&conn->bundle->channel_lock);
1246 protocol_error_free:
1247         kfree(ticket);
1248 protocol_error:
1249         kfree(response);
1250         trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
1251         key_put(server_key);
1252         *_abort_code = abort_code;
1253         return -EPROTO;
1254
1255 temporary_error_free_ticket:
1256         kfree(ticket);
1257 temporary_error_free_resp:
1258         kfree(response);
1259 temporary_error:
1260         /* Ignore the response packet if we got a temporary error such as
1261          * ENOMEM.  We just want to send the challenge again.  Note that we
1262          * also come out this way if the ticket decryption fails.
1263          */
1264         key_put(server_key);
1265         return ret;
1266 }
1267
1268 /*
1269  * clear the connection security
1270  */
1271 static void rxkad_clear(struct rxrpc_connection *conn)
1272 {
1273         _enter("");
1274
1275         if (conn->rxkad.cipher)
1276                 crypto_free_sync_skcipher(conn->rxkad.cipher);
1277 }
1278
1279 /*
1280  * Initialise the rxkad security service.
1281  */
1282 static int rxkad_init(void)
1283 {
1284         struct crypto_sync_skcipher *tfm;
1285         struct skcipher_request *req;
1286
1287         /* pin the cipher we need so that the crypto layer doesn't invoke
1288          * keventd to go get it */
1289         tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
1290         if (IS_ERR(tfm))
1291                 return PTR_ERR(tfm);
1292
1293         req = skcipher_request_alloc(&tfm->base, GFP_KERNEL);
1294         if (!req)
1295                 goto nomem_tfm;
1296
1297         rxkad_ci_req = req;
1298         rxkad_ci = tfm;
1299         return 0;
1300
1301 nomem_tfm:
1302         crypto_free_sync_skcipher(tfm);
1303         return -ENOMEM;
1304 }
1305
1306 /*
1307  * Clean up the rxkad security service.
1308  */
1309 static void rxkad_exit(void)
1310 {
1311         crypto_free_sync_skcipher(rxkad_ci);
1312         skcipher_request_free(rxkad_ci_req);
1313 }
1314
1315 /*
1316  * RxRPC Kerberos-based security
1317  */
1318 const struct rxrpc_security rxkad = {
1319         .name                           = "rxkad",
1320         .security_index                 = RXRPC_SECURITY_RXKAD,
1321         .no_key_abort                   = RXKADUNKNOWNKEY,
1322         .init                           = rxkad_init,
1323         .exit                           = rxkad_exit,
1324         .preparse_server_key            = rxkad_preparse_server_key,
1325         .free_preparse_server_key       = rxkad_free_preparse_server_key,
1326         .destroy_server_key             = rxkad_destroy_server_key,
1327         .init_connection_security       = rxkad_init_connection_security,
1328         .how_much_data                  = rxkad_how_much_data,
1329         .secure_packet                  = rxkad_secure_packet,
1330         .verify_packet                  = rxkad_verify_packet,
1331         .free_call_crypto               = rxkad_free_call_crypto,
1332         .issue_challenge                = rxkad_issue_challenge,
1333         .respond_to_challenge           = rxkad_respond_to_challenge,
1334         .verify_response                = rxkad_verify_response,
1335         .clear                          = rxkad_clear,
1336 };
This page took 0.108462 seconds and 4 git commands to generate.