uaccess.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* uaccess.c: userspace access functions
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/mm.h>
  12. #include <asm/uaccess.h>
  13. /*****************************************************************************/
  14. /*
  15. * copy a null terminated string from userspace
  16. */
  17. long strncpy_from_user(char *dst, const char *src, long count)
  18. {
  19. unsigned long max;
  20. char *p, ch;
  21. long err = -EFAULT;
  22. if (count < 0)
  23. BUG();
  24. p = dst;
  25. #ifndef CONFIG_MMU
  26. if ((unsigned long) src < memory_start)
  27. goto error;
  28. #endif
  29. if ((unsigned long) src >= get_addr_limit())
  30. goto error;
  31. max = get_addr_limit() - (unsigned long) src;
  32. if ((unsigned long) count > max) {
  33. memset(dst + max, 0, count - max);
  34. count = max;
  35. }
  36. err = 0;
  37. for (; count > 0; count--, p++, src++) {
  38. __get_user_asm(err, ch, src, "ub", "=r");
  39. if (err < 0)
  40. goto error;
  41. if (!ch)
  42. break;
  43. *p = ch;
  44. }
  45. err = p - dst; /* return length excluding NUL */
  46. error:
  47. if (count > 0)
  48. memset(p, 0, count); /* clear remainder of buffer [security] */
  49. return err;
  50. } /* end strncpy_from_user() */
  51. /*****************************************************************************/
  52. /*
  53. * Return the size of a string (including the ending 0)
  54. *
  55. * Return 0 on exception, a value greater than N if too long
  56. */
  57. long strnlen_user(const char *src, long count)
  58. {
  59. const char *p;
  60. long err = 0;
  61. char ch;
  62. if (count < 0)
  63. BUG();
  64. #ifndef CONFIG_MMU
  65. if ((unsigned long) src < memory_start)
  66. return 0;
  67. #endif
  68. if ((unsigned long) src >= get_addr_limit())
  69. return 0;
  70. for (p = src; count > 0; count--, p++) {
  71. __get_user_asm(err, ch, p, "ub", "=r");
  72. if (err < 0)
  73. return 0;
  74. if (!ch)
  75. break;
  76. }
  77. return p - src + 1; /* return length including NUL */
  78. } /* end strnlen_user() */