search

what is linux nx

Apr 10, 2023 am 11:05 AM
linux

linux nx refers to "No-eXecute", which is a protection mechanism in Linux, that is, the data is not executable to prevent the attacker's shellcode from trying to execute in the data area due to overflow during program operation. Case.

what is linux nx

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is linux nx?

Some protection mechanisms commonly used in Linux programs

1. NX (DEP in Windows)

NX: No-eXecute, DEP: Data Execute Prevention

  • That is, the data is not executable, preventing the attacker's shellcode from trying to execute in the data area due to overflow during program operation.
  • gcc is enabled by default, the options are:
gcc -o test test.c      // 默认情况下,开启NX保护
gcc -z execstack -o test test.c  // 禁用NX保护
gcc -z noexecstack -o test test.c  // 开启NX保护

2. PIE (ASLR)

PIE: Position-Independent Excutable, ASLR: Address Space Layout Randomization

  • fpie/fPIE: Need to be used with option -pie to turn on the pie option to compile the executable file so that elf has the shared library attribute. Can be loaded and run anywhere in memory. Similar to it, there is fpic/fPIC. The description is https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
-fpic

	Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)

	Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

	When this flag is set, the macros `__pic__` and `__PIC__` are defined to 1.

-fPIC

	If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table.This option makes a difference on AArch64, m68k, PowerPC and SPARC.

	Position-independent code requires special support, and therefore works only on certain machines.

	When this flag is set, the macros `__pic__` and `__PIC__` are defined to 2.

-fpie
-fPIE

	These options are similar to -fpic and -fPIC, but the generated position-independent code can be only linked into executables. Usually these options are used to compile code that will be linked using the  -pie  GCC option.

	-fpie and -fPIE both define the macros `__pie__` and `__PIE__`. The macros have the value 1 for `-fpie` and 2 for `-fPIE`.
  • The difference is that fpic /fPIC is used for compiling shared libraries, and fpie/fPIE is the option for pie file compilation. The document says that the shared library generated by pic (position-independent code) can only be linked to the executable file. Afterwards, you can compile a simple C program by yourself and pie will run normally. That is, as many articles on the Internet say, the position-independent code generated by the pie option can be assumed to be in This program, but I don’t see any difference between fpie/fPIE. It’s just that the macro definition is only the difference between 1 and 2. It seems...
    Compile command (PIE is not enabled by default):
gcc -fpie -pie -o test test.c    // 开启PIE
gcc -fPIE -pie -o test test.c    // 开启PIE
gcc -fpic -o test test.c         // 开启PIC
gcc -fPIC -o test test.c         // 开启PIC
gcc -no-pie -o test test.c       // 关闭PIE
  • ASLR (Address Space Randomization) was originally designed to only randomize the addresses of stack, library, heap and other segments. The value of ASLR is stored in /proc/sys/kernel/randomize_va_space, as follows:

0 - Indicates that process address space randomization is turned off.
1 - Indicates randomizing the base address of mmap, stack and vdso pages.
2 - Indicates increasing the randomization of the stack (heap) on the basis of 1. (Default)

Change its value method: echo 0 > /proc/sys/kernel/randomize_va_space

vDSO: virtual dynamic shared object;
mmap: Memory mapping.
PIE is responsible for the random base address of the executable program.
The following is taken from Wiki:

Position-independent executable (PIE) implements a random base address for the main executable binary and has been in place since 2003. It provides the same address randomness to the main executable as being used for the shared libraries.

PIE is part of ASLR, ASLR is a system function, and PIE is a compilation option.
Note: When allocating heap, there are two methods: mmap() and brk(), which are controlled by malloc() Called when memory is allocated, brk when the allocation is small, otherwise mmap, 128k difference.

3. Canary (Stack Protection)

Canary protects the stack. Every time the function is executed, a Canary value is randomly generated on the stack. Afterwards, when the function returns from execution, the Canary value is detected. If it is inconsistent, the system will report an exception.

  • Wiki:
  • Canaries or canary words are known values ​​that are placed between a buffer and control data on the stack to monitor buffer overflows. When the buffer overflows, the first data to be corrupted will usually be the canary, and a failed verification of the canary data will therefore alert of an overflow, which can then be handled, for example, by invalidating the corrupted data. A canary value should not be confused with a sentinel value.

As mentioned above, the canary value is placed between the buffer and the control data. When the buffer overflows, the value is overwritten so that it can be detected To determine whether there is an error or attack. Mitigating buffer overflow attacks.

  • Compile options:
gcc -o test test.c                       //默认关闭
gcc -fno-stack-protector -o test test.c  //禁用栈保护
gcc -fstack-protector -o test test.c     //启用堆栈保护,不过只为局部变量中含有 char 数组的函数插入保护代码
gcc -fstack-protector-all -o test test.c //启用堆栈保护,为所有函数插入保护代码

4. RELRO (RELocation Read Only)

There are two RELRO modes in Linux: ”Partial RELRO" and "Full RELRO". Partial RELRO is enabled by default in Linux.

Partial RELRO:

  • Compile command:
    gcc -o test test.c // Partially enabled by default
    gcc -Wl,- z,relro -o test test.c // Turn on part of RELRO
    gcc -z lazy -o test test.c // Turn on part of part
  • The various parts of the ELF file are reordered. Internal data sections (such as .got, .dtors, etc.) are placed before program's data sections (such as .data and .bss);
  • The GOT pointed to by no plt is only Read;
  • GOT table can be written (should be different from the above).

Full RELRO:

  • Compile command:
    gcc -Wl,-z,relro,-z,now -o test test.c // Turn on Full RELRO
    gcc -z now -o test test.c / / Enable all
  • Support all functions of Partial mode;
  • The entire GOT table is mapped to read-only.

gcc -z norelro -o a a.c // RELRO is turned off, that is, No RELRO

Note:

  • .dtors: Called when the shared library defined with .dtors is loaded;
  • In the case of bss or data overflow errors, Partial and Full RELRO protect the data segments in the ELF from being overwritten. However, only Full RELRO can mitigate GOT table overwrite attacks, but it is relatively expensive because the program needs to parse all symbols before starting.
  • Related recommendations: "Linux Video Tutorial"

The above is the detailed content of what is linux nx. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor