1# 2# Copyright (C) 2017 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import math 18 19from parse import with_pattern 20from vts.testcases.kernel.api.proc import KernelProcFileTestBase 21from vts.utils.python.file import target_file_utils 22 23# Test for /proc/sys/kernel/*. 24 25class ProcCorePattern(KernelProcFileTestBase.KernelProcFileTestBase): 26 '''/proc/sys/kernel/core_pattern is used to specify a core dumpfile pattern 27 name. 28 ''' 29 30 def parse_contents(self, contents): 31 pass 32 33 def get_path(self): 34 return "/proc/sys/kernel/core_pattern" 35 36 def get_permission_checker(self): 37 return target_file_utils.IsReadWrite 38 39 40class ProcCorePipeLimit(KernelProcFileTestBase.KernelProcFileTestBase): 41 '''/proc/sys/kernel/core_pipe_limit defines how many concurrent crashing 42 processes may be piped to user space applications in parallel. 43 ''' 44 45 def parse_contents(self, contents): 46 return self.parse_line("{:d}\n", contents)[0] 47 48 def get_path(self): 49 return "/proc/sys/kernel/core_pipe_limit" 50 51 def get_permission_checker(self): 52 return target_file_utils.IsReadWrite 53 54 55class ProcDmesgRestrict(KernelProcFileTestBase.KernelProcFileTestBase): 56 '''/proc/sys/kernel/dmesg_restrict indicates whether unprivileged users are 57 prevented from using dmesg. 58 ''' 59 60 def parse_contents(self, contents): 61 return self.parse_line("{:d}\n", contents)[0] 62 63 def result_correct(self, result): 64 return result in [0, 1] 65 66 def get_path(self): 67 return "/proc/sys/kernel/dmesg_restrict" 68 69 def get_permission_checker(self): 70 return target_file_utils.IsReadWrite 71 72 73class ProcDomainname(KernelProcFileTestBase.KernelProcFileTestBase): 74 '''/proc/sys/kernel/domainname determines YP/NIS domain name of the system.''' 75 76 def parse_contents(self, contents): 77 pass 78 79 def get_path(self): 80 return "/proc/sys/kernel/domainname" 81 82 def get_permission_checker(self): 83 return target_file_utils.IsReadWrite 84 85 86class ProcHostname(KernelProcFileTestBase.KernelProcFileTestBase): 87 '''/proc/sys/kernel/hostname determines the system's host name.''' 88 89 def parse_contents(self, contents): 90 pass 91 92 def get_path(self): 93 return "/proc/sys/kernel/hostname" 94 95 def get_permission_checker(self): 96 return target_file_utils.IsReadWrite 97 98 99class ProcHungTaskTimeoutSecs(KernelProcFileTestBase.KernelProcFileTestBase): 100 '''/proc/sys/kernel/hung_task_timeout_secs controls the default timeout 101 (in seconds) used to determine when a task has become non-responsive and 102 should be considered hung. 103 ''' 104 105 def parse_contents(self, contents): 106 return self.parse_line("{:d}\n", contents)[0] 107 108 def get_path(self): 109 return "/proc/sys/kernel/hung_task_timeout_secs" 110 111 def get_permission_checker(self): 112 return target_file_utils.IsReadWrite 113 114 def file_optional(self, shell=None, dut=None): 115 return True 116 117class ProcKptrRestrictTest(KernelProcFileTestBase.KernelProcFileTestBase): 118 '''/proc/sys/kernel/kptr_restrict determines whether kernel pointers are printed 119 in proc files. 120 ''' 121 122 def parse_contents(self, contents): 123 return self.parse_line("{:d}\n", contents)[0] 124 125 def result_correct(self, result): 126 return result >= 0 and result <= 4 127 128 def get_path(self): 129 return "/proc/sys/kernel/kptr_restrict" 130 131 def get_permission_checker(self): 132 """Get r/w file permission checker. 133 """ 134 return target_file_utils.IsReadWrite 135 136 137class ProcModulesDisabled(KernelProcFileTestBase.KernelProcFileTestBase): 138 '''/proc/sys/kernel/modules_disabled indicates if modules are allowed to be 139 loaded. 140 ''' 141 142 def parse_contents(self, contents): 143 return self.parse_line("{:d}\n", contents)[0] 144 145 def result_correct(self, result): 146 return result in [0, 1] 147 148 def get_path(self): 149 return "/proc/sys/kernel/modules_disabled" 150 151 def get_permission_checker(self): 152 return target_file_utils.IsReadWrite 153 154 155class ProcPanicOnOops(KernelProcFileTestBase.KernelProcFileTestBase): 156 '''/proc/sys/kernel/panic_on_oops controls kernel's behaviour on oops.''' 157 158 def parse_contents(self, contents): 159 return self.parse_line("{:d}\n", contents)[0] 160 161 def result_correct(self, result): 162 return result in [0, 1] 163 164 def get_path(self): 165 return "/proc/sys/kernel/panic_on_oops" 166 167 def get_permission_checker(self): 168 return target_file_utils.IsReadWrite 169 170 171class ProcPerfEventMaxSampleRate(KernelProcFileTestBase.KernelProcFileTestBase): 172 '''/proc/sys/kernel/perf_event_max_sample_rate sets the maximum sample rate 173 of performance events. 174 ''' 175 176 def parse_contents(self, contents): 177 return self.parse_line("{:d}\n", contents)[0] 178 179 def get_path(self): 180 return "/proc/sys/kernel/perf_event_max_sample_rate" 181 182 def get_permission_checker(self): 183 return target_file_utils.IsReadWrite 184 185 186class ProcPerfEventParanoid(KernelProcFileTestBase.KernelProcFileTestBase): 187 '''/proc/sys/kernel/perf_event_paranoid controls use of the performance 188 events system by unprivileged users. 189 ''' 190 191 def parse_contents(self, contents): 192 return self.parse_line("{:d}\n", contents)[0] 193 194 def get_path(self): 195 return "/proc/sys/kernel/perf_event_paranoid" 196 197 def get_permission_checker(self): 198 return target_file_utils.IsReadWrite 199 200 201class ProcPidMax(KernelProcFileTestBase.KernelProcFileTestBase): 202 '''/proc/sys/kernel/pid_max is the pid allocation wrap value.''' 203 204 def parse_contents(self, contents): 205 return self.parse_line("{:d}\n", contents)[0] 206 207 def get_path(self): 208 return "/proc/sys/kernel/pid_max" 209 210 def get_permission_checker(self): 211 return target_file_utils.IsReadWrite 212 213 214@with_pattern( 215 r"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" 216) 217def token_uuid(text): 218 return text 219 220class ProcSysKernelRandomBootId(KernelProcFileTestBase.KernelProcFileTestBase): 221 '''/proc/sys/kernel/random/boot_id generates a random ID each boot.''' 222 223 def parse_contents(self, contents): 224 return self.parse_line("{:uuid}", contents, dict(uuid=token_uuid))[0] 225 226 def get_path(self): 227 return "/proc/sys/kernel/random/boot_id" 228 229 def get_permission_checker(self): 230 return target_file_utils.IsReadOnly 231 232 233class ProcRandomizeVaSpaceTest(KernelProcFileTestBase.KernelProcFileTestBase): 234 '''/proc/sys/kernel/randomize_va_space determines the address layout randomization 235 policy for the system. 236 ''' 237 238 def parse_contents(self, contents): 239 return self.parse_line("{:d}\n", contents)[0] 240 241 def result_correct(self, result): 242 return result >= 0 and result <= 2 243 244 def get_path(self): 245 return "/proc/sys/kernel/randomize_va_space" 246 247 def get_permission_checker(self): 248 """Get r/w file permission checker. 249 """ 250 return target_file_utils.IsReadWrite 251 252 253class ProcSchedChildRunsFirst(KernelProcFileTestBase.KernelProcFileTestBase): 254 '''/proc/sys/kernel/sched_child_runs_first causes newly forked tasks to 255 be favored in scheduling over their parents. 256 ''' 257 258 def parse_contents(self, contents): 259 return self.parse_line("{:d}\n", contents)[0] 260 261 def get_path(self): 262 return "/proc/sys/kernel/sched_child_runs_first" 263 264 def get_permission_checker(self): 265 return target_file_utils.IsReadWrite 266 267 268class ProcSchedLatencyNS(KernelProcFileTestBase.KernelProcFileTestBase): 269 '''/proc/sys/kernel/sched_latency_ns is the maximum latency in nanoseconds a 270 task may incur prior to being scheduled. 271 ''' 272 273 def parse_contents(self, contents): 274 return self.parse_line("{:d}\n", contents)[0] 275 276 def result_correct(self, result): 277 return result >= 100000 and result <= 1000000000 278 279 def get_path(self): 280 return "/proc/sys/kernel/sched_latency_ns" 281 282 def get_permission_checker(self): 283 return target_file_utils.IsReadWrite 284 285 286class ProcSchedRTPeriodUS(KernelProcFileTestBase.KernelProcFileTestBase): 287 '''/proc/sys/kernel/sched_rt_period_us defines the period length used by the 288 system-wide RT execution limit in microseconds. 289 ''' 290 291 def parse_contents(self, contents): 292 return self.parse_line("{:d}\n", contents)[0] 293 294 def result_correct(self, result): 295 return result >= 1 and result <= math.pow(2,31) 296 297 def get_path(self): 298 return "/proc/sys/kernel/sched_rt_period_us" 299 300 def get_permission_checker(self): 301 return target_file_utils.IsReadWrite 302 303 304class ProcSchedRTRuntimeUS(KernelProcFileTestBase.KernelProcFileTestBase): 305 '''/proc/sys/kernel/sched_rt_runtime_us defines the amount of time in 306 microseconds relative to sched_rt_period_us that the system may execute RT 307 tasks. 308 ''' 309 310 def parse_contents(self, contents): 311 return self.parse_line("{:d}\n", contents)[0] 312 313 def result_correct(self, result): 314 return result >= -1 and result <= (math.pow(2,31) - 1) 315 316 def get_path(self): 317 return "/proc/sys/kernel/sched_rt_runtime_us" 318 319 def get_permission_checker(self): 320 return target_file_utils.IsReadWrite 321 322 323class ProcSchedTunableScaling(KernelProcFileTestBase.KernelProcFileTestBase): 324 '''/proc/sys/kernel/sched_tunable_scaling determines whether 325 sched_latency_ns should be automatically adjusted by the scheduler based on 326 the number of CPUs. 327 ''' 328 329 def parse_contents(self, contents): 330 return self.parse_line("{:d}\n", contents)[0] 331 332 def result_correct(self, result): 333 return result >= 0 and result <= 2 334 335 def get_path(self): 336 return "/proc/sys/kernel/sched_tunable_scaling" 337 338 def get_permission_checker(self): 339 return target_file_utils.IsReadWrite 340 341 342class ProcSchedWakeupGranularityNS(KernelProcFileTestBase.KernelProcFileTestBase): 343 '''/proc/sys/kernel/sched_wakeup_granularity_ns defines how much more 344 virtual runtime task A must have than task B in nanoseconds in order for 345 task B to preempt it. 346 ''' 347 348 def parse_contents(self, contents): 349 return self.parse_line("{:d}\n", contents)[0] 350 351 def result_correct(self, result): 352 return result >= 0 and result <= 1000000000 353 354 def get_path(self): 355 return "/proc/sys/kernel/sched_wakeup_granularity_ns" 356 357 def get_permission_checker(self): 358 return target_file_utils.IsReadWrite 359 360 361class ProcSysRqTest(KernelProcFileTestBase.KernelProcFileTestBase): 362 '''/proc/sys/kernel/sysrq controls the functions allowed to be invoked 363 via the SysRq key.''' 364 365 def parse_contents(self, contents): 366 return self.parse_line("{:d}\n", contents)[0] 367 368 def result_correct(self, result): 369 return result >= 0 and result <= 511 370 371 def get_path(self): 372 return "/proc/sys/kernel/sysrq" 373 374 def get_permission_checker(self): 375 return target_file_utils.IsReadWrite 376 377 378# Tests for /proc/sys/vm/*. 379 380class ProcDirtyBackgroundBytes(KernelProcFileTestBase.KernelProcFileTestBase): 381 '''/proc/sys/vm/dirty_background_bytes contains the amount of dirty memory 382 at which the background kernel flusher threads will start writeback. 383 ''' 384 385 def parse_contents(self, contents): 386 return self.parse_line("{:d}\n", contents)[0] 387 388 def get_path(self): 389 return "/proc/sys/vm/dirty_background_bytes" 390 391 def get_permission_checker(self): 392 return target_file_utils.IsReadWrite 393 394 395class ProcDirtyBackgroundRatio(KernelProcFileTestBase.KernelProcFileTestBase): 396 '''/proc/sys/vm/dirty_background_ratio contains, as a percentage of total 397 available memory that contains free pages and reclaimable pages, the number 398 of pages at which the background kernel flusher threads will start writing 399 out dirty data. 400 ''' 401 402 def parse_contents(self, contents): 403 return self.parse_line("{:d}\n", contents)[0] 404 405 def result_correct(self, result): 406 return result >= 0 and result <= 100 407 408 def get_path(self): 409 return "/proc/sys/vm/dirty_background_ratio" 410 411 def get_permission_checker(self): 412 return target_file_utils.IsReadWrite 413 414 415class ProcDirtyExpireCentisecs(KernelProcFileTestBase.KernelProcFileTestBase): 416 '''/proc/sys/vm/dirty_expire_centisecs is used to define when dirty data is 417 old enough to be eligible for writeout by the kernel flusher threads. 418 ''' 419 420 def parse_contents(self, contents): 421 return self.parse_line("{:d}\n", contents)[0] 422 423 def get_path(self): 424 return "/proc/sys/vm/dirty_expire_centisecs" 425 426 def get_permission_checker(self): 427 return target_file_utils.IsReadWrite 428 429 430class ProcDropCaches(KernelProcFileTestBase.KernelProcFileTestBase): 431 '''Writing to /proc/sys/vm/drop_caches will cause the kernel to drop clean 432 caches. 433 ''' 434 435 def parse_contents(self, contents): 436 # Format of this file is not documented, so don't check that. 437 return '' 438 439 def get_path(self): 440 return "/proc/sys/vm/drop_caches" 441 442 def get_permission_checker(self): 443 return target_file_utils.IsReadWrite 444 445 446class ProcExtraFreeKbytes(KernelProcFileTestBase.KernelProcFileTestBase): 447 '''/proc/sys/vm/extra_free_kbytes tells the VM to keep extra free memory 448 between the threshold where background reclaim (kswapd) kicks in, and the 449 threshold where direct reclaim (by allocating processes) kicks in. 450 ''' 451 452 def parse_contents(self, contents): 453 return self.parse_line("{:d}\n", contents)[0] 454 455 def get_path(self): 456 return "/proc/sys/vm/extra_free_kbytes" 457 458 def get_permission_checker(self): 459 return target_file_utils.IsReadWrite 460 461 def file_optional(self, shell=None, dut=None): 462 # This file isn't in Android common kernel. 463 return True 464 465 466class ProcOverCommitMemoryTest(KernelProcFileTestBase.KernelProcFileTestBase): 467 '''/proc/sys/vm/overcommit_memory determines the kernel virtual memory accounting mode. 468 ''' 469 470 def parse_contents(self, contents): 471 return self.parse_line("{:d}\n", contents)[0] 472 473 def result_correct(self, result): 474 return result >= 0 and result <= 2 475 476 def get_path(self): 477 return "/proc/sys/vm/overcommit_memory" 478 479 def get_permission_checker(self): 480 """Get r/w file permission checker. 481 """ 482 return target_file_utils.IsReadWrite 483 484 485class ProcMaxMapCount(KernelProcFileTestBase.KernelProcFileTestBase): 486 '''/proc/sys/vm/max_map_count contains the maximum number of memory map areas a process 487 may have. 488 ''' 489 490 def parse_contents(self, contents): 491 return self.parse_line("{:d}\n", contents)[0] 492 493 def get_path(self): 494 return "/proc/sys/vm/max_map_count" 495 496 def get_permission_checker(self): 497 return target_file_utils.IsReadWrite 498 499 500class ProcMmapMinAddrTest(KernelProcFileTestBase.KernelProcFileTestBase): 501 '''/proc/sys/vm/mmap_min_addr specifies the minimum address that can be mmap'd. 502 ''' 503 504 def parse_contents(self, contents): 505 return self.parse_line("{:d}\n", contents)[0] 506 507 def get_path(self): 508 return "/proc/sys/vm/mmap_min_addr" 509 510 def get_permission_checker(self): 511 """Get r/w file permission checker. 512 """ 513 return target_file_utils.IsReadWrite 514 515 516class ProcMmapRndBitsTest(KernelProcFileTestBase.KernelProcFileTestBase): 517 '''/proc/sys/vm/mmap_rnd_(compat_)bits specifies the amount of randomness in mmap'd 518 addresses. Must be >= 8. 519 ''' 520 521 def parse_contents(self, contents): 522 return self.parse_line("{:d}\n", contents)[0] 523 524 def result_correct(self, result): 525 return result >= 8 526 527 def get_path(self): 528 return "/proc/sys/vm/mmap_rnd_bits" 529 530 def get_permission_checker(self): 531 """Get r/w file permission checker. 532 """ 533 return target_file_utils.IsReadWrite 534 535 536class ProcMmapRndCompatBitsTest(ProcMmapRndBitsTest): 537 def get_path(self): 538 return "/proc/sys/vm/mmap_rnd_compat_bits" 539 540 541class ProcPageCluster(KernelProcFileTestBase.KernelProcFileTestBase): 542 '''/proc/sys/vm/page-cluster controls the number of pages up to which 543 consecutive pages are read in from swap in a single attempt. 544 ''' 545 546 def parse_contents(self, contents): 547 return self.parse_line("{:d}\n", contents)[0] 548 549 def get_path(self): 550 return "/proc/sys/vm/page-cluster" 551 552 def get_permission_checker(self): 553 return target_file_utils.IsReadWrite 554 555 556# Tests for /proc/sys/fs/*. 557 558class ProcPipeMaxSize(KernelProcFileTestBase.KernelProcFileTestBase): 559 '''/proc/sys/fs/pipe-max-size reports the maximum size (in bytes) of 560 individual pipes. 561 ''' 562 563 def parse_contents(self, contents): 564 return self.parse_line("{:d}\n", contents)[0] 565 566 def get_path(self): 567 return "/proc/sys/fs/pipe-max-size" 568 569 def get_permission_checker(self): 570 return target_file_utils.IsReadWrite 571 572 573class ProcProtectedHardlinks(KernelProcFileTestBase.KernelProcFileTestBase): 574 '''/proc/sys/fs/protected_hardlinks reports hardlink creation behavior.''' 575 576 def parse_contents(self, contents): 577 return self.parse_line("{:d}\n", contents)[0] 578 579 def result_correct(self, result): 580 return result in [0, 1] 581 582 def get_path(self): 583 return "/proc/sys/fs/protected_hardlinks" 584 585 def get_permission_checker(self): 586 return target_file_utils.IsReadWrite 587 588 589class ProcProtectedSymlinks(KernelProcFileTestBase.KernelProcFileTestBase): 590 '''/proc/sys/fs/protected_symlinks reports symlink following behavior.''' 591 592 def parse_contents(self, contents): 593 return self.parse_line("{:d}\n", contents)[0] 594 595 def result_correct(self, result): 596 return result in [0, 1] 597 598 def get_path(self): 599 return "/proc/sys/fs/protected_symlinks" 600 601 def get_permission_checker(self): 602 return target_file_utils.IsReadWrite 603 604 605class ProcSuidDumpable(KernelProcFileTestBase.KernelProcFileTestBase): 606 '''/proc/sys/fs/suid_dumpable value can be used to query and set the core 607 dump mode for setuid or otherwise protected/tainted binaries. 608 ''' 609 610 def parse_contents(self, contents): 611 return self.parse_line("{:d}\n", contents)[0] 612 613 def result_correct(self, result): 614 return result in [0, 1, 2] 615 616 def get_path(self): 617 return "/proc/sys/fs/suid_dumpable" 618 619 def get_permission_checker(self): 620 return target_file_utils.IsReadWrite 621 622 623class ProcUptime(KernelProcFileTestBase.KernelProcFileTestBase): 624 '''/proc/uptime tells how long the system has been running.''' 625 626 def parse_contents(self, contents): 627 return self.parse_line("{:f} {:f}\n", contents)[0] 628 629 def get_path(self): 630 return "/proc/uptime" 631