← Documents Documentation/staging/magic-number.rst GitHub 원문 ↗

Linux 6.18.37 · Staging

Linux magic number

Kernel structure 선두의 magic field로 memory 손상과 잘못된 void pointer를 검출하는 규칙, 변경 이력, 현재 registry 값을 정리합니다.

Source pathDocumentation/staging/magic-number.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

magic-number.rst:1-84

Kernel structure 선두의 magic field로 memory 손상과 잘못된 void pointer를 검출하는 규칙, 변경 이력, 현재 registry 값을 정리합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. _magicnumbers:
2
3 Linux magic numbers
4 ===================
5
6 This file is a registry of magic numbers which are in use. When you
7 add a magic number to a structure, you should also add it to this
8 file, since it is best if the magic numbers used by various structures
9 are unique.
10
11 It is a **very** good idea to protect kernel data structures with magic
12 numbers. This allows you to check at run time whether (a) a structure
13 has been clobbered, or (b) you've passed the wrong structure to a
14 routine. This last is especially useful --- particularly when you are
15 passing pointers to structures via a void * pointer. The tty code,
16 for example, does this frequently to pass driver-specific and line
17 discipline-specific structures back and forth.
18
19 The way to use magic numbers is to declare them at the beginning of
20 the structure, like so::
21
22 struct tty_ldisc {
23 int magic;
24 ...
25 };
26
27 Please follow this discipline when you are adding future enhancements
28 to the kernel! It has saved me countless hours of debugging,
29 especially in the screwy cases where an array has been overrun and
30 structures following the array have been overwritten. Using this
31 discipline, these cases get detected quickly and safely.
32
33 Changelog::
34
35 Theodore Ts'o
36 31 Mar 94
37
38 The magic table is current to Linux 2.1.55.
39
40 Michael Chastain
42 22 Sep 1997
43
44 Now it should be up to date with Linux 2.1.112. Because
45 we are in feature freeze time it is very unlikely that
46 something will change before 2.2.x. The entries are
47 sorted by number field.
48
49 Krzysztof G. Baranowski
51 29 Jul 1998
52
53 Updated the magic table to Linux 2.5.45. Right over the feature freeze,
54 but it is possible that some new magic numbers will sneak into the
55 kernel before 2.6.x yet.
56
57 Petr Baudis
59 03 Nov 2002
60
61 Updated the magic table to Linux 2.5.74.
62
63 Fabian Frederick
65 09 Jul 2003
66
67
68 ===================== ================ ======================== ==========================================
69 Magic Name Number Structure File
70 ===================== ================ ======================== ==========================================
71 PG_MAGIC 'P' pg_{read,write}_hdr ``include/uapi/linux/pg.h``
72 APM_BIOS_MAGIC 0x4101 apm_user ``arch/x86/kernel/apm_32.c``
73 FASYNC_MAGIC 0x4601 fasync_struct ``include/linux/fs.h``
74 SLIP_MAGIC 0x5302 slip ``drivers/net/slip/slip.h``
75 BAYCOM_MAGIC 19730510 baycom_state ``drivers/net/hamradio/baycom_epp.c``
76 HDLCDRV_MAGIC 0x5ac6e778 hdlcdrv_state ``include/linux/hdlcdrv.h``
77 KV_MAGIC 0x5f4b565f kernel_vars_s ``arch/mips/include/asm/sn/klkernvars.h``
78 CODA_MAGIC 0xC0DAC0DA coda_file_info ``fs/coda/coda_fs_i.h``
79 YAM_MAGIC 0xF10A7654 yam_port ``drivers/net/hamradio/yam.c``
80 CCB_MAGIC 0xf2691ad2 ccb ``drivers/scsi/ncr53c8xx.c``
81 QUEUE_MAGIC_FREE 0xf7e1c9a3 queue_entry ``drivers/scsi/arm/queue.c``
82 QUEUE_MAGIC_USED 0xf7e1cc33 queue_entry ``drivers/scsi/arm/queue.c``
83 NMI_MAGIC 0x48414d4d455201 nmi_s ``arch/mips/include/asm/sn/nmi.h``
84 ===================== ================ ======================== ==========================================
85

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

구조체 보호 규칙

1-32

이 파일은 사용 중인 magic number registry다. 여러 structure의 magic number는 고유한 것이 좋으므로 structure에 새 magic number를 추가할 때 이 파일에도 등록해야 한다.

Kernel data structure를 magic number로 보호하면 runtime에 structure가 덮어써졌는지, 또는 routine에 잘못된 structure를 전달했는지 검사할 수 있다. 특히 `void *` pointer로 structure pointer를 넘길 때 유용하다. TTY code는 driver별·line-discipline별 structure를 주고받을 때 이를 자주 사용한다.

Magic field는 예제 `struct tty_ldisc`처럼 structure 시작 부분에 선언한다. 이 규칙은 array overrun으로 뒤따르는 structure가 덮어써지는 까다로운 경우를 빠르고 안전하게 검출해 debugging 시간을 줄인다. 향후 kernel 기능을 추가할 때도 이 규칙을 따라야 한다.

Magic number 검사
Allocate structureSet leading magic fieldPass typed or void pointer
Routine entryValidate magicUse structure or reject corruption

Structure 시작의 sentinel로 잘못된 pointer와 memory 손상을 조기에 찾는다.

.. _magicnumbers:

Linux magic numbers
===================

This file is a registry of magic numbers which are in use.  When you
add a magic number to a structure, you should also add it to this
file, since it is best if the magic numbers used by various structures
are unique.

It is a **very** good idea to protect kernel data structures with magic
numbers.  This allows you to check at run time whether (a) a structure
has been clobbered, or (b) you've passed the wrong structure to a
routine.  This last is especially useful --- particularly when you are
passing pointers to structures via a void * pointer.  The tty code,
for example, does this frequently to pass driver-specific and line
discipline-specific structures back and forth.

The way to use magic numbers is to declare them at the beginning of
the structure, like so::

	struct tty_ldisc {
		int	magic;
		...
	};

Please follow this discipline when you are adding future enhancements
to the kernel!  It has saved me countless hours of debugging,
especially in the screwy cases where an array has been overrun and
structures following the array have been overwritten.  Using this
discipline, these cases get detected quickly and safely.

Registry 변경 이력

33-67

Theodore Ts'o가 1994년 3월 31일 changelog를 남겼다. Michael Chastain은 1997년 9월 22일 table을 Linux 2.1.55 기준으로 맞췄다.

Krzysztof G. Baranowski는 1998년 7월 29일 Linux 2.1.112 기준으로 갱신하고 feature freeze라 2.2.x 전 변경 가능성이 작으며 entry를 number field 순으로 정렬했다고 기록했다.

Petr Baudis는 2002년 11월 3일 Linux 2.5.45 기준으로 갱신하면서 feature freeze 뒤에도 2.6.x 전에 새 magic number가 들어올 수 있다고 설명했다. Fabian Frederick은 2003년 7월 9일 Linux 2.5.74 기준으로 table을 갱신했다. 이름·날짜·메일 주소는 원문 표기를 유지한다.

Magic registry 이력
날짜담당자기준
1994-03-31Theodore Ts'o초기 changelog
1997-09-22Michael ChastainLinux 2.1.55
1998-07-29Krzysztof G. BaranowskiLinux 2.1.112
2002-11-03Petr BaudisLinux 2.5.45
2003-07-09Fabian FrederickLinux 2.5.74

원문 changelog의 담당자와 기준 version이다.

Changelog::

					Theodore Ts'o
					31 Mar 94

  The magic table is current to Linux 2.1.55.

					Michael Chastain
					<mailto:[email protected]>
					22 Sep 1997

  Now it should be up to date with Linux 2.1.112. Because
  we are in feature freeze time it is very unlikely that
  something will change before 2.2.x. The entries are
  sorted by number field.

					Krzysztof G. Baranowski
					<mailto: [email protected]>
					29 Jul 1998

  Updated the magic table to Linux 2.5.45. Right over the feature freeze,
  but it is possible that some new magic numbers will sneak into the
  kernel before 2.6.x yet.

					Petr Baudis
					<[email protected]>
					03 Nov 2002

  Updated the magic table to Linux 2.5.74.

					Fabian Frederick
					<[email protected]>
					09 Jul 2003

등록된 magic number

68-84

아래 표는 magic name, numeric value, 보호하는 structure, source file을 원문 순서와 값 그대로 정리한다.

Linux magic number registry
Magic NameNumberStructureFile
PG_MAGIC'P'pg_{read,write}_hdrinclude/uapi/linux/pg.h
APM_BIOS_MAGIC0x4101apm_userarch/x86/kernel/apm_32.c
FASYNC_MAGIC0x4601fasync_structinclude/linux/fs.h
SLIP_MAGIC0x5302slipdrivers/net/slip/slip.h
BAYCOM_MAGIC19730510baycom_statedrivers/net/hamradio/baycom_epp.c
HDLCDRV_MAGIC0x5ac6e778hdlcdrv_stateinclude/linux/hdlcdrv.h
KV_MAGIC0x5f4b565fkernel_vars_sarch/mips/include/asm/sn/klkernvars.h
CODA_MAGIC0xC0DAC0DAcoda_file_infofs/coda/coda_fs_i.h
YAM_MAGIC0xF10A7654yam_portdrivers/net/hamradio/yam.c
CCB_MAGIC0xf2691ad2ccbdrivers/scsi/ncr53c8xx.c
QUEUE_MAGIC_FREE0xf7e1c9a3queue_entrydrivers/scsi/arm/queue.c
QUEUE_MAGIC_USED0xf7e1cc33queue_entrydrivers/scsi/arm/queue.c
NMI_MAGIC0x48414d4d455201nmi_sarch/mips/include/asm/sn/nmi.h

원문의 13개 registry entry다.

===================== ================ ======================== ==========================================
Magic Name            Number           Structure                File
===================== ================ ======================== ==========================================
PG_MAGIC              'P'              pg_{read,write}_hdr      ``include/uapi/linux/pg.h``
APM_BIOS_MAGIC        0x4101           apm_user                 ``arch/x86/kernel/apm_32.c``
FASYNC_MAGIC          0x4601           fasync_struct            ``include/linux/fs.h``
SLIP_MAGIC            0x5302           slip                     ``drivers/net/slip/slip.h``
BAYCOM_MAGIC          19730510         baycom_state             ``drivers/net/hamradio/baycom_epp.c``
HDLCDRV_MAGIC         0x5ac6e778       hdlcdrv_state            ``include/linux/hdlcdrv.h``
KV_MAGIC              0x5f4b565f       kernel_vars_s            ``arch/mips/include/asm/sn/klkernvars.h``
CODA_MAGIC            0xC0DAC0DA       coda_file_info           ``fs/coda/coda_fs_i.h``
YAM_MAGIC             0xF10A7654       yam_port                 ``drivers/net/hamradio/yam.c``
CCB_MAGIC             0xf2691ad2       ccb                      ``drivers/scsi/ncr53c8xx.c``
QUEUE_MAGIC_FREE      0xf7e1c9a3       queue_entry              ``drivers/scsi/arm/queue.c``
QUEUE_MAGIC_USED      0xf7e1cc33       queue_entry              ``drivers/scsi/arm/queue.c``
NMI_MAGIC             0x48414d4d455201 nmi_s                    ``arch/mips/include/asm/sn/nmi.h``
===================== ================ ======================== ==========================================