This file is indexed.

/usr/sbin/realksh.c is in binfmtc 0.17-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*BINFMTC: -lreadline -I/usr/include/readline
exit 1

 *  binfmt_misc Kernel Module C Interpreter
 *  Copyright (C) 2005,2006 Junichi Uekawa
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Real K shell implemented in binfmtc.
 *
 *
 * M-x compile:
 * gcc -S -c realksh.c -I /usr/include/readline -Wall -O2
 */
#define _GNU_SOURCE

#include <assert.h>
#include <history.h>
#include <readline.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/klog.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>


#define PRGNAME "realksh"

typedef struct defs_list
{
  struct defs_list* next;
  char* s;
} * defsp;


/*
   Add a new string to list of "#" lines.
   #include, #define etc.
 */
defsp add_string(defsp orig, const char* newstr)
{
  defsp t = malloc(sizeof(struct defs_list));
  t->next=orig;
  t->s=strdup(newstr);
  return t;
}


/*
 * Clear kernel 'ring-buffer' so that we don't need to see it.
 */
void clear_kernel_ring_buffer(void) {
  // clear kmsg ring buffer
  klogctl(5, NULL, 0);
}

/*
  Dump kernel message
 */
void dump_kernel_message(void)
{
  // arbitrary size of buffer which hopefully will fit anything
  // that your code will dump.
  //
  // NB: This makes this code non-reentrant.
  static char log_buffer[ 64 * 1024 ];

  // read ring buffer and do something.
  int read_size = klogctl(4, log_buffer, sizeof(log_buffer));
  if (read_size > 0) {
    write(1, "KMSG: ", 6);
    write(1, log_buffer, read_size);
    write(1, "\n:", 1);
  }
}

/*
   get the path to kernel build directory from /lib/modules/KVER/build/

   return NULL on failure.
*/
char* get_kerneldirname(void)
  {
    struct utsname u;
    char* kerneldirname;

    if (-1==uname (&u))
      {
	perror(PRGNAME": uname");
	return NULL;
      }
    if (asprintf (&kerneldirname, "/lib/modules/%s/build",
		  u.release)<0)
      {
	fprintf(stderr, PRGNAME ": asprintf failed\n");
	return NULL;
      }
    return kerneldirname;
  }

int main(int argc, char** argv)
{
  int ret;
  char * str = NULL;
  char * tempdirname = NULL;
  char * tempfilename = NULL;
  char * commandline = NULL;
  char * kerneldirname = NULL;
  char * kbuildfilename = NULL;

  /* the descriptions can be anything, since we aren't really using it for anything important */
  const char* module_author = "joe random realksh user";
  const char* module_description = "auto-generated code fromm realksh.c" ;
  const char* module_license = "GPL" ;

  /* the name should not
     duplicate what's already existing within kernel. */
  const char* modulename = "realkshmod2";

  FILE * f;
  defsp t, defs=NULL;

  /*
     initialize the header file list.  This is the minimal list that
     would enable basic module functionality.

     This list also happens to allow most kernel operations, like
     printk, and mfspr (ppc) etc.

     Add lines here to improve default for your liking.
   */
  defs=add_string(defs, "#include <linux/init.h>");
  defs=add_string(defs, "#include <linux/module.h>");

  /* obtain the kernel build directory */
  if (!(kerneldirname=get_kerneldirname()))
    return 1;

  /* prepare the temporary module build directory */
  if (asprintf(&tempdirname, "%s/realkshXXXXXX",
	   getenv("BINFMTCTMPDIR")?:
	   getenv("TMPDIR")?:
	   getenv("TEMPDIR")?:
	   "/tmp"
	   )<0)
    {
      fprintf(stderr, PRGNAME ": asprintf failed\n");
      return 1;
    }
  if (!mkdtemp(tempdirname))
    {
      perror (PRGNAME": mkdtemp");
      return 1;
    }

  asprintf(&kbuildfilename, "%s/Kbuild",
	   tempdirname);
  asprintf(&tempfilename, "%s/%s-main.c",
	   tempdirname,
	   modulename);
  f=fopen (kbuildfilename, "w");
  fprintf(f, "obj-m:=%s.o\n"
	  "%s-y:=%s-main.o\n",
	  modulename,
	  modulename, modulename);
  fclose(f);

  /*
     check that I have root permissions.
     insmod/rmmod, and /proc/kmsg requires root.

     You shoulnd't be running this code on a production system, or
     you're pretty much already screwed anyway
   */
  if (getuid()!=0)
    {
      fprintf(stderr, PRGNAME ": Warning: root privilege is probably required for most operation\n");
    }

  clear_kernel_ring_buffer();

  /*
     The main interactive command loop.
  */
  while (NULL!=(str = readline("REAL ksh: ")))
    {
      if (*str=='\0')		/* ignore blanks lines, they clutter history. */
	{
	  free(str);
	  continue;
	}
      add_history(str);

      /*
	 ## = dump list
	 # ... = add this line. e.g. #include <.....h>
      */
      if (*str=='#')
	{
	  /* ## debug symbol to dump header file list. */
	  if (*(str+1)=='#')
	    {
	      printf("List of items in the # list:\n");
	      for (t=defs; t; t=t->next)
		{
		  printf("%s\n", t->s);
		}
	      free(str);
	      continue;
	    }
	  defs=add_string(defs, str);
	  free(str);
	  continue;
	}

      /*
	 creation of module source-code to be processed
       */
      f=fopen(tempfilename, "w");
      for (t=defs; t; t=t->next)
	{
	  fprintf(f, "%s\n", t->s);
	}
      fprintf(f,
	      "MODULE_AUTHOR(\"%s\");\n"
	      "MODULE_DESCRIPTION(\"%s\");\n"
	      "MODULE_LICENSE(\"%s\");\n"
	      "static int __init %s_init(void)\n"
	      "{\n"
	      "%s;\n"
	      "return 0; \n"
	      "}\n"
	      "static void __exit %s_cleanup(void)\n"
	      "{\n"
	      "}\n"
	      "module_init(%s_init);\n"
	      "module_exit(%s_cleanup);\n"
	      ,
	      module_author,
	      module_description,
	      module_license,
	      modulename,
	      str,
	      modulename,
	      modulename,
	      modulename);
      fclose (f);

      /* build and execute */
      asprintf(&commandline,
	       "cd %s && make -s -C \"%s\" M=\"%s\" && insmod %s.ko",
	       tempdirname, kerneldirname,
	       tempdirname, modulename);
      if ((ret=system (commandline)))
	{
	  fprintf(stderr,
		  PRGNAME
		  ": non-zero return code from make/exec command: %i: %s\n",
		  ret, commandline);
	}
      free(commandline);

      /* clean-up module regardless of failure or success */
      asprintf(&commandline,
	       "rmmod %s.ko",
	       modulename
	       );
      if ((ret=system (commandline)))
	{
	  fprintf(stderr, PRGNAME ": non-zero return code from rmmod: %i\n", ret);
	}
      free(commandline);
      free(str);

      /* yield here, to let the kernel do what it wants to do. I want
	 output to come out. */
      sched_yield();
      sched_yield();
      dump_kernel_message();
    }

  /* After ctrl-D, you will come here, do a clean-up
   */
  printf ("\n");

  asprintf(&commandline,
	   "make -s -C \"%s\" M=\"%s\" clean && "
	   "rm -f \"%s/Module.symvers\" \"%s/modules.order\"",
	   kerneldirname,
	   tempdirname,
	   tempdirname,
	   tempdirname);
  if (system(commandline))
    {
      fprintf(stderr, PRGNAME ": Warning: Failed execution: %s\n",
	      commandline);
    }

  if ((-1==unlink (kbuildfilename)))
    perror (PRGNAME": unlink of temporary Kbuild file failed");
  unlink (tempfilename);	/* this may or may not exist */
  if (-1==rmdir (tempdirname))
    perror (PRGNAME": rmdir of temporary dir failed");

  return 0;
}