Ue18cs302 Os Unit5
Ue18cs302 Os Unit5
Unit 5
Aronya Baksy
December 14, 2021
1 I/O Management
• The I/O subsystem is the part of the kernel that handles control of various types and generations
of I/O devices and their interaction with the rest of the system.
• A device driver is a software abstraction layer between the OS and the actual hardware. It allows
the OS to access the I/O device functionalities in an uniform manner irrespective of the physical
nature of the device.
• Conflicting trends in I/O device design are increased standardization of h/w and s/w interfaces,
and an increasing variety of new I/O devices.
• Other common bus technologies are PCIe (e for Express) that offers upto 16 GBps throughput
and HyperTransport that offers upto 25 GBps throughput.
• An I/O Controller is a collection of electronics that operate a port/device/bus.
1
• Some bus architectures like serial have a simple controller (single chip that can control the signals
on the bus).
• More complex architectures like SCSI contain their own entire circuit boards that consist of proces-
sor, microcode instructions and some memory to process protocol messages. These circuti boards,
also known as host adaptors, plug directly into the computer.
• Some devices have their own controllers (eg: disks, that implement the device side of SCSI or SATA
protocol).
2 Polling
• Polling is a method used by host to perform I/O on a device.
• The busy bit in the status register indicates the current status of the I/O device (busy or idle)
• The command-ready bit in the control register indicates that the host is ready to send a command
to the device controller.
• The following steps take place in polling
1. The host reads the busy bit repeatedly until it is clear (0).
2. The host sets the write bit in the command register, and writes the byte of data into the
data-out register.
3. The host then sets the command-ready bit in the control register. The controller notices this
and sets the busy bit to 1.
4. The controller performs the I/O by seeing the write command from the control register and
the data to be written from the data-out register.
5. Once the I/O is complete the command-ready bit is cleared, the error bit in the status register
is cleared, and the busy bit is once again reset to 0.
2
• This loop is repeated for each byte of data to be written.
• In case there are long wait times for the device to be not busy, then the host might switch to
another task, which leaves no mechanism for it to know when the device is free.
• The process of servicing the device must be fast, or else data might be lost (eg: data coming in via
keyboard might overflow the buffer if it is not written to the device fast enough).
• The basic polling operations (read register, logical and for extracting a status bit, and branch if not
zero) are fast instructions, but become inefficient when done repeatedly for no immediate reward.
3 Interrupts
• An interrupt is a mechanism by which an I/O device requests service from the host, rather than
the host polling the device repeatedly.
• The interrupt request line is the method by which devices notify the CPU. As soon as a device
raises an interrupt on the request line, the CPU saves the current state, catches the interrupt and
dispatches it to the interrupt handler routine (ISR).
• The ISR determines the cause of the interrupt, handles it, and executes a return-from-interrupt
instruction that restores the state of the CPU. This is called clearing the interrupt.
• Additional features of Interrupt handling that are needed are:
1. Defer interrupt handling during critical processes
2. Multilevel interrupts to separate high and low priority interrupts
3. An efficient method of determining which device raised the interrupt (not polling)
• Maskable interrupts can be turned off by the CPU while it performs some critical operations.
Non-maskable interrupts cannot be turned off, and they normally indicate unrecoverable memory
errors.
• Interrupt hardware accepts an address which is an offset in the interrupt vector table that
contains addresses of interrupt handlers for specific types.
• In case of large number of devices and the associated large number of ISRs, chained interrupts
allow the IVT to index to a linked list of interrupts. Each time an entry in the IVT is referenced,
all the list entries are checked until one ISR is found in that list which can service the current
interrupt.
• An exception is an anomalous condition that requires special handling. Interrupts are used to
handle specific exceptions.
• A trap or a software interrupt is an interrupt that is generated by a program running in user
mode. It is used when an user program requests for service from the kernel via a system call.
• Upon receipt of a trap instruction, the interrupt hardware saves the state of the user code, switches
to kernel mode, and then dispatches to the kernel routine that implements that desired service.
• Multithreaded kernels are useful for handling multiple interrupt priorities. (eg: In Solaris, interrupt
handlers are implemented as kernel threads. Interrupt handling threads are issued high priorities,
higher than application (user) threads, and they can preempt one another.
3
• While the DMA uses the memory bus, the CPU cannot access main memory. This is referred to as
cycle stealing where CPU execution cycles are being used by the DMA. Despite this phenomenon,
DMA allows for better performance compared to non-DMA access.
• There is a tradeoff between protected and non-protected kernels whether to give direct access of
memory devices to user programs (ie. allow user programs to directly issue device commands,
instead of using privileged system calls to do the same).
• The tradeoff is between performance (the direct approach removes the overhead of kernel commu-
nication and context switching) and security (invalid access by user process can cause device errors
or system crash).
5. DMA reads one byte at a time and uses the memory bus to write to memory, increasing the memory
address and decreasing C.
6. When C is 0, meaning all data is now transferred to memory, DMA sends an interrupt to the CPU
signalling end of transfer.
4
• Cycle Stealing Mode: In this mode, the DMA controller forces the CPU to stop its operation
and relinquish the control over the bus for a short term to DMA controller. After the transfer of
every byte, the DMA controller releases the bus and then again requests for the system bus. In
this way, the DMA controller steals the clock cycle for transferring every byte.
• Transparent Mode: Here, the DMA controller takes the charge of system bus only if the processor
does not require the system bus.
– Read call to the descriptor of a file that has been opened previously.
– In case of input, if the data is available in the buffer then it is returned to the process and
the I/O request is completed.
– Else an I/O must be performed. The process is removed from the ready queue and put on the
wait queue for the device. Once the process is ready the I/O subsystem sends an I/O request
to the device driver.
– The device driver creates kernel memory space, and schedules the I/O. Eventually the device
driver issues a command to the device controller by writing to the control registers.
– The device controller operates the device hardware to perform the I/O.
– The DMA setup manages the data transfer. Once the transfer is complete, it sends an interrupt
to the CPU.
– The correct interrupt handler handles the interrupt, signals the device driver and returns from
the interrupt.
– The device driver receives this signal, checks to see which I/O request has completed and
notifies the kernel I/O subsystem of the completion of that request.
– The kernel transfers data or return codes to the address space of the requesting process and
moves the process from the wait queue back to the ready queue
– The process is now unblocked, and once it gets access to the CPU it resumes execution.
6 Protection
6.1 Goals of Protection
• To ensure that intentional access violations do not cause harm to the functioning of the system.
• Provide a means to distinguish between authorized and unauthorized usage. This is done by detect-
ing hidden errors at the interfaces between components of the OS, which can lead to contamination
by a malfunctioning component.
• Provide a mechanism that ensures that policies that govern resource usage are enforced, and allow
for flexibility in policy creation (either in the system design, or by the management of the system,
or by individual users for their own files).
5
• These policies are implemented both by the OS and the application programs, to guard the resources
that are used by the application programs.
• Separation of policy and mechanism allows for multiple policies to be implemented flexibly.
• Domains are associated to process either statically (once assigned cannot be changed) or dynami-
cally (process can move between domains). If this association is static, then a mechanism must be
there to change the content of a domain
• eg: An object O1 , process P1 needs read access to O1 in one phase and write access to it in the
next. The domain in which P1 is running cannot have both read and write at all times as per the
principle of least privilege).
• In case of dynamic association between process and domain, a mechanism for domain creation and
domain switching is used for the above.
• Each user may be a domain, each process may be a domain, or each procedure is a domain.
6
• In order to enforce protection, the privileged programs may be placed in a single directory owned
by root. This prevents intruders from hiding privileged programs with setuid 1 in random locations
for later use.
• Another protection technique is to prevent userID from changing on the fly. All privileged access
must happen via a single daemon process that itself has root as its userID.
7 Access Matrix
• The general model of protection involves a matrix where rows are domains and columns are objects.
This is called the access matrix.
• Each element of the matrix is a set of operations that the domain i can perform on object j.
• The access matrix also includes columns for domains. The entry A(Di , Dj ) is whether a process in
domain i is allowed to switch to domain j.
• If one of the operations in the entry A(Di , Oj ) has copy privilege (marked as *), then it can copy
that operation to any of the other domains only for the resource Oj .
• Either the copy can be implemented as a propagate (ie. copy to new domain then remove from
original), or it can be a single limit copy (ie. the new domain gets a non-copiable right).
• If A(Di , Oj ) has an owner privilege, then it can add or remove any access right of any domain
in that resource Oj .
• If A(Di , Dj ) has a control privilege, then Di can remove any access right from the entire row of
Dj .
7
7.1.2 Access List for Objects
• Each object has an access list with tuples of the form hDomain, Access rights list i.
• This design also allows a default set of operations apart from the list that is defined as part of the
domain. This default set is checked if the tuple Di , Ri does not contain the requested operation.
• The capability is not accessible in the address space of all processes working in a domain. It lies
in a separate protected memory, that is created by the OS.
• Protection for the capability list is enforced using:
– A tag that identifies a capability list as against normal data. Hardware or firmware protection
is used to ensure that no process can modify the tag bits. Multiple tag bits allow the OS to
further identify other types of data as well.
– Memory is segmented into a space for data and another space for capability list that can only
be accessed by the OS. Segmentation of memory implements this easily.
• Capability lists are efficient from a process point of view, but revocation of capabilities is inefficient.
• Lock and Key is a compromise between access list and capability list. The keys can be passed
freely from domain to domain. In addition, access privileges can be effectively revoked by the
simple technique of changing some of the locks associated with the object.
8
8.1 Revocation
• Immediate vs delayed. If delayed, when will revocation take place?
• Selective vs general. If an access right for an object is revoked, does it affect all users who have an
access right to that object, or can a subset of users be specified?
• Partial vs total. Can only a subset of rights be revoked for an object, or must all the rights be
revoked for it?
• Permanent vs temporary revocation.
9 Security
Secure system is one that only allows resources to be used legally and in the intended manner, under all
circumstances irrespective of the malicious (or otherwise) intent of the user.
• Replay Attack: Repeat of a legal communication in order to illegally get access to resources. It
can be done as an exact replica, or can be done with message modification to extract further higher
privileges.
9
• Man-in-the-middle Attack: A third party intercepts the current ongoing communication session
using session hijacking, and poses as the sender to the receiver, thus listening to all information
exchanged.
• Phishing: A portal that tricks legitimate users into giving up confidential information without
their conscious knowledge of it being used for malicious purposes.
10 Program Threats
10.1 Trojan Horse
• A code segment that misuses it’s environment is called a Trojan Horse.
• Many systems have mechanisms for allowing programs written by users to be executed by other
users.
• If these programs are executed in a domain that provides the access rights of the executing user,
the other users may misuse these rights.
• Spyware is a variation of Trojan horse software wherein such software comes bundled with user
applications.
• The role of spyware is to display ads, or open pop up windows or capture information from the
user’s system and send it back to a central site.
• Such attacks are generally classified as covert channel attacks.
10
10.5 Viruses
• A virus is a self-replicating fragment of code embedded in a legitimate program, that is used to
”infect” a computer system.
• Virus Dropper is a software that inserts a virus into a system. It is commonly implemented in
the form of a Trojan Horse.
• Categorization of Viruses:
– File Virus: A standard file virus infects a system by appending itself to a file. It changes the
start of the program so that execution jumps to its code. After it executes, it returns control
to the program.
– Boot Sector Virus: Loads at every boot time, infects other devices that are also loaded at
boot time.
– Macro Virus: Virus written in a high-level language that runs along with high-level user
applications.
– Source-Code Virus: A source code virus looks for source code and modifies it to include
the virus and to help spread the virus.
– Polymorphic: One that changes its form each time it is installed, in order to avoid detection
by anitvirus software. This is done by changing not the functionality but the signature of the
virus.
– Encrypted Virus: In order to avoid detection, the virus is installed in encrypted form and
decrypted (with another installed decryption code) before execution.
– Stealth Virus: Attempts to avoid detection by modifying parts of the system that could be
used to detect it. For example, it could modify the read system call so that if the file it has
modified is read, the original form of the code is returned rather than the infected code
• This is mainly done by reducing openness, ie. number of available services and functionalities.
11.1 Worm
• A worm consists of a grappling hook (aka vector) program and a main program.
• The job of the grappling hook is to connect to the original system where it was uploaded, and copy
the main program to that machine. It effectively spreads the main program to as many targets as
possible.
• Morris’ worm is one popular example of a worm program (written in 1998, used to infect computers
on the internet).
• The grappling hook programs were propagated using three main tools: the rsh tool, the finger
tool and the sendmail tool.
11
11.1.2 Finger attack
• The finger tool gives information about a specific host on the internet (like username, real name,
phone no. etc).
• The worm executed a buffer-overflow attack on finger by sending it a a 536-byte string crafted to
overflow the buffer.
• Instead of returning to the main routine where it resided before the worm’s call, the finger daemon
was routed to a procedure within the invading 536-byte string now residing on the stack.
• The new procedure executed /bin/sh, which, if successful, gave the worm a remote shell on the
machine under attack.
• Morris included in his attack arsenal a call to debug that issued a set of commands that mailed
and executed a copy of the grappling-hook program.
• As the worm spread to a new system, it checked the system for a copy of itself. If found, the worm
would exit, except in every 7th instance.
• This was done as a means to avoid baiting with fake copies of the worm program.
• Utilities such as nmap are used for this purpose. When pointed at a target, it will determine
what services are running, including application names and versions, the host operating system,
and information about defenses, such as what firewalls are defending the target.
12