Saturday, November 21, 2026

Standard Disk Format

 Introduction

The standard described in the document is used on nearly all x86 PC's, for administration of diskspace on fixed disks. The kind of diskspace administration this standard provides is very simple. The standard allows the diskspace to be divided in up to 4 partitions (normally referred to as primary partitions). Each of these 4 partitions can contain a separate file system. The heart of the standard is the first sector on the disk, which is reserved for the Master Boot Record (MBR).

Master Boot Record

The Master Boot Record is in first sector on the disk. This is sector 0 in LBA mode, and sector 0:0:1 in CHS mode. This sector is checked by the BIOS for the AA55h signature at offset 1FEh, to see if drive is bootable. This signature lets the BIOS know that the code in the beginning of this sector is executable. The structure of the Master Boot Record is:

Master Boot Record Structure

Part

Offset

Size

Description

Code

0000h

446 bytes

Execution is transferred to here after the BIOS has finished.

Partition Table

01BEh

16 bytes

1st Partition Entry

01CEh

16 bytes

2nd Partition Entry

01DEh

16 bytes

3rd Partition Entry

01EEh

16 bytes

4th Partition Entry

Signature

01FEh

2 bytes

AA55h signature. 55h at offset 1FEh and AAh at offset 1FFh

 

Partition Entries

Each of the partition entries from the partition table has the following structure:

Partition Entry Structure

Offset

Size

Description

0000h

1 bytes

A value of 80h means active, and 00h means inactive.

0001h

1 bytes

Start of partition - Head

0002h

2 bytes

Start of partition - Cylinder/Sector

0004h

1 bytes

Partition Type

0005h

1 bytes

End of partition - Head

0006h

2 bytes

End of partition - Cylinder/Sector

0008h

4 bytes

LBA address for the first sector in the partition.

000Ch

4 bytes

Number of sectors in the partition

 

Packed CHS

The location information for the beginning and end of the partition, consist of three bytes each. The first byte is the Head count. This is an 8 bit value, which makes room for 256 different values. The next word contains the Cylinder and Sector information and is packed together. The cylinder number is a 10 bit value and the Sector number is a 6 bit value. They are packed in the following way:

Structure of the packed cylinder/structure word

15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

Cylinder [7-0]

Cyl. [9+8]

Sector [5-0]

0000h

 

Partition Types

The partition type value shows what kind of file system that has been installed in the partition. Partition Type = File System Identifier. The list of partition types is long and world wide there are probably at least a few which override each other. I'm continuously trying to compile a pretty complete list of file system identifiers. The most up-to-date list can be found here.

MBR Code

The code in the MBR sector has a very limited amount of resources available. It only has a maximum of 446 bytes to get the job done. What it has to do, is locate the partition which is marked active, load the first sector from that partition into linear memory at 00007C00h, and transfer execution to that code.

This is the execution environment under which the MBR code must execute:

· CS:IP can be 07C0:0000 or 0000:7C00. Linear address is the same. Should be initialized.

· SS:SP is also unknown. Usually initialized.

· DL contain number of the drive from which the code was loaded from.

· The state of the other registers like AX, BX, CX, DH, SI, DI, BP, DS, ES, FS and GS are unknown.

Remember to pass the drive number along in DL.

Compatibility Advice

When adding new partitions to a disk, it is advised that the partition is aligned at sector 1. That means a partition would always start at cylinder x, head y and sector 1, where x and y can be any given number (but still within the limits of the disk).

The 8 Gb Limit

The CHS address mode has a limit of 16.515.072 sectors, which is usually 8 Gb. To add partitions beyond this limit, the drive must support the LBA address mode. With this address mode the limit is 2 Tb. When dealing with partitions beyond this limit, the packed CHS information in the 'Start of partition' and 'End of partition' fields should be set to the maximum possible, and only the LBA address is used.

Conclusion

This is actually all there is to the standard disk format. But again it only divides the available space into a maximum of 4 primary partitions. The really advanced stuff comes when you want to access the data which has been saved in a file system specific way.

Top 10 Coding Practices

 1.  Validate input. Validate input from all untrusted data sources. Proper input validation can eliminate the vast majority of software vulnerabilities. Be suspicious of most external data sources, including command line arguments, network interfaces, environmental variables, and user controlled files.

2.  Heed compiler warnings. Compile code using the highest warning level available for your compiler and eliminate warnings by modifying the code.

3.  Architect and design for security policies. Create a software architecture and design your software to implement and enforce security policies. For example, if your system requires different privileges at different times, consider dividing the system into distinct intercommunicating subsystems, each with an appropriate privilege set.

4.  Keep it simple. Keep the design as simple and small as possible. Complex designs increase the likelihood that errors will be made in their implementation, configuration, and use. Additionally, the effort required to achieve an appropriate level of assurance increases dramatically as security mechanisms become more complex.

5.  Default deny. Base access decisions on permission rather than exclusion. This means that, by default, access is denied and the protection scheme identifies conditions under which access is permitted.

6.  Adhere to the principle of least privilege. Every process should execute with the least set of privileges necessary to complete the job. Any elevated permission should be held for a minimum time. This approach reduces the opportunities an attacker has to execute arbitrary code with elevated privileges.

7.  Sanitize data sent to other systems. Sanitize all data passed to complex subsystems such as command shells, relational databases, and commercial off-the-shelf (COTS) components. Attackers may be able to invoke unused functionality in these components through the use of SQL, command, or other injection attacks. This is not necessarily an input validation problem because the complex subsystem being invoked does not understand the context in which the call is made. Because the calling process understands the context, it is responsible for sanitizing the data before invoking the subsystem.

8.  Practice defense in depth. Manage risk with multiple defensive strategies, so that if one layer of defense turns out to be inadequate, another layer of defense can prevent a security flaw from becoming an exploitable vulnerability and/or limit the consequences of a successful exploit. For example, combining secure programming techniques with secure runtime environments should reduce the likelihood that vulnerabilities remaining in the code at deployment time can be exploited in the operational environment.

9.  Use effective quality assurance techniques. Good quality assurance techniques can be effective in identifying and eliminating vulnerabilities. Penetration testing, fuzz testing, and source code audits should all be incorporated as part of an effective quality assurance program. Independent security reviews can lead to more secure systems. External reviewers bring an independent perspective; for example, in identifying and correcting invalid assumptions.

10. Adopt a secure coding standard. Develop and/or apply a secure coding standard for your target development language and platform. 

Tuesday, April 25, 2017

Linux assemblers: A comparison of GAS and NASM


1. Assembling:

GAS (AT&T syntax)
as -o program.o program.s
NASM (Intel syntax)
nasm -f elf -o program.o program.asm

2. Linking: 
ld -o program program.o
ld --dynamic-linker /lib/ld-linux.so.2 -lc -o program program.o


AT&T syntax
Intel syntax
opposite order src&dest ops
movl $4, %eax
mov eax, 4
immediate operands
pushl $4
push 4
register operands
movl $4, %eax
mov eax, 4
the size of memory operands
movb foo, %al
#b(8-bit)/w(16)/l(32)
mov al, byte ptr foo
#byte/word/dword ptr
Immediate form long jumps and calls
lcall/ljmp $section, $offset
lret $stack-adjust
call/jmp far section:offset
ret far stack-adjust
declare 32-/16-/8-bit numbers
.long/.int/.byte
dd/dw/db
declare variables
.ascii/.asciz/.string

memory location address
(var1)
[var1]
comments
/* */, // or #
;
entry point(.text)
.global _start:
global _start:
BSS stands for "block storage segment". The memory reserved in BSS section is initialized to zero during the start of the program. Objects in BSS section have only a name and a size, and no value.
allocated space in BBS section
.lcomm varname, size
varname resb(w/d) size

Implementation of selection sort on an integer array
NASM
GAS
section .data
array db 89,10,67,1,4,27,12,34, 86, 3
  ARRAY_SIZE equ $ - array
  array_fmt db "  %d", 0
  usort_str db "unsorted array:", 0
  sort_str db "sorted array:", 0
  newline db 10, 0
section .text
   extern puts
   global _start
   _start:
      push  usort_str
      call  puts
      add   esp, 4
      push  ARRAY_SIZE
      push  array
      push  array_fmt
      call  print_array10
      add   esp, 12
      push  ARRAY_SIZE
      push  array
      call  sort_routine20
; Adjust the stack pointer
      add   esp, 8
      push  sort_str
      call  puts
      add   esp, 4
      push  ARRAY_SIZE
      push  array
      push  array_fmt
      call  print_array10
      add   esp, 12
      jmp   _exit
      extern printf
   print_array10:
      push  ebp
      mov   ebp, esp
      sub   esp, 4
      mov   edx, [ebp + 8]
      mov   ebx, [ebp + 12]
      mov   ecx, [ebp + 16]
      mov   esi, 0
   push_loop:
      mov   [ebp - 4], ecx
      mov   edx, [ebp + 8]
      xor   eax, eax
      mov   al, byte [ebx + esi]
      push  eax
      push  edx
      call  printf
      add   esp, 8
      mov   ecx, [ebp - 4]
      inc   esi
      loop  push_loop
      push  newline
      call  printf
      add   esp, 4
      mov   esp, ebp
      pop   ebp
      ret
   sort_routine20:
      push  ebp
      mov   ebp, esp
; Allocate a word of space in stack
      sub   esp, 4
; Get the address of the array
      mov   ebx, [ebp + 8]
; Store array size
      mov   ecx, [ebp + 12]
      dec   ecx
; Prepare for outer loop here
      xor   esi, esi
   outer_loop:
; This stores the min index
      mov   [ebp - 4], esi
      mov   edi, esi
      inc   edi
   inner_loop:
      cmp   edi, ARRAY_SIZE
      jge   swap_vars
      xor   al, al
      mov   edx, [ebp - 4]
      mov   al, byte [ebx + edx]
      cmp   byte [ebx + edi], al
      jge   check_next
      mov   [ebp - 4], edi
   check_next:
      inc   edi
      jmp   inner_loop
   swap_vars:
      mov   edi, [ebp - 4]
      mov   dl, byte [ebx + edi]
      mov   al, byte [ebx + esi]
      mov   byte [ebx + esi], dl
      mov   byte [ebx + edi], al
      inc   esi
      loop  outer_loop
      mov   esp, ebp
      pop   ebp
      ret
   _exit:
      mov   eax, 1
      mov   ebx, 0
      int   80h
.section .data
array: .byte 89,10,67,1,4,27,12,34, 86, 3
  array_end: .equ ARRAY_SIZE, array_end - array
  array_fmt: .asciz "  %d"
  usort_str: .asciz "unsorted array:"
  sort_str: .asciz "sorted array:"
  newline: .asciz "\n"
.section .text

   .globl _start
   _start:
      pushl $usort_str
      call  puts
      addl  $4, %esp
      pushl $ARRAY_SIZE
      pushl $array
      pushl $array_fmt
      call  print_array10
      addl  $12, %esp
      pushl $ARRAY_SIZE
      pushl $array
      call  sort_routine20
# Adjust the stack pointer
      addl  $8, %esp
      pushl $sort_str
      call  puts
      addl  $4, %esp
      pushl $ARRAY_SIZE
      pushl $array
      pushl $array_fmt
      call  print_array10
      addl  $12, %esp
      jmp   _exit

   print_array10:
      pushl %ebp
      movl  %esp, %ebp
      subl  $4, %esp
      movl  8(%ebp), %edx
      movl  12(%ebp), %ebx
      movl  16(%ebp), %ecx
      movl  $0, %esi
   push_loop:
      movl  %ecx, -4(%ebp) 
      movl  8(%ebp), %edx
      xorl  %eax, %eax
      movb  (%ebx, %esi, 1), %al
      pushl %eax
      pushl %edx
      call  printf
      addl  $8, %esp
      movl  -4(%ebp), %ecx
      incl  %esi
      loop  push_loop
      pushl $newline
      call  printf
      addl  $4, %esp
      movl  %ebp, %esp
      popl  %ebp
      ret
   sort_routine20:
      pushl %ebp
      movl  %esp, %ebp
# Allocate a word of space in stack
      subl  $4, %esp
# Get the address of the array
      movl  8(%ebp), %ebx
# Store array size
      movl  12(%ebp), %ecx
      decl  %ecx
# Prepare for outer loop here
      xorl  %esi, %esi
   outer_loop:
# This stores the min index
      movl  %esi, -4(%ebp)
      movl  %esi, %edi
      incl  %edi
   inner_loop:
      cmpl  $ARRAY_SIZE, %edi
      jge   swap_vars
      xorb  %al, %al
      movl  -4(%ebp), %edx
      movb  (%ebx, %edx, 1), %al
      cmpb  %al, (%ebx, %edi, 1)
      jge   check_next
      movl  %edi, -4(%ebp)
   check_next:
      incl  %edi
      jmp   inner_loop
   swap_vars:
      movl  -4(%ebp), %edi
      movb  (%ebx, %edi, 1), %dl
      movb  (%ebx, %esi, 1), %al
      movb  %dl, (%ebx, %esi, 1)
      movb  %al, (%ebx,  %edi, 1)
      incl  %esi
      loop  outer_loop
      movl  %ebp, %esp
      popl  %ebp
      ret
   _exit:
      movl  $1, %eax
      movl  0, %ebx
      int   $0x80

A program to read a string and display a greeting to the user
NASM
GAS
section .data
prompt_str db ‘Enter your name:’
STR_SIZE equ $ - prompt_str ;$ loca counter
greet_str  db  'Hello '
GSTR_SIZE  equ  $ - greet_str
section .bss
buff  resb  32 ; Reserve 32 bytes of memory

; A macro with two parameters
; Implements the write system call
   %macro write 2
      mov   eax, 4
      mov   ebx, 1
      mov   ecx, %1
      mov   edx, %2
      int   80h
   %endmacro
; Implements the read system call
   %macro read 2
      mov   eax, 3
      mov   ebx, 0
      mov   ecx, %1
      mov   edx, %2
      int   80h
   %endmacro
section .text
   global _start
   _start:
      write prompt_str, STR_SIZE
      read  buff, 32
; Read returns the length in eax
      push  eax
; Print the hello text
      write greet_str, GSTR_SIZE
      pop   edx
; edx  = length returned by read
      write buff, edx
   _exit:
      mov   eax, 1
      mov   ebx, 0
      int   80h
.section .data
prompt_str: .ascii "Enter Your Name: "
pstr_end: .set STR_SIZE, pstr_end - prompt_str
greet_str: .ascii "Hello "
gstr_end: .set GSTR_SIZE, gstr_end - greet_str
.section .bss
.lcomm buff, 32 //Reserve 32 bytes of memory

// A macro with two parameters
//  implements the write system call
   .macro write str, str_size
      movl  $4, %eax
      movl  $1, %ebx
      movl  \str, %ecx
      movl  \str_size, %edx
      int   $0x80
   .endm
// Implements the read system call
   .macro read buff, buff_size
      movl  $3, %eax
      movl  $0, %ebx
      movl  \buff, %ecx
      movl  \buff_size, %edx
      int   $0x80
   .endm
.section .text
   .globl _start
   _start:
      write $prompt_str, $STR_SIZE
      read  $buff, $32
// Read returns the length in eax
      pushl %eax
// Print the hello text
      write $greet_str, $GSTR_SIZE
      popl  %edx
// edx = length returned by read
   write $buff, %edx
   _exit:
      movl  $1, %eax
      movl  $0, %ebx
      int   $0x80