Microsoft MVP성태의 닷넷 이야기
Linux: 102. Linux - 커널 이미지 파일 서명 (Ubuntu 환경) [링크 복사], [링크+제목 복사],
조회: 3061
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 5개 있습니다.)
개발 환경 구성: 162. 닷넷 개발자가 컴파일해 본 리눅스
; https://www.sysnet.pe.kr/2/0/1325

개발 환경 구성: 730. github - Linux 커널 repo를 윈도우 환경에서 git clone하는 방법
; https://www.sysnet.pe.kr/2/0/13787

Linux: 97. menuconfig에 CONFIG_DEBUG_INFO_BTF, CONFIG_DEBUG_INFO_BTF_MODULES 옵션이 없는 경우
; https://www.sysnet.pe.kr/2/0/13789

Linux: 98. Ubuntu 22.04 - 리눅스 커널 빌드 및 업그레이드
; https://www.sysnet.pe.kr/2/0/13790

Linux: 102. Linux - 커널 이미지 파일 서명 (Ubuntu 환경)
; https://www.sysnet.pe.kr/2/0/13807




Linux - 커널 이미지 파일 서명 (Ubuntu 환경)

직접 빌드한 커널 이미지를,

Ubuntu 22.04 - 리눅스 커널 빌드 및 업그레이드
; https://www.sysnet.pe.kr/2/0/13790

(대표적으로) Secure Boot 환경에서 사용하고 싶다면 서명을 해야 합니다. 이에 대해서는 문서가 많이 있으니 쉽게 적용할 수 있는데요,

How to sign things for Secure Boot
; https://ubuntu.com/blog/how-to-sign-things-for-secure-boot

Chapter 3. Signing a kernel and modules for Secure Boot
; https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/signing-a-kernel-and-modules-for-secure-boot_managing-monitoring-and-updating-the-kernel

이번 글에서는 위의 내용을 간단하게 정리해 보겠습니다. ^^




우선, openssl로 커널 서명 용도를 만족하는 키를 생성하기 위해 cnf 파일을 만들고,

$ cat kernel_key.cnf
HOME                    = .
RANDFILE                = $ENV::HOME/.rnd 
[ req ]
distinguished_name      = req_distinguished_name
x509_extensions         = v3
string_mask             = utf8only
prompt                  = no

[ req_distinguished_name ]
commonName              = Secure Boot Signing Key

[ v3 ]
subjectKeyIdentifier    = hash
authorityKeyIdentifier  = keyid:always,issuer
basicConstraints        = critical,CA:FALSE
extendedKeyUsage        = codeSigning,1.3.6.1.4.1.311.10.3.6
nsComment               = "OpenSSL Generated Certificate"

이 설정을 반영해 키를 생성합니다.

$ openssl req -config ./kernel_key.cnf -new -x509 -newkey rsa:2048 -nodes -days 36500 -outform DER -keyout "kernel_key.priv" -out "kernel_key.der"

$ sudo openssl x509 -in kernel_key.der -inform DER -outform PEM -out kernel_key.pem

$ ls kernel_*
kernel_key.cnf  kernel_key.der  kernel_key.pem  kernel_key.priv

그다음, 해당 커널이 로드될 시스템에 위에서 생성한 키의 공개키 파트를 등록합니다.

// 암호는 임의로 설정합니다. (나중에 쓸 것이니 기억해 두세요.)

$ sudo mokutil --import kernel_key.der
input password: 
input password again: 

이후 "reboot"을 하면 다음 화면과 같이 잠시 "Shim UEFI key management" 화면이 나타납니다. (이 화면을 보여주는 프로그램의 이름이 "MOKManager" 라는군요. ^^)

sign_linux_kernel_1.png

그럼 아무 키나 누르면 화면이 넘어가는데요,

sign_linux_kernel_2.png

"Enroll MOK"을 선택하고 Enter를 누르면 다음 메뉴로 넘어가는데 (등록 키의 개수에 따라 다를 수 있는) "View key 0"와 "Continue"에서 "View key 0"를 선택해 Enter를 누릅니다. 그럼, 방금 전에 "mokutil"로 등록한 키에 해당하는 내용이 나올 것입니다. 해당 키가 맞는다면 Enter를 눌러 이전 메뉴로 돌아와 이번엔 "Continue"를 선택, 이어서 "Yes"를 선택합니다.

그럼 (역시 위에서 mokutil 실행 시 입력했던) 암호를 묻는 단계가 나오고, 통과하면 "Reboot"을 선택해 재부팅하면 됩니다.

이후, 정상적으로 등록됐는지는 이렇게 확인할 수 있습니다.

$ sudo mokutil --list-enrolled
[key 1]
SHA1 Fingerprint: 76:a0:92:06:58:00:bf:37:69:01:c3:72:cd:55:a9:0e:1f:de:d2:e0
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            b9:41:24:a0:18:2c:92:67
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=GB, ST=Isle of Man, L=Douglas, O=Canonical Ltd., CN=Canonical Ltd. Master Certificate Authority
        ...[생략]...

[key 2]
SHA1 Fingerprint: 06:1d:0c:9f:d6:b5:cb:35:3f:82:ea:c7:ae:15:c2:4a:c1:df:8c:46
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            64:2c:10:08:10:ce:f6:53:77:e3:92:ed:d1:97:1c:45:53:d8:11:aa
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: CN=Secure Boot Signing Key
        ...[생략]...




자, 이제 마지막으로 빌드된 커널 이미지 파일을 서명해 주면 됩니다.

$ ll /boot/vmlinuz-6.*
-rw-r--r-- 1 root root 13382144 Nov  8 05:39 /boot/vmlinuz-6.11.5
-rw------- 1 root root 14952840 Sep 27 12:26 /boot/vmlinuz-6.8.0-48-generic

제 경우에 6.8.0-48-generic이 최초 Ubuntu 24.04에 사용된 것이고, 새롭게 6.11.5 버전을 빌드한 것인데요, 이것을 다음과 같은 식으로 서명하시면 됩니다.

$ sudo sbsign --key kernel_key.priv --cert kernel_key.pem /boot/vmlinuz-6.11.5 --output /boot/vmlinuz-6.11.5.signed
Signing Unsigned original image

$ ls /boot/vmlinuz-6.*
/boot/vmlinuz-6.11.5  /boot/vmlinuz-6.11.5.old  /boot/vmlinuz-6.11.5.signed  /boot/vmlinuz-6.8.0-48-generic

그다음 서명되지 않은 initrd.img-* 파일이 있을 텐데요, (위의 경우 initrd.img-6.11.5) 이것을 위에서 만든 signed 이름 규칙을 따라 또 하나 만들어 줍니다.

$ sudo cp /boot/initrd.img-6.11.5{,.signed}

$ ls /boot/initrd.img-6.*
/boot/initrd.img-6.11.5  /boot/initrd.img-6.11.5.signed  /boot/initrd.img-6.8.0-48-generic

끝입니다. 이제 GRUB 설정을 업데이트하고,

$ sudo update-grub
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.11.5.signed
Found initrd image: /boot/initrd.img-6.11.5.signed
Found linux image: /boot/vmlinuz-6.11.5
Found initrd image: /boot/initrd.img-6.11.5
Found linux image: /boot/vmlinuz-6.11.5.old
Found initrd image: /boot/initrd.img-6.11.5
Found linux image: /boot/vmlinuz-6.8.0-48-generic
Found initrd image: /boot/initrd.img-6.8.0-48-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done

재부팅하면 새로운 버전의 커널이 부팅될 것입니다.

$ uname -r
6.11.5





[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 11/9/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13803정성태11/7/20242997닷넷: 2309. C# - .NET Core에서 바뀐 DateTime.Ticks의 정밀도
13802정성태11/6/20243252Windows: 269. GetSystemTimeAsFileTime과 GetSystemTimePreciseAsFileTime의 차이점파일 다운로드1
13801정성태11/5/20242947Linux: 100. eBPF의 2가지 방식 - libbcc와 libbpf(CO-RE)
13800정성태11/3/20243937닷넷: 2308. C# - ICU 라이브러리를 활용한 문자열의 대소문자 변환 [2]파일 다운로드1
13799정성태11/2/20242870개발 환경 구성: 732. 모바일 웹 브라우저에서 유니코드 문자가 표시되지 않는 경우
13798정성태11/2/20242811개발 환경 구성: 731. 유니코드 - 출력 예시 및 폰트 찾기
13797정성태11/1/20243110C/C++: 185. C++ - 문자열의 대소문자를 변환하는 transform + std::tolower/toupper 방식의 문제점파일 다운로드1
13796정성태10/31/20242926C/C++: 184. C++ - ICU dll을 이용하는 예제 코드 (Windows)파일 다운로드1
13795정성태10/31/20242771Windows: 268. Windows - 리눅스 환경처럼 공백으로 끝나는 프롬프트 만들기
13794정성태10/30/20242890닷넷: 2307. C# - 윈도우에서 한글(및 유니코드)을 포함한 콘솔 프로그램을 컴파일 및 실행하는 방법
13793정성태10/28/20242832C/C++: 183. C++ - 윈도우에서 한글(및 유니코드)을 포함한 콘솔 프로그램을 컴파일 및 실행하는 방법
13792정성태10/27/20242416Linux: 99. Linux - 프로세스의 실행 파일 경로 확인
13791정성태10/27/20242848Windows: 267. Win32 API의 A(ANSI) 버전은 DBCS를 사용할까요?파일 다운로드1
13790정성태10/27/20242734Linux: 98. Ubuntu 22.04 - 리눅스 커널 빌드 및 업그레이드
13789정성태10/27/20242517Linux: 97. menuconfig에 CONFIG_DEBUG_INFO_BTF, CONFIG_DEBUG_INFO_BTF_MODULES 옵션이 없는 경우
13788정성태10/26/20242680Linux: 96. eBPF (bpf2go) - fentry, fexit를 이용한 트레이스
13787정성태10/26/20242484개발 환경 구성: 730. github - Linux 커널 repo를 윈도우 환경에서 git clone하는 방법 [1]
13786정성태10/26/20242939Windows: 266. Windows - 대소문자 구분이 가능한 파일 시스템
13785정성태10/23/20243001C/C++: 182. 윈도우가 운영하는 2개의 Code Page파일 다운로드1
13784정성태10/23/20243018Linux: 95. eBPF - kprobe를 이용한 트레이스
13783정성태10/23/20242883Linux: 94. eBPF - vmlinux.h 헤더 포함하는 방법 (bpf2go에서 사용)
13782정성태10/23/20242652Linux: 93. Ubuntu 22.04 - 커널 이미지로부터 커널 함수 역어셈블
13781정성태10/22/20242565오류 유형: 930. WSL + eBPF: modprobe: FATAL: Module kheaders not found in directory
13780정성태10/22/20242764Linux: 92. WSL 2 - 커널 이미지로부터 커널 함수 역어셈블
13779정성태10/22/20242801개발 환경 구성: 729. WSL 2 - Mariner VM 커널 이미지 업데이트 방법
13778정성태10/21/20243042C/C++: 181. C/C++ - 소스코드 파일의 인코딩, 바이너리 모듈 상태의 인코딩
1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...