P. 1
Kernel Notes

Kernel Notes

|Views: 389|Likes:
Published by rash5377

More info:

Published by: rash5377 on Nov 01, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/06/2012

pdf

text

original

We will now make the Linux kernel execute Python byte code. The general idea is this -
our load_py function will recognize a Python byte code
le - it will then attempt to load the
Python interpreter (/usr/bin/python) with the name of the byte code
le as argument. The
loading of the Python interpreter, which is an ELF
le, will of course be done by the kernel
module responsible for loading ELF
les (fs/binfmt_elf.c).

Example 12-2. Executing Python Byte Code

1
2 static int load_py(struct linux_binprm *bprm,
3 struct pt_regs *regs)
4 {
5 int i, retval;
6 char *i_name = PY_INTERPRETER;
7 struct file *file;
8 if(is_python_binary(bprm)) {
9 remove_arg_zero(bprm);
10 retval = copy_strings_kernel(1, &bprm- filename, bprm);
11 if(retval 0) return retval;
12 bprm- argc++;
13 retval = copy_strings_kernel(1, &i_name, bprm);
14 if(retval 0) return retval;
15 bprm- argc++;
16 file = open_exec(i_name);
17 if (IS_ERR(file)) return PTR_ERR(file);
18 bprm- file = file;
19 retval = prepare_binprm(bprm);
20 if(retval 0) return retval;

84

Chapter 12. Executing Python Byte Code

21 return search_binary_handler(bprm, regs);
22 }
23 return -ENOEXEC;
24 }
25

Note: The author’s understanding of the code is not very clear - enjoy exploring on your
own!

The parameter bprm, besides holding pointer to a buffer containing therst few bytes of the
executable
le, also contains pointers to memory areas where the command line arguments to
the program are stored. Lets visualize the command line arguments as being stored one above
the other, with the zeroth command line argument (which is the name of the executable) com-
ing last. The function remove_arg_zero takes off this argument and decrements the argument
count. We then place the name of the byte code executable
le (say a.pyc) at this position
and the name of the Python interpreter (/usr/bin/python) above it - effectively making the
name of the interpreter the new zeroth command line argument and the name of the byte code
le therst command line argument (this is the combined effect of the two invocations of
copy_strings_kernel).

After this, we open /usr/bin/python for execution (open_exec). The prepare_binprm function
modi
es severalelds of the structure pointed to by bprm, like buf to reect the fact that we
are attempting to execute a different
le (prepare_binprm in fact reads in therst few bytes
of the new
le and stores it in buf - you should read the actual code for this function). The
last step is the invocation of search_binary_handler which will once again cycle through all
the registered binary formats attempting to load /usr/bin/python. The ELF loader registered
with the kernel will succeed in loading and executing the Python interpreter with the name of
the byte code
le as therst command line argument.

85

Chapter 12. Executing Python Byte Code

86

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->