Friday, May 06, 2005

Inserting data into the parent process

On Linux, and other UNIX-like systems that support it, you can make an ioctl call to insert data into your standard input file. That's pretty neat in an of itself, but it gets better: in most cases, you share the standard input of your parent process. What that means is that you can insert data into that file, and then terminate... and the parent process will read those data as user input.

The following Python snippet will, when run from a command shell, cause that shell to print a file listing of the current directory.

#! /usr/bin/env python
from fcntl import ioctl

TIOCSTI = 0x5412

ioctl(1, TIOCSTI, 'l')
ioctl(1, TIOCSTI, 's')
ioctl(1, TIOCSTI, '\n')

This trick has security implications, and is one reason why programs that run as root should never pass their open file handles on to their children.


Blogger Grig Gheorghiu said...

Very neat. I'm curious in what context you used this trick.

8:34 AM  
Blogger Daniel Arbuckle said...

I was writing a chording driver for an n52 gamepad, to make it usable as a full keyboard.

I didn't actually go with this method, but I thought y'all might get a kick out of it.

9:28 AM  

Post a Comment

<< Home