forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_hooks.c
More file actions
1975 lines (1753 loc) · 68.6 KB
/
module_hooks.c
File metadata and controls
1975 lines (1753 loc) · 68.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* module_hooks.c -- module load/unload hooks for libwolfssl.ko
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#define WOLFSSL_LINUXKM_NEED_LINUX_CURRENT
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifndef WOLFSSL_LICENSE
#define WOLFSSL_LICENSE "GPL"
#endif
#ifdef WOLFCRYPT_ONLY
#include <wolfssl/version.h>
#else
#include <wolfssl/ssl.h>
#endif
#ifdef HAVE_FIPS
#ifdef USE_CONTESTMUTEX
#error USE_CONTESTMUTEX is incompatible with WOLFSSL_LINUXKM
#endif
#ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
#include <wolfssl/wolfcrypt/hmac.h>
#endif
#include <wolfssl/wolfcrypt/fips_test.h>
#endif
#if !defined(NO_CRYPT_TEST) || defined(LINUXKM_LKCAPI_REGISTER)
#include <wolfcrypt/test/test.h>
#endif
#ifdef HAVE_ENTROPY_MEMUSE
#include <wolfssl/wolfcrypt/wolfentropy.h>
#endif
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/sha256.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES
enum linux_errcodes {
my_EINVAL = EINVAL,
my_ENOMEM = ENOMEM,
my_EBADMSG = EBADMSG
};
#undef EINVAL
#undef ENOMEM
#undef EBADMSG
#define EINVAL WC_ERR_TRACE(my_EINVAL)
#define ENOMEM WC_ERR_TRACE(my_ENOMEM)
#define EBADMSG WC_ERR_TRACE(my_EBADMSG)
#endif
static int libwolfssl_cleanup(void) {
int ret;
#ifdef WOLFCRYPT_ONLY
ret = wolfCrypt_Cleanup();
if (ret != 0)
pr_err("ERROR: wolfCrypt_Cleanup() failed: %s\n", wc_GetErrorString(ret));
else
pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
#else
ret = wolfSSL_Cleanup();
if (ret != WOLFSSL_SUCCESS)
pr_err("ERROR: wolfSSL_Cleanup() failed: %s\n", wc_GetErrorString(ret));
else
pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
#endif
return ret;
}
#ifdef HAVE_FIPS
/* failsafe definitions for FIPS <5.3 */
#ifndef FIPS_IN_CORE_DIGEST_SIZE
#ifndef NO_SHA256
#define FIPS_IN_CORE_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
#define FIPS_IN_CORE_HASH_TYPE WC_SHA256
#elif defined(WOLFSSL_SHA384)
#define FIPS_IN_CORE_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
#define FIPS_IN_CORE_HASH_TYPE WC_SHA384
#else
#error Unsupported FIPS hash alg.
#endif
#endif
#ifndef FIPS_IN_CORE_KEY_SZ
#define FIPS_IN_CORE_KEY_SZ FIPS_IN_CORE_DIGEST_SIZE
#endif
#ifndef FIPS_IN_CORE_VERIFY_SZ
#define FIPS_IN_CORE_VERIFY_SZ FIPS_IN_CORE_DIGEST_SIZE
#endif
#if FIPS_VERSION3_GE(6,0,0) || defined(WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE)
extern char verifyCore[FIPS_IN_CORE_DIGEST_SIZE*2 + 1];
#endif
#endif
#ifdef WC_SYM_RELOC_TABLES
#ifdef DEBUG_LINUXKM_PIE_SUPPORT
/* cheap portable ad-hoc hash function to confirm bitwise stability of the PIE
* binary image.
*/
static unsigned int hash_span(const u8 *start, const u8 *end, unsigned int sum) {
WC_SANITIZE_DISABLE();
while (start < end) {
unsigned int rotate_by;
sum ^= *start++;
rotate_by = (sum ^ (sum >> 5)) & 31;
sum = (sum << rotate_by) | (sum >> (32 - rotate_by));
}
WC_SANITIZE_ENABLE();
return sum;
}
struct wc_reloc_counts reloc_counts = {};
#endif /* DEBUG_LINUXKM_PIE_SUPPORT */
static int set_up_wolfssl_linuxkm_pie_redirect_table(void);
#ifdef HAVE_FIPS
extern const unsigned int wolfCrypt_FIPS_ro_start[];
extern const unsigned int wolfCrypt_FIPS_ro_end[];
#endif
#endif /* WC_SYM_RELOC_TABLES */
#ifdef HAVE_FIPS
static void lkmFipsCb(int ok, int err, const char* hash)
{
if ((! ok) || (err != 0))
pr_err("ERROR: libwolfssl FIPS error: %s\n", wc_GetErrorString(err));
if (err == WC_NO_ERR_TRACE(IN_CORE_FIPS_E)) {
if (hash) {
pr_err("In-core integrity hash check failure.\n"
"Update FIPS hash with \"make module-update-fips-hash FIPS_HASH=%s\".\n",
hash);
}
else {
pr_err("In-core integrity hash check failure.\n");
pr_err("ERROR: could not compute new hash. Contact customer support.\n");
}
}
}
#ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
static int updateFipsHash(void);
#endif
#endif /* HAVE_FIPS */
#ifdef WOLFSSL_LINUXKM_BENCHMARKS
extern int wolfcrypt_benchmark_main(int argc, char** argv);
#endif /* WOLFSSL_LINUXKM_BENCHMARKS */
#ifndef WOLFSSL_LINUXKM_USE_MUTEXES
int wc_lkm_LockMutex(wolfSSL_Mutex* m)
{
unsigned long irq_flags;
/* first, try the cheap way. */
if (spin_trylock_irqsave(&m->lock, irq_flags)) {
m->irq_flags = irq_flags;
return 0;
}
if (irq_count() != 0) {
/* Note, this catches calls while SAVE_VECTOR_REGISTERS()ed as
* required, because in_softirq() is always true while saved,
* even for WC_FPU_INHIBITED_FLAG contexts.
*/
spin_lock_irqsave(&m->lock, irq_flags);
m->irq_flags = irq_flags;
return 0;
}
else {
for (;;) {
int sig_ret = wc_linuxkm_check_for_intr_signals();
if (sig_ret)
return sig_ret;
cond_resched();
if (spin_trylock_irqsave(&m->lock, irq_flags)) {
m->irq_flags = irq_flags;
return 0;
}
}
}
__builtin_unreachable();
}
#endif
WC_MAYBE_UNUSED static int linuxkm_lkcapi_sysfs_install_node(struct kobj_attribute *node, int *installed_flag)
{
if ((installed_flag == NULL) || (! *installed_flag)) {
int ret = sysfs_create_file(&THIS_MODULE->mkobj.kobj, &node->attr);
if (ret) {
pr_err("ERROR: sysfs_create_file failed for %s: %d\n", node->attr.name, ret);
return ret;
}
if (installed_flag)
*installed_flag = 1;
}
return 0;
}
WC_MAYBE_UNUSED static int linuxkm_lkcapi_sysfs_deinstall_node(struct kobj_attribute *node, int *installed_flag)
{
if ((installed_flag == NULL) || *installed_flag) {
sysfs_remove_file(&THIS_MODULE->mkobj.kobj, &node->attr);
if (installed_flag)
*installed_flag = 0;
}
return 0;
}
#ifdef WC_LINUXKM_SUPPORT_DUMP_TO_FILE
static ssize_t dump_to_file(const char *path, const u8 *buf, size_t buf_len)
{
loff_t pos = 0;
struct file *fp;
ssize_t ret;
char tmp;
if (buf_len == 0) {
pr_info("libwolfssl: dump_to_file() called with buf_len == 0. Not dumping.\n");
return 0;
}
WC_SANITIZE_DISABLE();
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,8,0)
ret = probe_kernel_read(&tmp, buf, 1);
if (ret == 0)
ret = probe_kernel_read(&tmp, buf + buf_len - 1, 1);
#else
ret = copy_from_kernel_nofault(&tmp, buf, 1);
if (ret == 0)
ret = copy_from_kernel_nofault(&tmp, buf + buf_len - 1, 1);
#endif
WC_SANITIZE_ENABLE();
if (ret != 0) {
pr_err("libwolfssl: cannot safely read from buffer %px: %d\n", buf, (int)ret);
return ret;
}
fp = filp_open(path, O_WRONLY | O_CREAT, 0644);
if (IS_ERR(fp)) {
pr_err("libwolfssl: cannot open %s: %ld\n", path, PTR_ERR(fp));
return PTR_ERR(fp);
}
WC_SANITIZE_DISABLE();
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
/* kernel_write() fixed by e13ec939e9 */
ret = kernel_write(fp, buf, buf_len, &pos);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
/* kernel_write() exported by 7bb307e894d51 */
ret = kernel_write(fp, (char *)buf, buf_len, pos);
#else
ret = vfs_write(fp, buf, buf_len, &pos);
#endif
WC_SANITIZE_ENABLE();
filp_close(fp, NULL);
if (ret < 0)
pr_err("libwolfssl: write to %s failed: %zd\n", path, ret);
else if ((size_t)ret != buf_len)
pr_warn("libwolfssl: short write to %s: %zd of %zu bytes\n", path, ret, buf_len);
return ret;
}
static char *text_dump_path;
static char *rodata_dump_path;
/* indent these so they don't look like flush-left function calls. */
module_param(text_dump_path, charp, 0444);
module_param(rodata_dump_path, charp, 0444);
MODULE_PARM_DESC(text_dump_path,
"Path to dump live .wolfcrypt_text section to (e.g. /tmp/wc_text.bin)");
MODULE_PARM_DESC(rodata_dump_path,
"Path to dump live .wolfcrypt_rodata section to (e.g. /tmp/wc_rodata.bin)");
#endif /* WC_LINUXKM_SUPPORT_DUMP_TO_FILE */
#ifdef HAVE_FIPS
static ssize_t FIPS_rerun_self_test_handler(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count);
static struct kobj_attribute FIPS_rerun_self_test_attr = __ATTR(FIPS_rerun_self_test, 0220, NULL, FIPS_rerun_self_test_handler);
static int installed_sysfs_FIPS_files = 0;
#endif
#ifdef LINUXKM_LKCAPI_REGISTER
#include "linuxkm/lkcapi_glue.c"
#endif
/* for simplicity, we use a global count to suspend signal processing while any
* thread is running fipsEntry(), wolfCrypt_IntegrityTest_fips(),
* linuxkm_lkcapi_register(), or linuxkm_lkcapi_unregister(). This only affects
* startup dynamics and the FIPS runtime. Once the uninterruptible routine
* completes, signal handling resumes, and any still-pending signal on
* continuing threads will be processed in a timely fashion.
*/
static wolfSSL_Atomic_Int wc_linuxkm_sig_ignore_count = WOLFSSL_ATOMIC_INITIALIZER(0);
int wc_linuxkm_sig_ignore_begin(void) {
return wolfSSL_Atomic_Int_AddFetch(&wc_linuxkm_sig_ignore_count, 1);
}
int wc_linuxkm_sig_ignore_end(void) {
return wolfSSL_Atomic_Int_SubFetch(&wc_linuxkm_sig_ignore_count, 1);
}
int wc_linuxkm_check_for_intr_signals(void) {
static const int intr_signals[] = WC_LINUXKM_INTR_SIGNALS;
if (preempt_count() != 0)
return 0;
if (signal_pending(current)) {
int i;
for (i = 0;
i < (int)sizeof(intr_signals) / (int)sizeof(intr_signals[0]);
++i)
{
if (sigismember(¤t->pending.signal, intr_signals[i])) {
if (WOLFSSL_ATOMIC_LOAD(wc_linuxkm_sig_ignore_count) > 0) {
#ifdef WOLFSSL_LINUXKM_VERBOSE_DEBUG
pr_info("INFO: wc_linuxkm_check_for_intr_signals ignoring "
"signal %d\n", intr_signals[i]);
#endif
return 0;
}
#ifdef WOLFSSL_LINUXKM_VERBOSE_DEBUG
pr_info("INFO: wc_linuxkm_check_for_intr_signals returning "
"INTERRUPTED_E on signal %d\n", intr_signals[i]);
#endif
return INTERRUPTED_E;
}
}
}
return 0;
}
void wc_linuxkm_relax_long_loop(void) {
#if WC_LINUXKM_MAX_NS_WITHOUT_YIELD >= 0
if (preempt_count() == 0) {
#if (WC_LINUXKM_MAX_NS_WITHOUT_YIELD == 0) || !defined(CONFIG_SCHED_INFO)
cond_resched();
#else
/* note that local_clock() wraps a local_clock_noinstr() in a
* preempt_disable_notrace(), which sounds expensive but isn't --
* preempt_disable_notrace() is actually just a nonlocking integer
* increment of current_thread_info()->preempt.count, protected only by
* various compiler optimizer barriers.
*/
u64 now = local_clock();
u64 current_last_arrival = current->sched_info.last_arrival;
s64 delta = (s64)(now - current_last_arrival);
if (delta > WC_LINUXKM_MAX_NS_WITHOUT_YIELD) {
cond_resched();
/* if nothing else is runnable, cond_resched() is a no-op and
* doesn't even update .last_arrival. we could force update by
* sleeping, but there's no need. we've been nice enough by just
* cond_resched()ing, and it's actually preferable to call
* cond_resched() frequently once computation has looped
* continuously for longer than WC_LINUXKM_MAX_NS_WITHOUT_YIELD.
*/
}
#endif
return;
}
#endif
cpu_relax();
}
#if defined(WC_LINUXKM_WOLFENTROPY_IN_GLUE_LAYER)
/* When building without the wolfentropy source (HAVE_ENTROPY_MEMUSE not set),
* wc_Entropy_Get and MAX_ENTROPY_BITS are not declared via the normal header
* chain. Provide the declarations here at file scope so the compiler sees
* them before the call below.
*/
#ifndef HAVE_ENTROPY_MEMUSE
#ifndef MAX_ENTROPY_BITS
#define MAX_ENTROPY_BITS 256
#endif
extern int wc_Entropy_Get(int bits, unsigned char *entropy, word32 len) __attribute__((weak));
#endif /* !HAVE_ENTROPY_MEMUSE */
int wc_linuxkm_GenerateSeed_wolfEntropy(OS_Seed* os, byte* output, word32 sz)
{
(void)os;
#ifndef HAVE_ENTROPY_MEMUSE
if (!wc_Entropy_Get) {
pr_err("wolfentropy: wc_Entropy_Get unavailable -- is wolfentropy.ko loaded?\n");
return -ENODEV;
}
#endif
return wc_Entropy_Get(MAX_ENTROPY_BITS, output, sz);
}
#elif defined(WC_LINUXKM_RDSEED_IN_GLUE_LAYER)
/* backported wc_GenerateSeed_IntelRD() for FIPS v5, before breakout of wolfentropy.c. */
#include <wolfssl/wolfcrypt/cpuid.h>
#include <wolfssl/wolfcrypt/random.h>
static cpuid_flags_t intel_flags = WC_CPUID_INITIALIZER;
static inline void wc_InitRng_IntelRD(void)
{
cpuid_get_flags_ex(&intel_flags);
}
#define INTELRD_RETRY 32
static WC_INLINE int IntelRDseed64(word64* seed)
{
unsigned char ok;
__asm__ volatile("rdseed %0; setc %1":"=r"(*seed), "=qm"(ok));
return (ok) ? 0 : -1;
}
/* return 0 on success */
static WC_INLINE int IntelRDseed64_r(word64* rnd)
{
int iters, retry_counter;
word64 buf;
#if defined(HAVE_AMD_RDSEED)
/* See "AMD RNG ESV Public Use Document". Version 0.7 of October 24,
* 2024 specifies 0.656 to 1.312 bits of entropy per 128 bit block of
* RDSEED output, depending on CPU family.
*
* FIPS v5 random.c sets ENTROPY_SCALE_FACTOR to 1 for
* HAVE_INTEL_RDSEED.
*/
iters = 128;
#elif defined(HAVE_INTEL_RDSEED)
/* The value of 2 applies to Intel's RDSEED which provides about
* 0.5 bits minimum of entropy per bit. The value of 4 gives a
* conservative margin for FIPS.
*
* FIPS v5 random.c sets ENTROPY_SCALE_FACTOR to 2 for
* HAVE_INTEL_RDSEED.
*/
iters = 2;
#else
#error WC_LINUXKM_RDSEED_IN_GLUE_LAYER requires HAVE_INTEL_RDSEED or HAVE_AMD_RDSEED
#endif
while (--iters >= 0) {
for (retry_counter = 0; retry_counter < INTELRD_RETRY; retry_counter++) {
if (IntelRDseed64(&buf) == 0)
break;
}
if (retry_counter == INTELRD_RETRY)
return -1;
WC_SANITIZE_DISABLE();
*rnd ^= buf; /* deliberately retain any garbage passed in the dest buffer. */
WC_SANITIZE_ENABLE();
buf = 0;
}
return 0;
}
/* return 0 on success */
int wc_linuxkm_GenerateSeed_IntelRD(struct OS_Seed* os, byte* output, word32 sz)
{
int ret;
word64 rndTmp;
(void)os;
wc_InitRng_IntelRD();
if (!IS_INTEL_RDSEED(intel_flags)) {
static wolfSSL_Atomic_Int warned_on_missing_RDSEED = WOLFSSL_ATOMIC_INITIALIZER(0);
int expected_warned_on_missing_RDSEED = 0;
if (wolfSSL_Atomic_Int_CompareExchange(
&warned_on_missing_RDSEED, &expected_warned_on_missing_RDSEED, 1))
{
pr_err("ERROR: wc_linuxkm_GenerateSeed_IntelRD() called on CPU without RDSEED support.\n");
}
return -1;
}
for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64),
output += sizeof(word64)) {
ret = IntelRDseed64_r((word64*)output);
if (ret != 0) {
#ifdef WOLFSSL_LINUXKM_VERBOSE_DEBUG
pr_err("ERROR: IntelRDseed64_r() returned code %d.\n", ret);
#endif
return ret;
}
}
if (sz == 0)
return 0;
/* handle unaligned remainder */
ret = IntelRDseed64_r(&rndTmp);
if (ret != 0) {
#ifdef WOLFSSL_LINUXKM_VERBOSE_DEBUG
pr_err("ERROR: IntelRDseed64_r() returned code %d.\n", ret);
#endif
return ret;
}
XMEMCPY(output, &rndTmp, sz);
wc_ForceZero(&rndTmp, sizeof(rndTmp));
return 0;
}
#endif /* WC_LINUXKM_RDSEED_IN_GLUE_LAYER */
#if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && defined(CONFIG_X86)
#include "linuxkm/x86_vector_register_glue.c"
#endif
#ifdef CONFIG_HAVE_KPROBES
static WC_MAYBE_UNUSED void *my_kallsyms_lookup_name(const char *name);
#endif
#ifdef FIPS_OPTEST
#ifndef HAVE_FIPS
#error FIPS_OPTEST requires HAVE_FIPS.
#endif
#ifdef LINUXKM_LKCAPI_REGISTER
#error FIPS_OPTEST is not allowed with LINUXKM_LKCAPI_REGISTER.
#endif
extern int linuxkm_op_test_1(int argc, const char* argv[]);
extern int linuxkm_op_test_wrapper(void);
static wolfSSL_Atomic_Int *conTestFailure_ptr = NULL;
#ifdef HAVE_WC_FIPS_OPTEST_CONTESTFAILURE_EXPORT
WOLFSSL_API extern wolfSSL_Atomic_Int wc_fips_optest_conTestFailure;
#endif
static ssize_t FIPS_optest_trig_handler(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count);
static struct kobj_attribute FIPS_optest_trig_attr = __ATTR(FIPS_optest_run_code, 0220, NULL, FIPS_optest_trig_handler);
static int installed_sysfs_FIPS_optest_trig_files = 0;
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
static int __init wolfssl_init(void)
#else
static int wolfssl_init(void)
#endif
{
int ret;
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
{
unsigned long stack_usage;
stack_usage = wc_linuxkm_stack_current();
pr_info("STACK INFO: usage at entry to wolfssl_init(): %lu of %lu total\n", stack_usage, THREAD_SIZE);
wc_linuxkm_stack_hwm_prepare(0xee);
}
#endif
#ifdef WC_SYM_RELOC_TABLES
ret = set_up_wolfssl_linuxkm_pie_redirect_table();
if (ret < 0)
return ret;
#endif
#ifdef WC_LINUXKM_SUPPORT_DUMP_TO_FILE
#ifdef WC_SYM_RELOC_TABLES
if (text_dump_path) {
if (dump_to_file(text_dump_path,
(u8 *)__wc_text_start,
(size_t)((uintptr_t)__wc_text_end - (uintptr_t)__wc_text_start))
> 0)
{
pr_info("libwolfssl: dumped .wolfcrypt_text (%zu bytes) to %s.\n",
(size_t)((uintptr_t)__wc_text_end - (uintptr_t)__wc_text_start),
text_dump_path);
}
}
if (rodata_dump_path) {
if (dump_to_file(rodata_dump_path,
(u8 *)__wc_rodata_start,
(size_t)((uintptr_t)__wc_rodata_end - (uintptr_t)__wc_rodata_start))
> 0)
{
pr_info("libwolfssl: dumped .wolfcrypt_rodata (%zu bytes) to %s.\n",
(size_t)((uintptr_t)__wc_rodata_end - (uintptr_t)__wc_rodata_start),
rodata_dump_path);
}
}
#else
if ((text_dump_path != NULL) ||
(rodata_dump_path != NULL))
{
pr_info("libwolfssl: ignoring module dump path argument(s) -- module lacks WC_SYM_RELOC_TABLES.\n");
}
#endif
#endif /* WC_LINUXKM_SUPPORT_DUMP_TO_FILE */
#ifdef WC_LINUXKM_TEST_INET_PTON
{
const char *src;
byte dst[16] = { };
int pton_ret;
src = "1.2.3.4";
pton_ret = wc_linuxkm_inet_pton(AF_INET, src, dst);
printf("pton_ret=%d src=%s dst=%d.%d.%d.%d\n", pton_ret, src, (int)dst[0], (int)dst[1], (int)dst[2], (int)dst[3]);
src = "89ab::cdef";
pton_ret = wc_linuxkm_inet_pton(AF_INET6, src, dst);
printf("pton_ret=%d src=%s dst=%02x%02x::%02x%02x\n", pton_ret, src, (int)dst[0], (int)dst[1], (int)dst[14], (int)dst[15]);
}
#endif /* WC_LINUXKM_TEST_INET_PTON */
#ifdef HAVE_FIPS
/* The compiled-in verifycore must be the right length, else the module
* geometry will change when the correct value is passed in, destabilizing
* wc_linuxkm_pie_reloc_tab. It also must be the right length for the
* module-update-fips-hash recipe (in-place overwrite) to work, and for
* updateFipsHash() (WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE) to be safe from
* overruns.
*/
{
size_t verifyCore_len;
#if FIPS_VERSION3_GE(6,0,0) || defined(WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE)
verifyCore_len = strlen(verifyCore);
#else
#ifdef CONFIG_HAVE_KPROBES
char *verifyCore_ptr = my_kallsyms_lookup_name("verifyCore");
if (verifyCore_ptr)
verifyCore_len = strlen(verifyCore_ptr);
else
#endif /* CONFIG_HAVE_KPROBES */
{
/* can't check -- have to assume. */
#if defined(CONFIG_HAVE_KPROBES) && (defined(DEBUG_LINUXKM_PIE_SUPPORT) || defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG))
pr_err("INFO: couldn't get verifyCore_ptr -- skipping verifyCore length check.\n");
#endif
verifyCore_len = FIPS_IN_CORE_DIGEST_SIZE*2;
}
#endif
if (verifyCore_len != FIPS_IN_CORE_DIGEST_SIZE*2) {
pr_err("ERROR: compile-time FIPS hash is the wrong length (expected %d hex digits, got %zu).\n", FIPS_IN_CORE_DIGEST_SIZE*2, verifyCore_len);
return -ECANCELED;
}
}
#ifdef WC_SYM_RELOC_TABLES
if (((uintptr_t)__wc_text_start > (uintptr_t)wolfCrypt_FIPS_first) ||
((uintptr_t)__wc_text_end < (uintptr_t)wolfCrypt_FIPS_last) ||
((uintptr_t)__wc_rodata_start > (uintptr_t)wolfCrypt_FIPS_ro_start) ||
((uintptr_t)__wc_rodata_end < (uintptr_t)wolfCrypt_FIPS_ro_end))
{
pr_err("ERROR: ELF segment fenceposts and FIPS fenceposts conflict.\n");
return -ECANCELED;
}
#endif
#ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
#ifdef CONFIG_MODULE_SIG
if (THIS_MODULE->sig_ok == false) {
pr_err("ERROR: wolfSSL module load aborted -- bad or missing module signature with FIPS dynamic hash.\n");
return -ECANCELED;
}
#endif
#if defined(WC_SYM_RELOC_TABLES) && defined(DEBUG_LINUXKM_PIE_SUPPORT)
reloc_counts.text = reloc_counts.rodata = reloc_counts.rwdata = reloc_counts.bss =
reloc_counts.other = 0;
#endif
ret = updateFipsHash();
if (ret < 0) {
pr_err("ERROR: wolfSSL module load aborted -- updateFipsHash: %s\n",wc_GetErrorString(ret));
return -ECANCELED;
}
#if defined(WC_SYM_RELOC_TABLES) && defined(DEBUG_LINUXKM_PIE_SUPPORT)
pr_info("FIPS-bounded relocation normalizations from updateFipsHash(): text=%d, rodata=%d, rwdata=%d, bss=%d, other=%d\n",
reloc_counts.text, reloc_counts.rodata, reloc_counts.rwdata, reloc_counts.bss, reloc_counts.other);
#endif
#endif /* WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE */
#endif /* HAVE_FIPS */
#if defined(WC_SYM_RELOC_TABLES) && defined(DEBUG_LINUXKM_PIE_SUPPORT)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
/* see linux commit ac3b432839 */
#define THIS_MODULE_TEXT_BASE (THIS_MODULE->mem[MOD_TEXT].base)
#define THIS_MODULE_TEXT_SIZE (THIS_MODULE->mem[MOD_TEXT].size)
#define THIS_MODULE_RO_BASE (THIS_MODULE->mem[MOD_RODATA].base)
#define THIS_MODULE_RO_SIZE (THIS_MODULE->mem[MOD_RODATA].size)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
#define THIS_MODULE_TEXT_BASE (THIS_MODULE->core_layout.base)
#define THIS_MODULE_TEXT_SIZE (THIS_MODULE->core_layout.text_size)
#define THIS_MODULE_RO_BASE ((char *)THIS_MODULE->core_layout.base + THIS_MODULE->core_layout.text_size)
#define THIS_MODULE_RO_SIZE (THIS_MODULE->core_layout.ro_size)
#else
#define THIS_MODULE_TEXT_BASE (THIS_MODULE->module_core)
#define THIS_MODULE_TEXT_SIZE (THIS_MODULE->core_text_size)
#define THIS_MODULE_RO_BASE ((char *)THIS_MODULE->module_core + THIS_MODULE->core_ro_size)
#define THIS_MODULE_RO_SIZE (THIS_MODULE->core_ro_size)
#endif
{
unsigned int text_hash = hash_span((const u8 *)__wc_text_start, (const u8 *)__wc_text_end, 1);
unsigned int rodata_hash = hash_span((const u8 *)__wc_rodata_start, (const u8 *)__wc_rodata_end, 1);
u8 *canon_buf = malloc(WOLFSSL_TEXT_SEGMENT_CANONICALIZER_BUFSIZ);
ssize_t cur_reloc_index = -1;
const u8 *text_p = (const u8 *)__wc_text_start;
unsigned int stabilized_text_hash = 1;
if (! canon_buf) {
pr_err("ERROR: malloc(%d) for WOLFSSL_TEXT_SEGMENT_CANONICALIZER failed: %ld.\n", WOLFSSL_TEXT_SEGMENT_CANONICALIZER_BUFSIZ, PTR_ERR(canon_buf));
return -ECANCELED;
}
reloc_counts.text = reloc_counts.rodata = reloc_counts.rwdata = reloc_counts.bss =
reloc_counts.other = 0;
while (text_p < (const u8 *)__wc_text_end) {
ssize_t progress =
WOLFSSL_TEXT_SEGMENT_CANONICALIZER(
text_p,
min(WOLFSSL_TEXT_SEGMENT_CANONICALIZER_BUFSIZ,
(word32)((const u8 *)__wc_text_end - text_p)),
canon_buf, &cur_reloc_index);
if (progress <= 0) {
pr_err("ERROR: progress=%ld from WOLFSSL_TEXT_SEGMENT_CANONICALIZER() at offset %x (text=%x-%x).\n",
(long)progress,
(unsigned)(uintptr_t)text_p,
(unsigned)(uintptr_t)__wc_text_start,
(unsigned)(uintptr_t)__wc_text_end);
free(canon_buf);
return -ECANCELED;
}
stabilized_text_hash = hash_span(canon_buf, canon_buf + progress, stabilized_text_hash);
text_p += progress;
}
free(canon_buf);
canon_buf = 0;
/* note, "%pK" conceals the actual layout information. "%px" exposes
* the true module start address, which is potentially useful to an
* attacker.
*/
pr_info("wolfCrypt segment hashes (spans): text 0x%x (%llu), rodata 0x%x (%llu), offset %c0x%llx, canon text 0x%x\n",
text_hash, (unsigned long long)((uintptr_t)__wc_text_end - (uintptr_t)__wc_text_start),
rodata_hash, (unsigned long long)((uintptr_t)__wc_rodata_end - (uintptr_t)__wc_rodata_start),
(uintptr_t)__wc_text_start < (uintptr_t)&__wc_rodata_start[0] ? '+' : '-',
(uintptr_t)__wc_text_start < (uintptr_t)&__wc_rodata_start[0] ? (unsigned long long)((uintptr_t)&__wc_rodata_start[0] - (uintptr_t)__wc_text_start) : (unsigned long long)((uintptr_t)__wc_text_start - (uintptr_t)&__wc_rodata_start[0]),
stabilized_text_hash);
pr_info("wolfCrypt segments: text=%llx-%llx, rodata=%llx-%llx, "
"rwdata=%llx-%llx, bss=%llx-%llx\n",
(unsigned long long)(uintptr_t)__wc_text_start,
(unsigned long long)(uintptr_t)__wc_text_end,
(unsigned long long)(uintptr_t)__wc_rodata_start,
(unsigned long long)(uintptr_t)__wc_rodata_end,
(unsigned long long)(uintptr_t)__wc_rwdata_start,
(unsigned long long)(uintptr_t)__wc_rwdata_end,
(unsigned long long)(uintptr_t)__wc_bss_start,
(unsigned long long)(uintptr_t)__wc_bss_end);
pr_info("whole-segment relocation normalizations: text=%d, rodata=%d, rwdata=%d, bss=%d, other=%d\n",
reloc_counts.text, reloc_counts.rodata, reloc_counts.rwdata, reloc_counts.bss, reloc_counts.other);
}
#endif /* WC_SYM_RELOC_TABLES && DEBUG_LINUXKM_PIE_SUPPORT */
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
{
unsigned long stack_usage;
stack_usage = wc_linuxkm_stack_hwm_measure_rel(0xee);
pr_info("STACK INFO: rel usage by wolfssl_init() initial setup: %lu\n", stack_usage);
/* shush up false stack HWM reading by kernel: */
wc_linuxkm_stack_hwm_prepare(0);
}
#endif
#ifdef HAVE_FIPS
ret = wolfCrypt_SetCb_fips(lkmFipsCb);
if (ret != 0) {
pr_err("ERROR: wolfCrypt_SetCb_fips() failed: %s\n", wc_GetErrorString(ret));
return -ECANCELED;
}
#if defined(WC_SYM_RELOC_TABLES) && defined(DEBUG_LINUXKM_PIE_SUPPORT)
reloc_counts.text = reloc_counts.rodata = reloc_counts.rwdata = reloc_counts.bss =
reloc_counts.other = 0;
#endif
if (WC_SIG_IGNORE_BEGIN() >= 0) {
fipsEntry();
(void)WC_SIG_IGNORE_END();
}
else
pr_err("ERROR: WC_SIG_IGNORE_BEGIN() failed.\n");
#if defined(WC_SYM_RELOC_TABLES) && defined(DEBUG_LINUXKM_PIE_SUPPORT)
pr_info("FIPS-bounded relocation normalizations: text=%d, rodata=%d, rwdata=%d, bss=%d, other=%d\n",
reloc_counts.text, reloc_counts.rodata, reloc_counts.rwdata, reloc_counts.bss, reloc_counts.other);
#endif
ret = wolfCrypt_GetStatus_fips();
if (ret != 0) {
pr_err("ERROR: wolfCrypt_GetStatus_fips() failed with code %d: %s\n", ret, wc_GetErrorString(ret));
if (ret == WC_NO_ERR_TRACE(IN_CORE_FIPS_E)) {
const char *newhash = wolfCrypt_GetCoreHash_fips();
if (newhash) {
pr_err("In-core integrity hash check failure.\n"
"Update FIPS hash with \"make module-update-fips-hash FIPS_HASH=%s\".\n",
newhash);
}
else {
pr_err("In-core integrity hash check failure.\n");
pr_err("ERROR: could not compute new hash. Contact customer support.\n");
}
}
return -ECANCELED;
}
#endif /* HAVE_FIPS */
#ifdef WC_RNG_SEED_CB
ret = wc_SetSeed_Cb(WC_GENERATE_SEED_DEFAULT);
if (ret < 0) {
pr_err("ERROR: wc_SetSeed_Cb() failed with return code %d.\n", ret);
(void)libwolfssl_cleanup();
msleep(10);
return -ECANCELED;
}
#endif /* WC_RNG_SEED_CB */
#ifdef WOLFCRYPT_ONLY
ret = wolfCrypt_Init();
if (ret != 0) {
pr_err("ERROR: wolfCrypt_Init() failed: %s\n", wc_GetErrorString(ret));
return -ECANCELED;
}
#else
ret = wolfSSL_Init();
if (ret != WOLFSSL_SUCCESS) {
pr_err("ERROR: wolfSSL_Init() failed: %s\n", wc_GetErrorString(ret));
return -ECANCELED;
}
#endif
#if defined(HAVE_FIPS) && FIPS_VERSION3_GT(5,2,0)
ret = wc_RunAllCast_fips();
if (ret != 0) {
pr_err("ERROR: wc_RunAllCast_fips() failed with return value %d\n", ret);
return -ECANCELED;
}
pr_info("FIPS 140-3 wolfCrypt-fips v%d.%d.%d%s%s startup "
"self-test succeeded.\n",
#ifdef HAVE_FIPS_VERSION_MAJOR
HAVE_FIPS_VERSION_MAJOR,
#else
HAVE_FIPS_VERSION,
#endif
#ifdef HAVE_FIPS_VERSION_MINOR
HAVE_FIPS_VERSION_MINOR,
#else
0,
#endif
#ifdef HAVE_FIPS_VERSION_PATCH
HAVE_FIPS_VERSION_PATCH,
#else
0,
#endif
#ifdef HAVE_FIPS_VERSION_PORT
"-",
HAVE_FIPS_VERSION_PORT
#else
"",
""
#endif
);
#endif /* HAVE_FIPS && FIPS_VERSION3_GT(5,2,0) */
#ifdef FIPS_OPTEST
#ifdef HAVE_WC_FIPS_OPTEST_CONTESTFAILURE_EXPORT
conTestFailure_ptr = &wc_fips_optest_conTestFailure;
#else
conTestFailure_ptr = (wolfSSL_Atomic_Int *)my_kallsyms_lookup_name("conTestFailure");
if (conTestFailure_ptr == NULL) {
pr_err("ERROR: couldn't obtain conTestFailure_ptr.\n");
return -ECANCELED;
}
#endif
ret = linuxkm_lkcapi_sysfs_install_node(&FIPS_optest_trig_attr, &installed_sysfs_FIPS_optest_trig_files);
if (ret != 0) {
pr_err("ERROR: linuxkm_lkcapi_sysfs_install_node() failed for %s (code %d).\n", FIPS_optest_trig_attr.attr.name, ret);
return -ECANCELED;
}
#ifdef FIPS_OPTEST_FULL_RUN_AT_MODULE_INIT
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
{
unsigned long stack_usage;
stack_usage = wc_linuxkm_stack_current();
pr_info("STACK INFO: usage at call to linuxkm_op_test_wrapper(): %lu of %lu total\n", stack_usage, THREAD_SIZE);
wc_linuxkm_stack_hwm_prepare(0xee);
#endif
(void)linuxkm_op_test_wrapper();
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
stack_usage = wc_linuxkm_stack_hwm_measure_rel(0xee);
pr_info("STACK INFO: rel usage by linuxkm_op_test_wrapper(): %lu\n", stack_usage);
/* shush up false stack HWM reading by kernel: */
wc_linuxkm_stack_hwm_prepare(0);
}
#endif
WOLFSSL_ATOMIC_STORE(*conTestFailure_ptr, 0);
for (i = 0; i < FIPS_CAST_COUNT; ++i)
fipsCastStatus_put(i, FIPS_CAST_STATE_INIT);
/* note, must call fipsEntry() here, not wolfCrypt_IntegrityTest_fips(),
* because wc_GetCastStatus_fips(FIPS_CAST_HMAC_SHA2_256) isn't available
* anymore.
*/
if (WC_SIG_IGNORE_BEGIN() >= 0) {
fipsEntry();
(void)WC_SIG_IGNORE_END();
}
else
pr_err("ERROR: WC_SIG_IGNORE_BEGIN() failed.\n");
ret = wolfCrypt_GetStatus_fips();
if (ret != 0) {
pr_err("ERROR: wolfCrypt_GetStatus_fips() after reset failed with code %d: %s\n", ret, wc_GetErrorString(ret));
return -ECANCELED;
}
#endif
#endif /* FIPS_OPTEST */
#ifndef NO_CRYPT_TEST
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
{
unsigned long stack_usage;
stack_usage = wc_linuxkm_stack_current();
pr_info("STACK INFO: usage at call to wolfcrypt_test(): %lu of %lu total\n", stack_usage, THREAD_SIZE);
wc_linuxkm_stack_hwm_prepare(0xee);
#endif
ret = wolfcrypt_test(NULL);
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
stack_usage = wc_linuxkm_stack_hwm_measure_rel(0xee);
pr_info("STACK INFO: rel usage by wolfcrypt_test(): %lu\n", stack_usage);
/* shush up false stack HWM reading by kernel: */
wc_linuxkm_stack_hwm_prepare(0);
}
#endif
if (ret < 0) {
pr_err("ERROR: wolfcrypt self-test failed with return code %d.\n", ret);
(void)libwolfssl_cleanup();
msleep(10);
return -ECANCELED;
}
pr_info("wolfCrypt self-test passed.\n");
#else
#if !defined(HAVE_FIPS) || FIPS_VERSION3_LE(5,2,0)
pr_info("skipping full wolfcrypt_test() "
"(configure with --enable-crypttests to enable).\n");
#endif
#endif
#ifdef LINUXKM_LKCAPI_REGISTER
#ifdef LINUXKM_LKCAPI_REGISTER_ONLY_ON_COMMAND
ret = linuxkm_lkcapi_sysfs_install();
if (ret) {