Docsity

Deep Dive into Memory Protection & Access Control in Computer Security: 80386 Mode - Prof., Study notes of Computer Science

Toggle sidebar
bg1
CIS/CSE 785: Computer Security (Syracuse University) 80386 Protection Mode: 1
80386 Protection Mode
1 Introduction and Initial Discussion
Question 1: Assume that to write to /etc/passwd file, the CPU instructions (Machine codes) that get
executed are c1,c2,...,cn. And also assume that the instructions related to access control is a1,...,as.
Now let’s construct a new program p′=c1,...,cn-a1, ..., as, and let run it directly on CPU, can we
succeed in writing to /etc/passwd file?
Answer: In 8086, you can do this. In 80386, you cannot!
Question 2: Assume that we know the address of the code for system calls write(), which can write
data to disks. There are two ways to call it:
1. Go through the system-call approach, which is subject to access control.
2. Directly jump to that code.
We know the first approach works, but can the second choice succeed? If not, what prevents a program from
jumping to that code?
Answer: the hardware access control disallows it. There is a security policy to prevent the above direct
jumping from happening, and the policy is enforced by hardware. We are interested in how such access
control policy is defined and how hardware enforces the policy.
Discussion: From the above two questions and their answers, it seems certain kind of access control is
protecting the systems. If you get chance to design such a protection scheme, how would you design an
access control like this?
•Three components of a security policy: subject, object, and rules.
•Memory address can be used as object (this is quite straightforward because we are protecting mem-
ory). However, at what granularity, each byte, word, block? What are the disadvantages and advan-
tages of your choices?
•What can be used as subject?
–Can we use user ID or process ID as subjects? No we cannot use things that are defined in
an operating system, because this access control is not part of an OS, it is underneath an OS.
Processes and users are meaningful in an OS, but the underlying hardware does not know what
those are.
–In other words, how to give each instruction an identity?
•How to design the rules (or policies)?
–How to represent the policies?
–Where to store the policies?
April 21, 2008
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Deep Dive into Memory Protection & Access Control in Computer Security: 80386 Mode - Prof. and more Study notes Computer Science in PDF only on Docsity!

80386 Protection Mode

1 Introduction and Initial Discussion

Question 1: Assume that to write to /etc/passwd file, the CPU instructions (Machine codes) that get executed are c 1 , c 2 ,.. ., cn. And also assume that the instructions related to access control is a 1 ,.. ., as. Now let’s construct a new program p′^ = c 1 ,.. ., cn - a 1 , ..., as, and let run it directly on CPU, can we succeed in writing to /etc/passwd file?

Answer: In 8086, you can do this. In 80386, you cannot!

Question 2: Assume that we know the address of the code for system calls write(), which can write data to disks. There are two ways to call it:

  1. Go through the system-call approach, which is subject to access control.
  2. Directly jump to that code.

We know the first approach works, but can the second choice succeed? If not, what prevents a program from jumping to that code?

Answer: the hardware access control disallows it. There is a security policy to prevent the above direct jumping from happening, and the policy is enforced by hardware. We are interested in how such access control policy is defined and how hardware enforces the policy.

Discussion: From the above two questions and their answers, it seems certain kind of access control is protecting the systems. If you get chance to design such a protection scheme, how would you design an access control like this?

  • Three components of a security policy: subject, object, and rules.
  • Memory address can be used as object (this is quite straightforward because we are protecting mem- ory). However, at what granularity, each byte, word, block? What are the disadvantages and advan- tages of your choices?
  • What can be used as subject? - Can we use user ID or process ID as subjects? No we cannot use things that are defined in an operating system, because this access control is not part of an OS, it is underneath an OS. Processes and users are meaningful in an OS, but the underlying hardware does not know what those are. - In other words, how to give each instruction an identity?
  • How to design the rules (or policies)? - How to represent the policies? - Where to store the policies?

- When to enforce the policies? - Access matrix: high cost, inflexible, etc.

  • Mandatory versus Discretionary Access Control - If MAC is used, system-wise mandatory access control polices are enforced. - If DAC is used, the owner of an object can set up security polices. - 80386 Protection Mode chose MAC. ∗ It is believed that mandatory access control is better for highly secure systems. ∗ The implementation of DAC scheme might be inefficient. Note that such access control policy is checked for each memory access, so it must be very fast.
  • In MAC, objects and subjects are assigned security labels, and access control policies are defined based on these labels. What can be used as labels and where to put them?

2 The Ring Architecture and Segments

  • History - Late 70’s: 8086, Real Mode and has no protection. - 1982: 80286, Real Mode and 16b Protected Mode. - 1985: 80386, Real Mode and 32b Protected Mode.

Operating System Kernel

Applications

Operating System Services

Protection Rings

Level 0

Level 1 Level 2 Level 3

Figure 1: Ring Levels of 80386

  • The Ring architecture: the labels used by MAC (see Figure 5). - 80386 has four rings. Each ring is associated with different privileges. Ring 0 is the most privileged ring, and the OS kernel usually resides within this layer. - Each object and subject is associated with a label, called ring. This label is used as the subject in access control policies.

∗ Index of Capabilities : when a code tries to access a memory, it should present a “ticket”, which is the index of the actual capability stored in a privileged ring. This way, the proces- sor only needs to check this specific capability. The performance is much better than the capbility list approach. This is similar to how the file descriptor is implemented.

- 80386 chooses the capability as its access control model to achieve memory isolation; it uses the index approach.

  • Logical and Linear Address (see Figure 2) - Logical address: consist of segment selector and offset. The processor converts the logical address to linear address using the segment descriptor indicated by the segment selector. - Linear address: when the paging is disabled, the linear address is actually the physical address; when the paging is enabled, the linear address is converted to physical address through paging mechanisms. - Segment selectors are provided by segment registers: For example, in the following instruction, the segment selector is provided by the register DS, and the offset is 80 : MOV %DS:[80], %EAX.

Linear Address

Descriptor

Segment

15 0 Seg. Selector

Descriptor Table

Logical Address

Capability−Based Access Control (Selecting Segment)

Mandatory Access Control: (Privilege Check)

31 0 Offset

31 0

Figure 2: Logical Address to Linear Address

- Two access control mechanisms are used here: ∗ Capability-based Access Control: the segment selector and segment descriptor are actually the capability concept. The segment descriptor is the capability, while the segment selector is the index to the descriptor. Segment selectors are accessible to user programs, but not segment descriptors. ∗ Mandatory Access Control: even if a process has a capability, its access right is further restricted by another level of access control that is based on MAC. This level of access control ensures that ring based access policies are enfored. ∗ Note: one might wonder whether the second-level of access control is redundant; if the access is not allowed, why bother to create a capability (descriptor) for a process at the first place? The answer will be partially clear when when talk about Global Descriptor Table (GDT) , which contains capabilities shared by all processes; however, whether a process can use a capability in GDT depends on its privilege level.

  • Segment Selector (see Figure 3) - TI : Table Indictor. Indicate whether GDT or LDT is used. - Index : The processor multiplies the index by 8 (the number of bytes in a segment descriptor), and add the result to the base address of the GDT or LDT based on the TI value (the base addresses are stored in the GDTR or LDTR register, respectively). ∗ GDT : Global Descriptor Table. Each system must have one GDT defined. ∗ LDT : Local Descriptor Table. One or more LDT can be defined. For example, an LDT can be defined for each task being run, or some or all tasks can share the same LDT. - RPL : Request Privilege Level. Specifies the privilege level of the selector. We will explain this later.

T Index (^) I RPL

(^153 2 1 )

GDT^ LDT

TI=0^ TI=

GDTR Register LDTR Register

Segment Selector

TI: Table Indicator

Figure 3: Segment Selector

  • Segment Descriptor (see Figure 4) - Base (32 bits): the base address of the segment. - Segment Limit (20 bits): the size of the segment. The processor will ensure that the offset of the address does not go beyond the segment limit. - Type (4 bits): specify the type of segment. The processor will enforce type rules. For example, no instruction may write into a data segment if it is not writable, no instruction may read an executable segment unless the readable flag is set, etc. ∗ Data Type: Read-Only, Read/Write, etc. ∗ Code Type: Execute-Only, Execute/Read - DPL (2 bits): Descriptor Privilege Level. It specifies the ring level of the segment. DPL is used in access control.
  • Segment Registers - Due to the address translation step, accessing data or code in memory involves two memory ac- cess, one for retrieving segment descriptor from the descriptor table, and the other for accessing the actual memory. To avoid consulting a descriptor table for each memory acess, 80386 caches information from descriptors in segment registers.

- Potential Risk : At ring 0, code can access data at any ring level. This poses a risk when the code (say A) is invoked by some other code (say B) in a less privileged ring, and B passes a pointer to A. Normally, the pointer refers to a memory space that belongs to B (and of course A can also access). However, if B is malicious, B can pass a pointer of a memory that does not belong to B (B does not have privileges to access the memory). Because A is a privileged code, access control cannot prevent A from accessing the memory. This way, B can use A to corrupt the targeted memory in a privileged space. - Principle of Least Privilege : in the above case, it is really unnecessary to run A with the ring- privilege when accessing the pointed memory passed by B. According to the principle of least privilege, A should drop its privilege to B’s ring level when accessing the memory. - How does RPL works: ∗ Assume that A is in ring 0 and B is in ring 3, and the memory address’s selector is S. ∗ The last two bit of a selector is used for RPL. It means that when accessing this memory, the code’s privilege is droped to the RPL level. Therefore, if S’s RPL=3, when A tries to access the memory in ring 0 (i.e. DPL=0), the access will be denied. If S’s RPL is not dropped to 3 (instead it is set to 0 ), the access will succeed because A’s CPL is 0. ∗ In other words, max(RP L, CP L) is actually used for access control. ∗ RPL is usually less than or equal to CPL. ∗ If RPL = 0, RPL will have no effect. - Policy of access control : max(CPL, RPL) ≤ DPL of data segment.

Code−Segment Descriptor

CS Register

Segment Selector For Data Segment

CPL

RPL PrivilegeCheck

Data−Segment Descriptor

CS Register

Segment Selector

CPL

RPL PrivilegeCheck

DPL (^) DPL C

(a) Privilege Check for Data Access (b) Privilege Check for Control Transfer Without Using a Gate

For Code Segment

Figure 5: Privilege Check for Data Access and Code Transfer (without using Gates)

  • Privilege Check for Control Transfer without Using a Gate - Policy: ∗ For non-comforming segment: transfer is allowed when CPL = DPL. ∗ For comforming segment: trasfer is allowed when CPL ≥ DPL. ∗ RPL does not have much effect here. - Why can’t we access code with a higher DPL (i.e., lower privilege)? ∗ Possible reason 1: It is easy to jump (lower the CPL) to the code with higher DPL, but it is difficult to return back, because on returning, we jump from a lower privileged ring to a higher privileged ring. This violates the mandatory access control policy.

∗ Possible reason 2: Another reason is the data access. If a code A jumps to another code B at a lower privilege level, B cannot access A’s data because the data are most likely in A’s ring level. ∗ Possible reason 3: Is there really a need to allow jumping from a higher privilege to a lower privilege?

- Why can’t we jump to code with a lower DPL (i.e., higher privilege)? ∗ For security reasons, we cannot do this. ∗ Is this type of jump necessary? Yes, we definitely need this. For the device driver code is usually in a privileged ring. User-level program should be able to jump to the device driver code somehow. ∗ How can we achieve jumping to lower DPL? Gates are designed for this purpose. We will talk about gates later.

  • The conforming bit - Permits sharing of procedures, so they can be called from various privilege levels. - Usually for math library and exception handlers. - If you have a procedure that you want everybody to be able to call it, where do you put it? in which ring? ∗ Ring 3: ring 0,1,2 cannot call it. ∗ Ring 0: ring 1,2,3 cannot call it. ∗ Ring 0 and call gate: you need to build a call gate for each library call. - The conforming segment mechanism permits sharing of procedures that may be called from various privilege levels but should execute at the privilege level of the calling procedure. When control is transferred to a conforming segment, the CPL does not change.

4 Call Gates

  • Supporting system calls : In an operating systems, all the privileged operations are carried out in the privileged rings, such as modifing kernal data structure, interacting with devices, etc. OS does not allow user programs to invoke them in an arbitrary way, such as jumping to the middle of a privileged operations. Instead, OS provides a number of interfaces to users programs, which can only invoke those provileged operations via the interfaces. These interfaces are often called system calls. Invoking system calls is quite different from invoking a normal function. In the latter case, the call is within the same ring; however in the former case, the call is from a less privileged ring to a prvileged ring. 80386’s ring protection does not allow a direct jump like this. Some special mechanism must be provided to allow the control transfer from a less privileged ring to a privileged ring.
  • How to invoke system calls? - Call Gate : Call gates allow a program to directly call system calls. However, since system calls are often in a privileged ring, calling them directly is not allowed because of the ring protection. The 80386 protection mode uses a call-gate concept to allow this kind of transfer. Call gates enable programs in a lower privileged ring to jump to designated places in a higher privileged ring.

Descriptor Table

Offset

Base

Base Base

Offset Seg Selector Offset

Call−Gate

Descriptor

Descriptor

Code−Segment

Seg. Selector

Procedure Entry Point

Far Pointer to Call Gate

(Required but not used by processor)

Figure 6: Call-Gate Mechanism

P

31 24 20 16 0

31 16 0

DPL

8

Offset in Segment 15:

0

Segment Selector

Offset in Segment 31: 1 1 0 0

Type 0 0 0

Parameter Count

4

Figure 7: Call-Gate Descriptor

- A far return that requires a privilege-level change is only allowed when returning to a less privi- leged level

5 Miscellaneous

  • How to change rings? - The privilege level can only be changed by POPF, which has to be executed at level 0.
  • Protecting descriptor tables. - GDT, LDT, IDT are very important. They contain the followings. ∗ Call gates ∗ Code Segment Descriptors ∗ Data Segment Descriptors - These tables should be in a protected memory - The GDTR, LDTR, and IDTR registers (they store the locations of these tables) can only be set by privileged code.
  • I/O Protection - Introduction Question: everything can be boiled down to I/O operations. Is Direct I/O from ring 3 possible? - How to prevent any arbitrary code from conducting I/O operations? - Instructions: IN, INS, OUT, OUTS - Policy: CPL ≤ IOPL - IOPL is stored in EFLAGS, can only be modified by POPF instruction - POPF can only be executed in ring 0 - This way, the security sensitive I/O operations can only be conducted by the code in the privi- leged rings (e.g. ring 0).