headers_install.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. if [ $# -ne 2 ]
  4. then
  5. echo "Usage: headers_install.sh INFILE OUTFILE"
  6. echo
  7. echo "Prepares kernel header files for use by user space, by removing"
  8. echo "all compiler.h definitions and #includes, removing any"
  9. echo "#ifdef __KERNEL__ sections, and putting __underscores__ around"
  10. echo "asm/inline/volatile keywords."
  11. echo
  12. echo "INFILE: header file to operate on"
  13. echo "OUTFILE: output file which the processed header is written to"
  14. exit 1
  15. fi
  16. # Grab arguments
  17. INFILE=$1
  18. OUTFILE=$2
  19. TMPFILE=$OUTFILE.tmp
  20. trap 'rm -f $OUTFILE $TMPFILE' EXIT
  21. # SPDX-License-Identifier with GPL variants must have "WITH Linux-syscall-note"
  22. if [ -n "$(sed -n -e "/SPDX-License-Identifier:.*GPL-/{/WITH Linux-syscall-note/!p}" $INFILE)" ]; then
  23. echo "error: $INFILE: missing \"WITH Linux-syscall-note\" for SPDX-License-Identifier" >&2
  24. exit 1
  25. fi
  26. sed -E -e '
  27. s/([[:space:](])(__user|__force|__iomem)[[:space:]]/\1/g
  28. s/__attribute_const__([[:space:]]|$)/\1/g
  29. s@^#include <linux/compiler(|_types).h>@@
  30. s/(^|[^a-zA-Z0-9])__packed([^a-zA-Z0-9_]|$)/\1__attribute__((packed))\2/g
  31. s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g
  32. s@#(ifndef|define|endif[[:space:]]*/[*])[[:space:]]*_UAPI@#\1 @
  33. ' $INFILE > $TMPFILE || exit 1
  34. scripts/unifdef -U__KERNEL__ -D__EXPORTED_HEADERS__ $TMPFILE > $OUTFILE
  35. [ $? -gt 1 ] && exit 1
  36. # Remove /* ... */ style comments, and find CONFIG_ references in code
  37. configs=$(sed -e '
  38. :comment
  39. s:/\*[^*][^*]*:/*:
  40. s:/\*\*\**\([^/]\):/*\1:
  41. t comment
  42. s:/\*\*/: :
  43. t comment
  44. /\/\*/! b check
  45. N
  46. b comment
  47. :print
  48. P
  49. D
  50. :check
  51. s:^\(CONFIG_[[:alnum:]_]*\):\1\n:
  52. t print
  53. s:^[[:alnum:]_][[:alnum:]_]*::
  54. s:^[^[:alnum:]_][^[:alnum:]_]*::
  55. t check
  56. d
  57. ' $OUTFILE)
  58. # The entries in the following list do not result in an error.
  59. # Please do not add a new entry. This list is only for existing ones.
  60. # The list will be reduced gradually, and deleted eventually. (hopefully)
  61. #
  62. # The format is <file-name>:<CONFIG-option> in each line.
  63. config_leak_ignores="
  64. arch/alpha/include/uapi/asm/setup.h:CONFIG_ALPHA_LEGACY_START_ADDRESS
  65. arch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_16K
  66. arch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_4K
  67. arch/arc/include/uapi/asm/swab.h:CONFIG_ARC_HAS_SWAPE
  68. arch/arm/include/uapi/asm/ptrace.h:CONFIG_CPU_ENDIAN_BE8
  69. arch/hexagon/include/uapi/asm/ptrace.h:CONFIG_HEXAGON_ARCH_VERSION
  70. arch/hexagon/include/uapi/asm/user.h:CONFIG_HEXAGON_ARCH_VERSION
  71. arch/ia64/include/uapi/asm/cmpxchg.h:CONFIG_IA64_DEBUG_CMPXCHG
  72. arch/m68k/include/uapi/asm/ptrace.h:CONFIG_COLDFIRE
  73. arch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_NO
  74. arch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_SUPPORT
  75. arch/riscv/include/uapi/asm/ptrace.h:CONFIG_VLEN_256
  76. arch/x86/include/uapi/asm/auxvec.h:CONFIG_IA32_EMULATION
  77. arch/x86/include/uapi/asm/auxvec.h:CONFIG_X86_64
  78. arch/x86/include/uapi/asm/mman.h:CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  79. include/uapi/asm-generic/fcntl.h:CONFIG_64BIT
  80. include/uapi/linux/atmdev.h:CONFIG_COMPAT
  81. include/uapi/linux/eventpoll.h:CONFIG_PM_SLEEP
  82. include/uapi/linux/hw_breakpoint.h:CONFIG_HAVE_MIXED_BREAKPOINTS_REGS
  83. include/uapi/linux/pktcdvd.h:CONFIG_CDROM_PKTCDVD_WCACHE
  84. "
  85. for c in $configs
  86. do
  87. leak_error=1
  88. for ignore in $config_leak_ignores
  89. do
  90. if echo "$INFILE:$c" | grep -q "$ignore$"; then
  91. leak_error=
  92. break
  93. fi
  94. done
  95. if [ "$leak_error" = 1 ]; then
  96. echo "error: $INFILE: leak $c to user-space" >&2
  97. exit 1
  98. fi
  99. done
  100. rm -f $TMPFILE
  101. trap - EXIT