| 1690 | |
| 1691 | == /proc read skeleton == |
| 1692 | * [http://www.ibm.com/developerworks/jp/linux/library/l-proc/ /procファイルシステムを使用したLinuxカーネルへのアクセス] |
| 1693 | * [http://homepage3.nifty.com/rio_i/lab/driver24/007proc.html /proc インタフェースを使う] |
| 1694 | * [http://d.hatena.ne.jp/masami256/20100315/1268662409 procfs経由でデータの読み書き - linuxとかテストとか] |
| 1695 | |
| 1696 | * 参考 |
| 1697 | * sound/core/info.c -> create_proc_entry |
| 1698 | * arch/h8300/kernel/gpio.c -> create_proc_entry |
| 1699 | * arch/arm/mach-bcmring/dma.c -> create_proc_read_entry |
| 1700 | * drivers/nubus/nubus.c -> create_proc_read_entry |
| 1701 | * include/linux/proc_fs.h -> static inline struct proc_dir_entry *create_proc_read_entry |
| 1702 | |
| 1703 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko |
| 1704 | {{{ |
| 1705 | Nov 21 00:33:28 ubuntu-lucid64 kernel: [ 41.547601] stackmod is loaded |
| 1706 | Nov 21 00:33:28 ubuntu-lucid64 kernel: [ 41.547606] stackmod: 128 entry, major is 251, minor is 0 |
| 1707 | Nov 21 00:33:28 ubuntu-lucid64 kernel: [ 41.547615] stackmod: create /proc/stackmod |
| 1708 | Nov 21 00:33:28 ubuntu-lucid64 kernel: [ 41.547617] stackmod is added successfully |
| 1709 | }}} |
| 1710 | |
| 1711 | * s0711489@ubuntu-lucid64:~$ ls -l /proc/ | grep stack |
| 1712 | {{{ |
| 1713 | -r--r--r-- 1 root root 0 2011-11-21 00:34 stackmod |
| 1714 | }}} |
| 1715 | |
| 1716 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 1717 | {{{ |
| 1718 | Nov 21 00:34:21 ubuntu-lucid64 kernel: [ 94.446963] /proc/stackmod is read |
| 1719 | }}} |
| 1720 | |
| 1721 | * s0711489@ubuntu-lucid64:~$ sudo rmmod stackmod.ko |
| 1722 | {{{ |
| 1723 | Nov 21 00:35:43 ubuntu-lucid64 kernel: [ 177.060782] stackmod is unloaded |
| 1724 | }}} |
| 1725 | |
| 1726 | === implement stackmod_proc_read as proc_dir_entry->read_proc === |
| 1727 | * fs/proc/generic.c -> __proc_file_read |
| 1728 | {{{ |
| 1729 | * How to be a proc read function |
| 1730 | }}} |
| 1731 | |
| 1732 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko |
| 1733 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 1734 | {{{ |
| 1735 | 0 stacks |
| 1736 | }}} |
| 1737 | {{{ |
| 1738 | Nov 21 02:30:23 ubuntu-lucid64 kernel: [ 7055.042732] /proc/stackmod is read |
| 1739 | Nov 21 02:30:23 ubuntu-lucid64 kernel: [ 7055.042747] /proc/stackmod is read |
| 1740 | }}} |
| 1741 | |
| 1742 | == kmalloc/kfree -> stack.buffer == |
| 1743 | === research kmalloc === |
| 1744 | * [http://tiki.is.os-omicron.org/tiki.cgi?c=v&p=Linux%2FLKM%2FGDB%A4%F2%BB%C8%A4%C3%A4%C6%A5%B3%A1%BC%A5%C9%A4%F2%C4%C9%A4%C3%A4%AB%A4%B1%A4%EB Omicron Linux/LKM/GDBを使ってコードを追っかける] |
| 1745 | * [http://www.denx.de/wiki/DULG/DebuggingLinuxKernel DebuggingLinuxKernel < DULG < DENX] |
| 1746 | |
| 1747 | ==== without memset ==== |
| 1748 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko |
| 1749 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.text |
| 1750 | {{{ |
| 1751 | 0xffffffffa0056000 |
| 1752 | }}} |
| 1753 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.data |
| 1754 | {{{ |
| 1755 | 0xffffffffa0056458 |
| 1756 | }}} |
| 1757 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.bss |
| 1758 | {{{ |
| 1759 | 0xffffffffa0056690 |
| 1760 | }}} |
| 1761 | |
| 1762 | * gdb |
| 1763 | {{{ |
| 1764 | (gdb) file vmlinux |
| 1765 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| 1766 | (gdb) add-symbol-file ../../04/stackmod/stackmod.o 0xffffffffa0056000 -s .data 0xffffffffa0056458 -s .bss 0xffffffffa0056690 |
| 1767 | add symbol table from file "../../04/stackmod/stackmod.o" at |
| 1768 | .text_addr = 0xffffffffa0056000 |
| 1769 | .data_addr = 0xffffffffa0056458 |
| 1770 | .bss_addr = 0xffffffffa0056690 |
| 1771 | (y or n) y |
| 1772 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/04/stackmod/stackmod.o...done. |
| 1773 | (gdb) l stackmod.c:145 |
| 1774 | |
| 1775 | 140 stack.buffer = (char **) kmalloc(entry * sizeof(char *), GFP_KERNEL); |
| 1776 | 141 if (stack.buffer == NULL) { |
| 1777 | 142 printk(KERN_WARNING MODNAME ": (char **) kmalloc failed\n"); |
| 1778 | 143 ret = -ENOMEM; |
| 1779 | 144 goto error_cdev; |
| 1780 | 145 } |
| 1781 | 146 // memset(stack.buffer, NULL, entry * sizeof(char *)); |
| 1782 | 147 |
| 1783 | 148 printk(KERN_INFO MODNAME " is added successfully\n"); |
| 1784 | 149 |
| 1785 | (gdb) p stack |
| 1786 | Cannot access memory at address 0xffffffffa0056690 |
| 1787 | (gdb) target remote localhost:8864 |
| 1788 | Remote debugging using localhost:8864 |
| 1789 | 0xffffffff810097a9 in native_safe_halt () |
| 1790 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 1791 | 49 asm volatile("sti; hlt": : :"memory"); |
| 1792 | (gdb) p stack |
| 1793 | $1 = {buffer = 0xffff88001b740800, depth = 0, errno = 0} |
| 1794 | (gdb) ptype stack |
| 1795 | type = struct kernel_module_stack { |
| 1796 | char **buffer; |
| 1797 | int depth; |
| 1798 | int errno; |
| 1799 | } |
| 1800 | (gdb) p stack.buffer |
| 1801 | $2 = (char **) 0xffff88001b740800 |
| 1802 | (gdb) p stack.buffer[0] |
| 1803 | $3 = 0xffff88001b744c00 "" |
| 1804 | (gdb) p stack.buffer[1] |
| 1805 | $4 = 0x1000 <Address 0x1000 out of bounds> |
| 1806 | (gdb) p stack.buffer[2] |
| 1807 | $5 = 0xffffea00005f0148 "," |
| 1808 | (gdb) p stack.buffer[3] |
| 1809 | $6 = 0x1000 <Address 0x1000 out of bounds> |
| 1810 | (gdb) p stack.buffer[4] |
| 1811 | $7 = 0xffffea00005f0110 "," |
| 1812 | (gdb) p stack.buffer[5] |
| 1813 | $8 = 0x1000 <Address 0x1000 out of bounds> |
| 1814 | (gdb) p stack.buffer[6] |
| 1815 | $9 = 0xffffea00005f4048 "," |
| 1816 | (gdb) p stack.depth |
| 1817 | $10 = 0 |
| 1818 | (gdb) p/c stack.depth |
| 1819 | $11 = 0 '\000' |
| 1820 | (gdb) p stack.buffer |
| 1821 | $12 = (char **) 0xffff88001b740800 |
| 1822 | (gdb) p/x stack.buffer |
| 1823 | $13 = 0xffff88001b740800 |
| 1824 | (gdb) p/x stack.buffer[0] |
| 1825 | $14 = 0xffff88001b744c00 |
| 1826 | (gdb) p/x stack.buffer[1] |
| 1827 | $15 = 0x1000 |
| 1828 | (gdb) p/x stack.buffer[2] |
| 1829 | $16 = 0xffffea00005f0148 |
| 1830 | (gdb) p/x stack.buffer[3] |
| 1831 | $17 = 0x1000 |
| 1832 | (gdb) p/x stack.buffer[4] |
| 1833 | $18 = 0xffffea00005f0110 |
| 1834 | }}} |
| 1835 | |
| 1836 | ==== with memset ==== |
| 1837 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko |
| 1838 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.text |
| 1839 | {{{ |
| 1840 | 0xffffffffa0056000 |
| 1841 | }}} |
| 1842 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.data |
| 1843 | {{{ |
| 1844 | 0xffffffffa0056458 |
| 1845 | }}} |
| 1846 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.bss |
| 1847 | {{{ |
| 1848 | 0xffffffffa0056690 |
| 1849 | }}} |
| 1850 | |
| 1851 | * gdb |
| 1852 | {{{ |
| 1853 | (gdb) file vmlinux |
| 1854 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| 1855 | (gdb) add-symbol-file ../../04/stackmod/stackmod.o 0xffffffffa0056000 -s .data 0xffffffffa0056458 -s .bss 0xffffffffa0056690 |
| 1856 | add symbol table from file "../../04/stackmod/stackmod.o" at |
| 1857 | .text_addr = 0xffffffffa0056000 |
| 1858 | .data_addr = 0xffffffffa0056458 |
| 1859 | .bss_addr = 0xffffffffa0056690 |
| 1860 | (y or n) y |
| 1861 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/04/stackmod/stackmod.o...done. |
| 1862 | (gdb) l stackmod.c:145 |
| 1863 | |
| 1864 | 140 stack.buffer = (char **) kmalloc(entry * sizeof(char *), GFP_KERNEL); |
| 1865 | 141 if (stack.buffer == NULL) { |
| 1866 | 142 printk(KERN_WARNING MODNAME ": (char **) kmalloc failed\n"); |
| 1867 | 143 ret = -ENOMEM; |
| 1868 | 144 goto error_cdev; |
| 1869 | 145 } |
| 1870 | 146 memset(stack.buffer, (int) NULL, entry * sizeof(char *)); |
| 1871 | 147 |
| 1872 | 148 printk(KERN_INFO MODNAME " is added successfully\n"); |
| 1873 | 149 |
| 1874 | (gdb) target remote localhost:8864 |
| 1875 | Remote debugging using localhost:8864 |
| 1876 | 0xffffffff810097a9 in native_safe_halt () |
| 1877 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 1878 | 49 asm volatile("sti; hlt": : :"memory"); |
| 1879 | (gdb) p stack |
| 1880 | $1 = {buffer = 0xffff88001b5cb400, depth = 0, errno = 0} |
| 1881 | (gdb) p stack.buffer |
| 1882 | $2 = (char **) 0xffff88001b5cb400 |
| 1883 | (gdb) p/c stack.buffer |
| 1884 | $3 = 0 '\000' |
| 1885 | (gdb) p/x stack.buffer |
| 1886 | $4 = 0xffff88001b5cb400 |
| 1887 | (gdb) p/x stack.buffer[0] |
| 1888 | $5 = 0x0 |
| 1889 | (gdb) p/x stack.buffer[1] |
| 1890 | $6 = 0x0 |
| 1891 | (gdb) p/x stack.buffer[2] |
| 1892 | $7 = 0x0 |
| 1893 | (gdb) p/x stack.buffer[3] |
| 1894 | $8 = 0x0 |
| 1895 | (gdb) p/x stack.buffer[4] |
| 1896 | $9 = 0x0 |
| 1897 | (gdb) p/x stack.buffer[5] |
| 1898 | $10 = 0x0 |
| 1899 | (gdb) p/x stack.buffer[127] |
| 1900 | $11 = 0x0 |
| 1901 | (gdb) p/x stack.buffer[128] |
| 1902 | $12 = 0xffff88001b5cb000 |
| 1903 | (gdb) p/x stack.buffer[129] |
| 1904 | $13 = 0x0 |
| 1905 | (gdb) p/x stack.buffer[130] |
| 1906 | $14 = 0xffffffff00000000 |
| 1907 | (gdb) p/x stack.buffer[126] |
| 1908 | $15 = 0x0 |
| 1909 | (gdb) p/x stack.buffer[125] |
| 1910 | $16 = 0x0 |
| 1911 | }}} |
| 1912 | |
| 1913 | ===== much more entry with memset ===== |
| 1914 | * s0711489@ubuntu-lucid64:~$ sudo rmmod stackmod.ko |
| 1915 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko entry=1024 |
| 1916 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.text |
| 1917 | {{{ |
| 1918 | 0xffffffffa005c000 |
| 1919 | }}} |
| 1920 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.data |
| 1921 | {{{ |
| 1922 | 0xffffffffa005c458 |
| 1923 | }}} |
| 1924 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.bss |
| 1925 | {{{ |
| 1926 | 0xffffffffa005c690 |
| 1927 | }}} |
| 1928 | |
| 1929 | * gdb |
| 1930 | {{{ |
| 1931 | (gdb) file vmlinux |
| 1932 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| 1933 | (gdb) add-symbol-file ../../04/stackmod/stackmod.o 0xffffffffa005c000 -s .data 0xffffffffa005c458 -s .bss 0xffffffffa005c690 |
| 1934 | add symbol table from file "../../04/stackmod/stackmod.o" at |
| 1935 | .text_addr = 0xffffffffa005c000 |
| 1936 | .data_addr = 0xffffffffa005c458 |
| 1937 | .bss_addr = 0xffffffffa005c690 |
| 1938 | (y or n) y |
| 1939 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/04/stackmod/stackmod.o...done. |
| 1940 | (gdb) target remote localhost:8864 |
| 1941 | Remote debugging using localhost:8864 |
| 1942 | 0xffffffff810097a9 in native_safe_halt () |
| 1943 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 1944 | 49 asm volatile("sti; hlt": : :"memory"); |
| 1945 | (gdb) p stack |
| 1946 | $1 = {buffer = 0xffff88001af16000, depth = 0, errno = 0} |
| 1947 | (gdb) p entry |
| 1948 | $2 = 1024 |
| 1949 | (gdb) p stack.buffer |
| 1950 | $3 = (char **) 0xffff88001af16000 |
| 1951 | (gdb) p/x stack.buffer |
| 1952 | $4 = 0xffff88001af16000 |
| 1953 | (gdb) p/x stack.buffer[0] |
| 1954 | $5 = 0x0 |
| 1955 | (gdb) p/x stack.buffer[127] |
| 1956 | $6 = 0x0 |
| 1957 | (gdb) p/x stack.buffer[128] |
| 1958 | $7 = 0x0 |
| 1959 | (gdb) p/x stack.buffer[1020] |
| 1960 | $8 = 0x0 |
| 1961 | (gdb) p/x stack.buffer[1021] |
| 1962 | $9 = 0x0 |
| 1963 | (gdb) p/x stack.buffer[1022] |
| 1964 | $10 = 0x0 |
| 1965 | (gdb) p/x stack.buffer[1023] |
| 1966 | $11 = 0x0 |
| 1967 | (gdb) p/x stack.buffer[1024] |
| 1968 | $12 = 0xc7c7485500c3c9a0 |
| 1969 | (gdb) p/x stack.buffer[1025] |
| 1970 | $13 = 0xe8e58948a001a000 |
| 1971 | (gdb) p/x stack.buffer[1026] |
| 1972 | $14 = 0x4855c3c9e11ca49c |
| 1973 | }}} |