| 531 | |
| 532 | = 10/14 = |
| 533 | == getcpu system call == |
| 534 | * http://www.kernel.org/doc/man-pages/online/pages/man2/getcpu.2.html |
| 535 | * http://www.linuxquestions.org/questions/programming-9/determine-what-cpu-my-thread-is-on-817697/ |
| 536 | |
| 537 | * getcpu system callはには存在しない |
| 538 | * arch/x86/include/asm/unistd_64.h |
| 539 | {{{#!cc |
| 540 | #define __IGNORE_getcpu /* implemented as a vsyscall */ |
| 541 | }}} |
| 542 | |
| 543 | * s0711489@ubuntu-lucid64:~/coursework/KernelHack/02$ cat > getcpu.c |
| 544 | {{{#!cc |
| 545 | #define _GNU_SOURCE /* See feature_test_macros(7) */ |
| 546 | #include <linux/getcpu.h> |
| 547 | #include <stdio.h> |
| 548 | |
| 549 | int main (void) { |
| 550 | int c, s; |
| 551 | s = getcpu(&c, NULL, NULL); |
| 552 | printf ("getcpu() -> %d\n", (s == -1) ? s : c); |
| 553 | } |
| 554 | }}} |
| 555 | * s0711489@ubuntu-lucid64:~/coursework/KernelHack/02$ gcc getcpu.c -o getcpu |
| 556 | {{{ |
| 557 | getcpu.c:1:26: error: linux/getcpu.h: No such file or directory |
| 558 | }}} |
| 559 | |
| 560 | === i386 === |
| 561 | * s0711489@ubuntu-lucid:~$ sudo /mnt/hgfs/tools/install.sh |
| 562 | {{{ |
| 563 | + cat /mnt/hgfs/linux-2.6.35.14/include/config/kernel.release |
| 564 | + VERSION=2.6.35.14 |
| 565 | + echo Install Linux Kernel version 2.6.35.14 |
| 566 | Install Linux Kernel version 2.6.35.14 |
| 567 | + cd /mnt/hgfs/linux-2.6.35.14/ |
| 568 | + make install |
| 569 | sh /mnt/hgfs/linux-2.6.35.14/arch/x86/boot/install.sh 2.6.35.14 arch/x86/boot/bzImage \ |
| 570 | System.map "/boot" |
| 571 | + make modules_install |
| 572 | INSTALL arch/x86/kernel/test_nx.ko |
| 573 | INSTALL drivers/scsi/scsi_wait_scan.ko |
| 574 | INSTALL net/netfilter/xt_mark.ko |
| 575 | DEPMOD 2.6.35.14 |
| 576 | + mkinitramfs -o /boot/initrd.img-2.6.35.14 2.6.35.14 |
| 577 | + update-grub |
| 578 | Generating grub.cfg ... |
| 579 | Found linux image: /boot/vmlinuz-2.6.35.14 |
| 580 | Found initrd image: /boot/initrd.img-2.6.35.14 |
| 581 | Found linux image: /boot/vmlinuz-2.6.32-33-generic |
| 582 | Found initrd image: /boot/initrd.img-2.6.32-33-generic |
| 583 | Found memtest86+ image: /boot/memtest86+.bin |
| 584 | done |
| 585 | + echo Install Kernel Headers to /lib/modules/2.6.35.14/build |
| 586 | Install Kernel Headers to /lib/modules/2.6.35.14/build |
| 587 | + date +%Y%m%d |
| 588 | + mv /lib/modules/2.6.35.14/build /lib/modules/2.6.35.14/build-20111014 |
| 589 | + cd /mnt/hgfs/ |
| 590 | + tar c --files-from - |
| 591 | + tar x -C /lib/modules/2.6.35.14/ |
| 592 | + egrep -v vmlinu |
| 593 | + egrep -v .o$ |
| 594 | + find linux-2.6.35.14 -type f |
| 595 | + mv /lib/modules/2.6.35.14/linux-2.6.35.14 /lib/modules/2.6.35.14/build |
| 596 | + uname -a |
| 597 | Linux ubuntu-lucid 2.6.32-33-generic #72-Ubuntu SMP Fri Jul 29 21:08:37 UTC 2011 i686 GNU/Linux |
| 598 | }}} |
| 599 | * s0711489@ubuntu-lucid:~$ sudo vmware-config-tools.pl --default |
| 600 | |
| 601 | * s0711489@ubuntu-lucid:~$ cat > getcpu.c |
| 602 | {{{#!cc |
| 603 | #define _GNU_SOURCE |
| 604 | |
| 605 | #include <stdio.h> |
| 606 | #include <sys/syscall.h> |
| 607 | #include <unistd.h> |
| 608 | #include <errno.h> |
| 609 | #include <string.h> |
| 610 | |
| 611 | int main( void ) |
| 612 | { |
| 613 | int cpu = syscall(SYS_getcpu); |
| 614 | if (cpu < 0) |
| 615 | { |
| 616 | printf( "Error: errno = %d\n", errno ); fflush(stdout); |
| 617 | printf( "Error: errno: %s\n", strerror(errno) ); fflush(stdout); |
| 618 | return -1; |
| 619 | } |
| 620 | printf( "cpu = %d\n", cpu ); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | }}} |
| 625 | |
| 626 | * s0711489@ubuntu-lucid:~$ gcc getcpu.c -o getcpu |
| 627 | * s0711489@ubuntu-lucid:~$ ./getcpu |
| 628 | {{{ |
| 629 | Error: errno = 14 |
| 630 | Error: errno: Bad address |
| 631 | }}} |
| 632 | |
| 633 | * うまく動かない |
| 634 | |
| 635 | == getuid system call == |
| 636 | * windell57:x86_64 s0711489$ gdb |
| 637 | {{{ |
| 638 | (gdb) set logging file gdb.getuid.log |
| 639 | (gdb) set logging on |
| 640 | Copying output to gdb.getuid.log. |
| 641 | (gdb) file vmlinux |
| 642 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| 643 | (gdb) b sys_getuid |
| 644 | Breakpoint 1 at 0xffffffff81048d17: file kernel/timer.c, line 1366. |
| 645 | (gdb) target remote localhost:8864 |
| 646 | Remote debugging using localhost:8864 |
| 647 | 0xffffffff810097a9 in native_safe_halt () |
| 648 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 649 | 49 asm volatile("sti; hlt": : :"memory"); |
| 650 | (gdb) c |
| 651 | Continuing. |
| 652 | |
| 653 | Breakpoint 1, sys_getuid () at kernel/timer.c:1366 |
| 654 | 1366 { |
| 655 | (gdb) s |
| 656 | 1368 return current_uid(); |
| 657 | (gdb) |
| 658 | get_current () |
| 659 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 |
| 660 | 14 return percpu_read_stable(current_task); |
| 661 | (gdb) |
| 662 | 1368 return current_uid(); |
| 663 | (gdb) |
| 664 | sys_getuid () at kernel/timer.c:1366 |
| 665 | 1366 { |
| 666 | (gdb) |
| 667 | 1368 return current_uid(); |
| 668 | (gdb) |
| 669 | get_current () at kernel/timer.c:1368 |
| 670 | 1368 return current_uid(); |
| 671 | (gdb) |
| 672 | sys_getuid () at kernel/timer.c:1369 |
| 673 | 1369 } |
| 674 | (gdb) |
| 675 | |
| 676 | Program received signal SIGINT, Interrupt. |
| 677 | 0xffffffff810097a9 in native_safe_halt () |
| 678 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 679 | 49 asm volatile("sti; hlt": : :"memory"); |
| 680 | (gdb) detach |
| 681 | Ending remote debugging. |
| 682 | (gdb) quit |
| 683 | }}} |
| 684 | |
| 685 | * s0711489@ubuntu-lucid64:~/coursework/KernelHack/02$ ./getuid |
| 686 | {{{ |
| 687 | getuid() -> 1000 |
| 688 | }}} |
| 689 | |
| 690 | === trace log with source list and print data === |
| 691 | * windell57:x86_64 s0711489$ gdb |
| 692 | {{{ |
| 693 | (gdb) file vmlinux |
| 694 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| 695 | (gdb) b sys_getuid |
| 696 | Breakpoint 1 at 0xffffffff81048d17: file kernel/timer.c, line 1366. |
| 697 | (gdb) target remote localhost:8864 |
| 698 | Remote debugging using localhost:8864 |
| 699 | 0xffffffff810097a9 in native_safe_halt () |
| 700 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 701 | 49 asm volatile("sti; hlt": : :"memory"); |
| 702 | (gdb) c |
| 703 | Continuing. |
| 704 | |
| 705 | Breakpoint 1, sys_getuid () at kernel/timer.c:1366 |
| 706 | 1366 { |
| 707 | (gdb) bt |
| 708 | #0 sys_getuid () at kernel/timer.c:1366 |
| 709 | #1 0xffffffff810029eb in ?? () |
| 710 | #2 0x0000000000000206 in ?? () |
| 711 | #3 0x00007ffff56d6ad0 in ?? () |
| 712 | #4 0x00007f8671347210 in ?? () |
| 713 | #5 0x00007f8671333300 in ?? () |
| 714 | #6 0x0000000000000066 in ?? () |
| 715 | #7 0x0000000000000000 in ?? () |
| 716 | (gdb) l |
| 717 | 1361 |
| 718 | 1362 return pid; |
| 719 | 1363 } |
| 720 | 1364 |
| 721 | 1365 SYSCALL_DEFINE0(getuid) |
| 722 | 1366 { |
| 723 | 1367 /* Only we change this so SMP safe */ |
| 724 | 1368 return current_uid(); |
| 725 | 1369 } |
| 726 | 1370 |
| 727 | (gdb) s |
| 728 | 1368 return current_uid(); |
| 729 | (gdb) |
| 730 | get_current () |
| 731 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 |
| 732 | 14 return percpu_read_stable(current_task); |
| 733 | (gdb) bt |
| 734 | #0 get_current () |
| 735 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/current.h:14 |
| 736 | #1 sys_getuid () at kernel/timer.c:1368 |
| 737 | #2 0xffffffff810029eb in ?? () |
| 738 | #3 0x0000000000000206 in ?? () |
| 739 | #4 0x00007ffff56d6ad0 in ?? () |
| 740 | #5 0x00007f8671347210 in ?? () |
| 741 | #6 0x00007f8671333300 in ?? () |
| 742 | #7 0x0000000000000066 in ?? () |
| 743 | #8 0x0000000000000000 in ?? () |
| 744 | (gdb) l |
| 745 | 9 |
| 746 | 10 DECLARE_PER_CPU(struct task_struct *, current_task); |
| 747 | 11 |
| 748 | 12 static __always_inline struct task_struct *get_current(void) |
| 749 | 13 { |
| 750 | 14 return percpu_read_stable(current_task); |
| 751 | 15 } |
| 752 | 16 |
| 753 | 17 #define current get_current() |
| 754 | 18 |
| 755 | (gdb) p current_task |
| 756 | Cannot access memory at address 0xb540 |
| 757 | (gdb) ptype current_task |
| 758 | type = struct task_struct { |
| 759 | |
| 760 | (snip |
| 761 | |
| 762 | ---Type <return> to continue, or q <return> to quit---q |
| 763 | Quit |
| 764 | (gdb) s |
| 765 | 1368 return current_uid(); |
| 766 | (gdb) |
| 767 | sys_getuid () at kernel/timer.c:1366 |
| 768 | 1366 { |
| 769 | (gdb) bt |
| 770 | #0 sys_getuid () at kernel/timer.c:1366 |
| 771 | #1 0xffffffff810029eb in ?? () |
| 772 | #2 0x0000000000000206 in ?? () |
| 773 | #3 0x00007ffff56d6ad0 in ?? () |
| 774 | #4 0x00007f8671347210 in ?? () |
| 775 | #5 0x00007f8671333300 in ?? () |
| 776 | #6 0x0000000000000066 in ?? () |
| 777 | #7 0x0000000000000000 in ?? () |
| 778 | (gdb) s |
| 779 | 1368 return current_uid(); |
| 780 | (gdb) |
| 781 | get_current () at kernel/timer.c:1368 |
| 782 | 1368 return current_uid(); |
| 783 | (gdb) bt |
| 784 | #0 get_current () at kernel/timer.c:1368 |
| 785 | #1 sys_getuid () at kernel/timer.c:1368 |
| 786 | #2 0xffffffff810029eb in ?? () |
| 787 | #3 0x0000000000000206 in ?? () |
| 788 | #4 0x00007ffff56d6ad0 in ?? () |
| 789 | #5 0x00007f8671347210 in ?? () |
| 790 | #6 0x00007f8671333300 in ?? () |
| 791 | #7 0x0000000000000066 in ?? () |
| 792 | #8 0x0000000000000000 in ?? () |
| 793 | (gdb) l |
| 794 | 1363 } |
| 795 | 1364 |
| 796 | 1365 SYSCALL_DEFINE0(getuid) |
| 797 | 1366 { |
| 798 | 1367 /* Only we change this so SMP safe */ |
| 799 | 1368 return current_uid(); |
| 800 | 1369 } |
| 801 | 1370 |
| 802 | 1371 SYSCALL_DEFINE0(geteuid) |
| 803 | 1372 { |
| 804 | (gdb) s |
| 805 | sys_getuid () at kernel/timer.c:1369 |
| 806 | 1369 } |
| 807 | (gdb) bt |
| 808 | #0 sys_getuid () at kernel/timer.c:1369 |
| 809 | #1 0xffffffff810029eb in ?? () |
| 810 | #2 0x0000000000000206 in ?? () |
| 811 | #3 0x00007ffff56d6ad0 in ?? () |
| 812 | #4 0x00007f8671347210 in ?? () |
| 813 | #5 0x00007f8671333300 in ?? () |
| 814 | #6 0x0000000000000066 in ?? () |
| 815 | #7 0x0000000000000000 in ?? () |
| 816 | (gdb) l |
| 817 | 1364 |
| 818 | 1365 SYSCALL_DEFINE0(getuid) |
| 819 | 1366 { |
| 820 | 1367 /* Only we change this so SMP safe */ |
| 821 | 1368 return current_uid(); |
| 822 | 1369 } |
| 823 | 1370 |
| 824 | 1371 SYSCALL_DEFINE0(geteuid) |
| 825 | 1372 { |
| 826 | 1373 /* Only we change this so SMP safe */ |
| 827 | (gdb) p current_uid |
| 828 | No symbol "current_uid" in current context. |
| 829 | (gdb) ptype current_uid |
| 830 | No symbol "current_uid" in current context. |
| 831 | (gdb) s |
| 832 | |
| 833 | Breakpoint 1, sys_getuid () at kernel/timer.c:1366 |
| 834 | 1366 { |
| 835 | (gdb) detach |
| 836 | Ending remote debugging. |
| 837 | }}} |
| 838 | |
| 839 | * include/linux/cred.h |
| 840 | {{{#!cc |
| 841 | #define current_cred_xxx(xxx) \ |
| 842 | ({ \ |
| 843 | current->cred->xxx; \ |
| 844 | }) |
| 845 | |
| 846 | #define current_uid() (current_cred_xxx(uid)) |
| 847 | #define current_gid() (current_cred_xxx(gid)) |
| 848 | |
| 849 | (snip) |
| 850 | }}} |
| 851 | |
| 852 | * ptype of current_task->cred |
| 853 | {{{ |
| 854 | (gdb) ptype current_task->cred |
| 855 | type = const struct cred { |
| 856 | atomic_t usage; |
| 857 | uid_t uid; |
| 858 | gid_t gid; |
| 859 | uid_t suid; |
| 860 | gid_t sgid; |
| 861 | uid_t euid; |
| 862 | gid_t egid; |
| 863 | uid_t fsuid; |
| 864 | gid_t fsgid; |
| 865 | unsigned int securebits; |
| 866 | kernel_cap_t cap_inheritable; |
| 867 | kernel_cap_t cap_permitted; |
| 868 | kernel_cap_t cap_effective; |
| 869 | kernel_cap_t cap_bset; |
| 870 | unsigned char jit_keyring; |
| 871 | struct key *thread_keyring; |
| 872 | struct key *request_key_auth; |
| 873 | struct thread_group_cred *tgcred; |
| 874 | void *security; |
| 875 | struct user_struct *user; |
| 876 | struct group_info *group_info; |
| 877 | struct rcu_head rcu; |
| 878 | } * |
| 879 | (gdb) ptype current_task->cred->uid |
| 880 | type = unsigned int |
| 881 | }}} |