File: mkprofile.pl

package info (click to toggle)
apparmor 2.13.2-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 28,404 kB
  • sloc: python: 19,093; ansic: 17,037; perl: 11,105; sh: 10,442; cpp: 5,323; yacc: 1,933; makefile: 1,679; pascal: 1,097; lex: 1,088; ruby: 374; exp: 250; java: 212; xml: 159
file content (507 lines) | stat: -rwxr-xr-x 13,745 bytes parent folder | download | duplicates (2)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#! /usr/bin/perl -w
#
# mkprofile.pl -
#   generate a formatted profile based on passed in arguments
#
# Gawd, I hate writing perl. It shows, too.
#
my $__VERSION__=$0;

use strict;
use Getopt::Long;
use Cwd 'realpath';

my $help = '';
my $nowarn = '';
my $nodefault;
my $noimage;
my $escape = '';
my $usestdin = '';
my %output_rules;
my $hat = "__no_hat";
my %flags;

GetOptions(
  'escape|E' => \$escape,
  'nowarn' => \$nowarn,
  'help|h' => \$help,
  'nodefault|N' => \$nodefault,
  'noimage|I' => \$noimage,
  'stdin' => \$usestdin,
);

sub usage {
  print STDERR "$__VERSION__\n";
  print STDERR "Usage $0 [--nowarn|--escape] execname [rules]\n";
  print STDERR "      $0 --help\n";
  print STDERR "      $0 --stdin\n";
  print STDERR "  nowarn:      don't warn if execname does not exist\n";
  print STDERR "  nodefault:   don't include default rules/ldd output\n";
  print STDERR "  escape:      escape stuff that would be treated as regexs\n";
  print STDERR "  help:        print this message\n";
}

# genprofile passes in $bin:w as default rule atm
&usage && exit 0 if ($help || (!$usestdin && @ARGV < 1) || ($usestdin && @ARGV != 2));

sub head ($) {
  my $file = shift;

  my $first = "";
  if (open(FILE, $file)) {
    $first = <FILE>;
    close(FILE);
  }

  return $first;
}

sub get_output ($@) {
  my ($program, @args) = @_;

  my $ret = -1;

  my $pid;
  my @output;

  if (-x $program) {
    $pid = open(KID_TO_READ, "-|");
    unless (defined $pid) {
      die "can't fork: $!";
    }

    if ($pid) {
      while (<KID_TO_READ>) {
        chomp;
        push @output, $_;
      }
      close(KID_TO_READ);
      $ret = $?;
    } else {
      ($>, $)) = ($<, $();
      open(STDERR, ">&STDOUT")
        || die "can't dup stdout to stderr";
      exec($program, @args) || die "can't exec program: $!";

      # NOTREACHED
    }
  }

  return ($ret, @output);
}

sub gen_default_rules() {
  gen_file("/etc/ld.so.cache:r");

  # give every profile access to change_hat
  gen_file("/proc/*/attr/current:w");

  # give every profile access to /dev/urandom (propolice, etc.)
  gen_file("/dev/urandom:r");
}

sub gen_elf_binary($) {
  my $bin = shift;

  my ($ret, @ldd) = get_output("/usr/bin/ldd", $bin);
  if ($ret == 0) {
    for my $line (@ldd) {
      last if $line =~ /not a dynamic executable/;
      last if $line =~ /cannot read header/;
      last if $line =~ /statically linked/;

      # avoid new kernel 2.6 poo
      next if $line =~ /linux-(gate|vdso(32|64)).so/;

      if ($line =~ /^\s*\S+ => (\/\S+)/) {
        # shared libraries
        gen_file(realpath($1) . ":mr")
      } elsif ($line =~ /^\s*(\/\S+)/) {
        # match loader lines like "/lib64/ld-linux-x86-64.so.2 (0x00007fbb46999000)"
        gen_file(realpath($1) . ":rix")
      }
    }
  }
}

sub gen_binary($) {
  my $bin = shift;

  gen_file("$bin:rix") unless $noimage;

  my $hashbang = head($bin);
  if ($hashbang && $hashbang =~ /^#!\s*(\S+)/) {
    my $interpreter = $1;
    gen_file(realpath($interpreter) . ":rix");
    gen_elf_binary($interpreter);
  } else {
    gen_elf_binary(realpath($bin))
  }
}

sub gen_netdomain($) {
  my $rule = shift;
  # only split on single ':'s
  my @rules = split (/(?<!:):(?!:)/, $rule);
  # convert '::' to ':' -- for port designations
  foreach (@rules) { s/::/:/g; }
  push (@{$output_rules{$hat}}, "  @rules,\n");
}

sub gen_network($) {
  my $rule = shift;
  my @rules = split (/:/, $rule);
  push (@{$output_rules{$hat}}, "  @rules,\n");
}

sub gen_unix($) {
  my $rule = shift;
  if ($rule =~ /^unix:ALL$/) {
    push (@{$output_rules{$hat}}, "  unix,\n");
  } else {
    $rule =~ s/:/ /g;
    push(@{$output_rules{$hat}}, "  " . $rule . ",\n");
  }
}

sub gen_cap($) {
  my $rule = shift;
  my @rules = split (/:/, $rule);
  if (@rules == 2) {
    if ($rules[1] =~ /^ALL$/) {
      push (@{$output_rules{$hat}}, "  capability,\n");
    } else {
      push (@{$output_rules{$hat}}, "  capability $rules[1],\n");
    }
  } elsif (@rules == 3) {
    if ($rules[1] =~ /^ALL$/) {
      push (@{$output_rules{$hat}}, "  $rules[2] capability,\n");
    } else {
      push (@{$output_rules{$hat}}, "  $rules[2] capability $rules[1],\n");
    }
  } else {
    (!$nowarn) && print STDERR "Warning: invalid capability description '$rule', ignored\n";
  }
}

sub gen_ptrace($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
	    push (@{$output_rules{$hat}}, "  ptrace,\n");
	} else {
	    push (@{$output_rules{$hat}}, "  ptrace $rules[1],\n");
	}
    } elsif (@rules == 3) {
	push (@{$output_rules{$hat}}, "  ptrace $rules[1] $rules[2],\n");
    } else {
	(!$nowarn) && print STDERR "Warning: invalid ptrace description '$rule', ignored\n";
    }
}

sub gen_signal($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
	    push (@{$output_rules{$hat}}, "  signal,\n");
	} else {
	    push (@{$output_rules{$hat}}, "  signal $rules[1],\n");
	}
    } elsif (@rules == 3) {
	push (@{$output_rules{$hat}}, "  signal $rules[1] $rules[2],\n");
    } else {
	(!$nowarn) && print STDERR "Warning: invalid signal description '$rule', ignored\n";
    }
}

sub gen_mount($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
	    push (@{$output_rules{$hat}}, "  mount,\n");
	} else {
	    push (@{$output_rules{$hat}}, "  mount $rules[1],\n");
	}
    } elsif (@rules == 3) {
	push (@{$output_rules{$hat}}, "  mount $rules[1] $rules[2],\n");
    } elsif (@rules == 4) {
	push (@{$output_rules{$hat}}, "  mount $rules[1] $rules[2] $rules[3],\n");
    } elsif (@rules == 5) {
	push (@{$output_rules{$hat}}, "  mount $rules[1] $rules[2] $rules[3] $rules[4],\n");
    } elsif (@rules == 6) {
	push (@{$output_rules{$hat}}, "  mount $rules[1] $rules[2] $rules[3] $rules[4] $rules[5],\n");
    } elsif (@rules == 7) {
	push (@{$output_rules{$hat}}, "  mount $rules[1] $rules[2] $rules[3] $rules[4] $rules[5] $rules[6],\n");
    } else {
	(!$nowarn) && print STDERR "Warning: invalid mount description '$rule', ignored\n";
    }
}

sub gen_remount($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
	    push (@{$output_rules{$hat}}, "  remount,\n");
	} else {
	    push (@{$output_rules{$hat}}, "  remount $rules[1],\n");
	}
    } elsif (@rules == 3) {
	push (@{$output_rules{$hat}}, "  remount $rules[1] $rules[2],\n");
    } elsif (@rules == 4) {
	push (@{$output_rules{$hat}}, "  remount $rules[1] $rules[2] $rules[3],\n");
    } elsif (@rules == 5) {
	push (@{$output_rules{$hat}}, "  remount $rules[1] $rules[2] $rules[3] $rules[4],\n");
    } elsif (@rules == 6) {
	push (@{$output_rules{$hat}}, "  remount $rules[1] $rules[2] $rules[3] $rules[4] $rules[5],\n");
    } elsif (@rules == 7) {
	push (@{$output_rules{$hat}}, "  remount $rules[1] $rules[2] $rules[3] $rules[4] $rules[5] $rules[6],\n");
    } else {
	(!$nowarn) && print STDERR "Warning: invalid remount description '$rule', ignored\n";
    }
}

sub gen_umount($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
	    push (@{$output_rules{$hat}}, "  umount,\n");
	} else {
	    push (@{$output_rules{$hat}}, "  umount $rules[1],\n");
	}
    } elsif (@rules == 3) {
	push (@{$output_rules{$hat}}, "  umount $rules[1] $rules[2],\n");
    } elsif (@rules == 4) {
	push (@{$output_rules{$hat}}, "  umount $rules[1] $rules[2] $rules[3],\n");
    } elsif (@rules == 5) {
	push (@{$output_rules{$hat}}, "  umount $rules[1] $rules[2] $rules[3] $rules[4],\n");
    } elsif (@rules == 6) {
	push (@{$output_rules{$hat}}, "  umount $rules[1] $rules[2] $rules[3] $rules[4] $rules[5],\n");
    } elsif (@rules == 7) {
	push (@{$output_rules{$hat}}, "  umount $rules[1] $rules[2] $rules[3] $rules[4] $rules[5] $rules[6],\n");
    } else {
	(!$nowarn) && print STDERR "Warning: invalid umount description '$rule', ignored\n";
    }
}

sub gen_pivot_root($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
	    push (@{$output_rules{$hat}}, "  pivot_root,\n");
	} else {
	    push (@{$output_rules{$hat}}, "  pivot_root $rules[1],\n");
	}
    } else {
	(!$nowarn) && print STDERR "Warning: invalid pivot_root description '$rule', ignored\n";
    }
}

sub gen_file($) {
  my $rule = shift;
  my @rules = split (/:/, $rule);
  # default: file rules
  if (@rules == 1) {
      # support raw rules
      push (@{$output_rules{$hat}}, "  $rules[0],\n");
  } elsif (@rules == 2) {
    if ($escape) {
      $rules[0]=~ s/(["[\]{}\:])/\\$1/g;
      $rules[0]=~ s/(\#)/\\043/g;
    }
    if ($rules[0]=~ /[\s\!\"\^]/) {
      push (@{$output_rules{$hat}}, "  \"$rules[0]\" $rules[1],\n");
    } else {
      push (@{$output_rules{$hat}}, "  $rules[0] $rules[1],\n");
    }
  } else {
    (!$nowarn) && print STDERR "Warning: invalid file access '$rule', ignored\n";
  }
}

sub gen_flag($) {
  my $rule = shift;
  my @rules = split (/:/, $rule);
  if (@rules != 2) {
    (!$nowarn) && print STDERR "Warning: invalid flag description '$rule', ignored\n";
  } else {
    push (@{$flags{$hat}},$rules[1]);
  }
}

sub gen_change_profile($) {
    my $rule = shift;
    my @rules = split (/:/, $rule);
    if (@rules == 2) {
	if ($rules[1] =~ /^ALL$/) {
            push (@{$output_rules{$hat}}, "  change_profile,\n",);
	} else {
            push (@{$output_rules{$hat}}, "  change_profile -> $rules[1],\n",);
	}
    } elsif (@rules == 3) {
        push (@{$output_rules{$hat}}, "  change_profile $rules[1] -> $rules[2],\n",);
    } elsif (@rules == 4) {
        push (@{$output_rules{$hat}}, "  change_profile $rules[1] $rules[2] -> $rules[3],\n",);
    } else {
        (!$nowarn) && print STDERR "Warning: invalid change_profile description '$rule', ignored\n";
    }
}

sub gen_hat($) {
  my $rule = shift;
  my @rules = split (/:/, $rule);
  if (@rules != 2) {
    (!$nowarn) && print STDERR "Warning: invalid hat description '$rule', ignored\n";
  } else {
    $hat = $rules[1];
    # give every profile/hat access to change_hat
    @{$output_rules{$hat}} = ( "  /proc/*/attr/current w,\n",);
  }
}

sub gen_addimage($) {
  my $rule = shift;
  my @rules = split (/:/, $rule);
  if (@rules != 2) {
    (!$nowarn) && print STDERR "Warning: invalid addimage description '$rule', ignored\n";
  } else {
    gen_binary($rules[1]);
  }
}

sub emit_flags($) {
  my $hat = shift;

  if (exists $flags{$hat}) {
    print STDOUT " flags=(";
    print STDOUT pop(@{$flags{$hat}});
    foreach my $flag (@{$flags{$hat}}) {
      print STDOUT ", $flag";
    }
    print STDOUT ") ";
  }
}

# generate profiles based on cmd line arguments
sub gen_from_args() {
  my $bin = shift @ARGV;
  my $addimage = 0;

  unless ($nodefault) {
    gen_default_rules();
    gen_binary($bin);
  }

  for my $rule (@ARGV) {
    #($fn, @rules) = split (/:/, $rule);
    if ($rule =~ /^(tcp|udp)/) {
      # netdomain rules
      gen_netdomain($rule);
    } elsif ($rule =~ /^network:/) {
      gen_network($rule);
    } elsif ($rule =~ /^unix:/) {
      gen_unix($rule);
    } elsif ($rule =~ /^cap:/) {
      gen_cap($rule);
    } elsif ($rule =~ /^ptrace:/) {
      gen_ptrace($rule);
    } elsif ($rule =~ /^signal:/) {
      gen_signal($rule);
    } elsif ($rule =~ /^mount:/) {
      gen_mount($rule);
    } elsif ($rule =~ /^remount:/) {
      gen_remount($rule);
    } elsif ($rule =~ /^umount:/) {
      gen_umount($rule);
    } elsif ($rule =~ /^pivot_root:/) {
      gen_pivot_root($rule);
    } elsif ($rule =~ /^flag:/) {
      gen_flag($rule);
    } elsif ($rule =~ /^hat:/) {
      gen_hat($rule);
    } elsif ($rule =~ /^change_profile:/) {
      gen_change_profile($rule);
    } elsif ($rule =~ /^addimage:/) {
      gen_addimage($rule);
      $addimage = 1;
    } else {
      gen_file($rule);
    }
  }

  !(-e $bin || $addimage || $nowarn) && print STDERR "Warning: execname '$bin': no such file or directory\n";

  print STDOUT "# Profile autogenerated by $__VERSION__\n";
  if (not substr($bin, 0, 1) eq "/") {
	  print STDOUT "profile "
  }
  print STDOUT "$bin ";
  emit_flags('__no_hat');
  print STDOUT "{\n";
  foreach my $outrule (@{$output_rules{'__no_hat'}}) {
    print STDOUT $outrule;
  }
  foreach my $hat (keys %output_rules) {
    if (not $hat =~ /^__no_hat$/) {
      print STDOUT "\n  ^$hat";
      emit_flags($hat);
      print STDOUT " {\n";
      foreach my $outrule (@{$output_rules{$hat}}) {
        print STDOUT "  $outrule";
      }
      print STDOUT "  }\n";
    }
  }
  #foreach my $hat keys
  #foreach my $outrule (@output_rules) {
  #  print STDOUT $outrule;
  #}
  print STDOUT "}\n";
}

#generate the profiles from stdin, interpreting and replacing the following sequences
# @{gen_elf name} - generate rules for elf binaries
# @{gen_bin name} - generate rules for a binary
# @{gen_def} - generate default rules
# @{gen name} - do @{gen_def} @{gen_bin name}

sub emit_and_clear_rules() {
  foreach my $outrule (@{$output_rules{'__no_hat'}}) {
    print STDOUT $outrule;
  }

  undef %output_rules;
}

sub gen_from_stdin() {
  while(<STDIN>) {
    chomp;
    if ($_ =~ m/@\{gen_def}/) {
      gen_default_rules();
      emit_and_clear_rules();
    } elsif ($_ =~ m/@\{gen_bin\s+(.+)\}/) {
      gen_binary($1);
      emit_and_clear_rules();
    } elsif ($_ =~ m/@\{gen_elf\s+(.+)\}/) {
      gen_elf_binary($1);
      emit_and_clear_rules();
    } elsif ($_ =~ m/@\{gen\s+(.+)\}/) {
      gen_default_rules();
      gen_binary($1);
      emit_and_clear_rules();
    } else {
      print STDOUT "$_\n" ;
    }
  }
}

if ($usestdin) {
  gen_from_stdin();
} else {
  gen_from_args();
}