On working with LKMs Using Linux Kernel Modules to quickly export privileged kernel information to ordinary users.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
The ATA/IDE Interface Can we write a character-mode device driver for the hard disk?
Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati. Kernel Module Writing/Registering to /proc FS Payload of kernel module Reading CMOS Data – Real Time CMOS.
Creating a device-file node An introduction to some privileged Linux system-calls (needed for an upcoming programming exercise)
COSC 120 Computer Programming
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
How to make a pseudo-file As a follow-up to our first lab, we examine the steps needed to create our own ‘/proc’ file.
Firewall Lab Zutao Zhu 02/05/2010. Outline Preliminaries getopt LKM /proc filesystem Netfilter.
Computer System Overview
Rapid prototyping of LKMs Configuring our Linux systems for the quick creation and use of loadable kernel modules.
The ‘ioctl’ driver-function On implementing a way to query and modify our UART’s baudrate via the ‘device-file’ abstraction.
Midterm Tuesday October 23 Covers Chapters 3 through 6 - Buses, Clocks, Timing, Edge Triggering, Level Triggering - Cache Memory Systems - Internal Memory.
The PC’s Real-Time Clock An introduction to the capabilities and programming of the Real- Time Clock and CMOS memory.
CCNA 2 v3.1 Module 2.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Chapter 3 – Computer Hardware Computer Components – Hardware (cont.) Lecture 3.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
COMPONENTS OF THE SYSTEM UNIT
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
Computer Organization
System Calls 1.
Systems Software & Operating systems
Computer Maintenance Unit Subtitle: Basic Input/Output System (BIOS) Excerpted from 1 Copyright © Texas Education Agency, All.
Operating System. Architecture of Computer System Hardware Operating System (OS) Programming Language (e.g. PASCAL) Application Programs (e.g. WORD, EXCEL)
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Chapter 3: Operating-System Structures System Components Operating System Services System Calls System Programs System Structure Virtual Machines System.
DOS  In the 1980s or early 1990s, the operating system that shipped with most PCs was a version of the Disk Operating System (DOS) created by Microsoft:
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
CSC 322 Operating Systems Concepts Lecture - 25: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
MICROPROCESSOR INPUT/OUTPUT
Chapter 4 Storage Management (Memory Management).
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 3: Operating-System Structures System Components Operating System Services.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
UNIX Files File organization and a few primitives.
School of Computer Science & Information Technology G6DICP Introduction to Computer Programming Milena Radenkovic.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Guide To UNIX Using Linux Third Edition Chapter 8: Exploring the UNIX/Linux Utilities.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
UNIX Unit 1- Architecture of Unix - By Pratima.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Chapter 13 – I/O Systems (Pgs ). Devices  Two conflicting properties A. Growing uniformity in interfaces (both h/w and s/w): e.g., USB, TWAIN.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Computer Maintenance I
بسم الله الرحمن الرحيم MEMORY AND I/O.
Chapter Eight Exploring the UNIX Utilities. 2 Lesson A Using the UNIX Utilities.
FILE SYSTEM IMPLEMENTATION 1. 2 File-System Structure File structure Logical storage unit Collection of related information File system resides on secondary.
MINIX Presented by: Clinton Morse, Joseph Paetz, Theresa Sullivan, and Angela Volk.
Amdahl’s Law & I/O Control Method 1. Amdahl’s Law The overall performance of a system is a result of the interaction of all of its components. System.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
NON STANDARD HARDWARE By the end of this lesson you will be able to: 1. Identify non standard computer hardware 2. Understand ACRONYMS used to describe.
PCs ENVIRONMENT and PERIPHERALS Lecture 3. operating system and other system software that control the usage of the computer equipment application programs.
1 The user’s view  A user is a person employing the computer to do useful work  Examples of useful work include spreadsheets word processing developing.
Chapter 13: I/O Systems Modified by Dr. Neerja Mhaskar for CS 3SH3.
Operating System.
Computer Organization & Assembly Language Chapter 3
Operating System I/O System Monday, August 11, 2008.
CS703 - Advanced Operating Systems
Introduction to Computers
Operation System Program 4
Computers: Hardware and Software
Operating Systems Chapter 5: Input/Output Management
CS703 - Advanced Operating Systems
Chapter 2: Operating-System Structures
Chapter 2: Operating-System Structures
Computer Operation 6/22/2019.
Presentation transcript:

On working with LKMs Using Linux Kernel Modules to quickly export privileged kernel information to ordinary users

Privileged kernel information Users ordinarily are prohibited from seeing what goes on inside a running Linux kernel But we can use kernel modules to override normal restrictions on kernel data access The handiest mechanism for doing this is to employ the so-called ‘/proc’ file system Linux actually encourages this by offering quite a few examples, built in by default

The ‘/proc’ directory In addition to the normal files stored on a hard disk, UNIX systems support several types of ‘special’ files: –Directories and sub-directories (aka ‘folders’) –Symbolic links (i.e., placeholders for pointers) –Device files (providing access to ‘peripherals’) –‘Pseudo’ files (for data created ‘on-demand’) Such ‘pseudo’ files are usually in ‘/proc’

The ‘cat’ command This standard UNIX command offers users a quick way to view the text in a ‘/proc’ file It’s not necessary to write an application program that will open, read, and display the transient contents of a ‘pseudo’ file The file-concatenation operation transfers data from any file(s) to ‘standard output’ Example:$ cat /proc/version

More ‘/proc’ examples $ cat /proc/cpuinfo $ cat /proc/modules $ cat /proc/meminfo $ cat /proc/iomem $ cat /proc/devices $ cat /proc/self/maps [Read the ‘man-page’ for details: $ man proc ]

Our own ‘cat’ workalike A good technique for understanding how a standard command (like ‘cat’) really works is to re-implement your own version of it As an illustration of this principle, we have created a demo-program (‘mycat.cpp) You can copy it from our website to your own current working directory: $ cp /home/web/cruse/cs635/mycat.cpp.

Emulating ‘cat’ command

In-class exercise #1 Compile our ‘mycat.c’ application: $ g++ mycat.c -o mycat Then try using it: –(1) to view a normal text-file: $./mycat mycat.c –(2) to view a ‘/proc’ pseudo-file: $./mycat /proc/cpuinfo

Creating our own ‘/proc’ files We can write code to implement our own ‘pseudo’ files, located in ‘/proc’ directory We do this by adding a ‘payload’ function to a Linux Kernel Module, and by including calls to special kernel-functions within our module-init and our module-exit routines These special kernel-functions serve to ‘register’, and ‘unregister’, our payload

Our module’s organization get_info module_init module_exit The module’s two required administrative functions The module’s ‘payload’ function

The ‘get_info()’ callback When an application-program (like ‘mycat’) tries to read our pseudo-file, the kernel will call our ‘get_info()’ function, passing it four function arguments -- and will expect it to return an integer value: int get_info( char *buf, char **start, off_t off, int count ); pointer to a kernel buffer current file-pointer offset pointer (optional) to module’ own buffer size of space available in the kernel’s buffer function should return the number of bytes it has written into its buffer

The ‘sprintf()’ function The kernel provides a function you module can call to print formatted text into a buffer It resembles a standard C library-function: int sprintf( char *dstn, const char *fmt, ); pointer to destination formatting specification string list of the argument-values to format will return the number of characters that were printed to the destination-buffer int len = sprintf( buf, “count = %d \n”, count ); Example:

register/unregister Your module-initialization function should ‘register’ the module’s ‘get_info()’ function: create_proc_info_entry( modname, 0, NULL, get_info ); Your cleanup should do an ‘unregister’: remove_proc_entry( modname, NULL ); the name for your proc file the file-access attributes (0=default) directory where file will reside (NULL=default) function-pointer to your module’s ‘callback’ routine file’s name directory

Rapid prototyping We will write lots of LKM’s during the class For efficiency we’ve created some utilities: –‘newmod.cpp’ (it creates an LKM ‘skeleton’) –‘newinfo.cpp’ (it creates a ‘get_info()’ LKM) Helps to reduce LKM development-time – you just fill in the ‘skeleton’ with your own code for specific desired functionality These utilities are on our class website

Downloading CS 635 demos There are various ways you can download program-files from our course’s website Some are risky (e.g., ‘copy-and-paste’) as they may insert extra ‘invisible’ bytes Here’s one good way (‘fast’ and ‘clean’) if you are logged in to a classroom machine: $ cp /home/web/cruse/cs635/.

Creating a useful ‘/proc’ file The ‘get_info()’ function has full privileges! It executes inside the Linux kernel, where there is no enforced protection against accessing peripheral devices’ hardware The CPU communicates with devices by using the special ‘in’ and ‘out’ instructions A kernel header-file defines macros that let you avoid writing assembler language

Non-Volatile Memory The original IBM-PC had no internal clock Users had to run a utility program to reset the date and time after any system reboot That defect was eliminated in the IBM-AT A special battery-powered peripheral was added to keep track of the time and date It also provided a small amount of memory which would retain ‘configuration settings’

Motorola’s MC146818A PC-AT’s Real-Time Clock plus RAM was manufactured by Motorola Corporation Other companies have ‘cloned’ this chip Its capabilities are described online in an official datasheet by Dallas Semiconductor (see ‘Maxim’ integrated circuit: DS12887) You may also get the Motorola datasheet (by writing to its corporate headquarters)

Features of DS12887 Can operate over ten years without power Counts seconds, minutes, hours, days, day-of-the-week, date, month, and year (with leap-year compensation), valid up until the year 2100 AD, with options for 12/24-hour clock and Daylight Savings Can use binary or BCD representation Provides 114 bytes of nonvolatile storage

Programming Interface The RTC interfaces with system software as an array of 128 bytes, accessed via i/o ports 0x70 and 0x71 using a multiplexing scheme: port 0x70: address-port port 0x71: data-port Macros make it easy to access such ports: #include

Ten clock/calendar bytes Current seconds Alarm seconds Current minutes Alarm minutes Current hours Alarm hours Day-of-the-Week Date of the Month Current Month Current Year 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 Range is Range is or Range is 1..7 (Sunday=7) Range is Range is (January=1) Range is 0..99

Operating Capabilities The RTC can be programmed to generate an interrupt under any combination of the following three conditions: 1) time/date counters were updated 2) current time equals the alarm time 3) periodic frequency interval restarts The frequency of the periodic interrupt is a selectable rate (e.g., from 122 to 500ms)

Four Status/Control bytes UIPDivider bits SETPIEAIEUIESQWEDM24/12DSE IRQFPFAFUF 0000 VRT 0xA 0xB 0xC 0xD Rate-Select

Other NVRAM locations Besides these 14 dedicated RTC bytes, there are 114 byte locations which can serve as nonvolatile storage in whatever manner the system-designer decides IBM has established some ‘standard’ uses for many (but not all) of these locations A fairly complete CMOS Memory Map is accessible online (see course website) (CMOS means “complementary metal-oxide semiconductor”)

Example: Diagnostic Status Power Status failure Check Sum bad POST Config invalid RAM Size wrong Fixed Disk bad CMOS Time invalid reserved 0xE During the Power-On Self-Test, the ROM-BIOS routines perform tests of the memory and peripheral devices, and record any failures/errors in this Diagnostic Status byte

In-class exercise #2 Modify our ‘cmos.c’ module’s ‘get_info()’ function by introducing a for-loop so that, in addition to showing the current time, it will also display the full array of 128 bytes currently stored in the CMOS memory Here’s “C” code to read the N-th location: { intdatum;// storage for a CMOS data-value outb( N, 0x70 );// select cell number N for access datum = inb( 0x71 );// input value from selected location }