Linux Kernel Modules - Advanced System Programming - Lecture Slides, Slides of Computer Applications

Main points are: Linux Kernel Modules, Ordinary Users, Privileged Kernel Information, Running Linux Kernel, Unix Systems, Standard Unix Command, Emulating ‘Cat’ Command, Module’s Organization, Integer Value, Rapid Prototyping

Typology: Slides

2012/2013

Uploaded on 04/17/2013

pamelaaaa
pamelaaaa 🇮🇳

4.5

(12)

103 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
On working with LKMs
Using Linux Kernel Modules to
quickly export privileged kernel
information to ordinary users
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Linux Kernel Modules - Advanced System Programming - Lecture Slides and more Slides Computer Applications in PDF only on Docsity!

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 ‘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 ]

Emulating ‘cat’ command

In-class exercise

  • 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

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 Docsity.com

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

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’

Features of DS

  • 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 <asm/io.h>