Title: Embedded null characters can lead to bugs or even security vulnerabilities · Issue #111656 · python/cpython · GitHub
Open Graph Title: Embedded null characters can lead to bugs or even security vulnerabilities · Issue #111656 · python/cpython
X Title: Embedded null characters can lead to bugs or even security vulnerabilities · Issue #111656 · python/cpython
Description: I just modified PyUnicode_AsUTF8() of the C API to raise an exception if a string contains an embedded null character to reduce the risk of security vulnerabilities. PyUnicode_AsUTF8() caller expects a string terminated by a null byte. I...
Open Graph Description: I just modified PyUnicode_AsUTF8() of the C API to raise an exception if a string contains an embedded null character to reduce the risk of security vulnerabilities. PyUnicode_AsUTF8() caller expec...
X Description: I just modified PyUnicode_AsUTF8() of the C API to raise an exception if a string contains an embedded null character to reduce the risk of security vulnerabilities. PyUnicode_AsUTF8() caller expec...
Opengraph URL: https://github.com/python/cpython/issues/111656
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Embedded null characters can lead to bugs or even security vulnerabilities","articleBody":"I just modified PyUnicode_AsUTF8() of the C API to raise an exception if a string contains an embedded null character to reduce the risk of security vulnerabilities. PyUnicode_AsUTF8() caller expects a string terminated by a null byte. If the UTF-8 encoded string contains embedded null byte, the caller is likely to truncate the string without knowing that there are more bytes after \"the first\" null byte.\r\n\r\nSee: https://owasp.org/www-community/attacks/Embedding_Null_Code\r\n\r\nIt's not only about security issue, it can also just be seen as a bug: unwanted behavior.\r\n\r\nPrevious issues:\r\n\r\n* #111089\r\n* #106844\r\n* #106242 -- CVE-2023-41105\r\n* #103223\r\n* #84335\r\n* #73228\r\n* #66411\r\n* #65346\r\n* #57826\r\n\r\nDiscussions:\r\n\r\n* 2014: https://mail.python.org/archives/list/python-dev@python.org/thread/MZDL7FZZMRSW5MTIHLSA6ANNMCV7EEZN/\r\n\r\n---\r\n\r\nExample with Python 3.12:\r\n\r\n```py\r\nimport ctypes\r\n\r\nlibc = ctypes.cdll.LoadLibrary('libc.so.6')\r\nprintf = libc.printf\r\nPyUnicode_AsUTF8 = ctypes.pythonapi.PyUnicode_AsUTF8\r\nPyUnicode_AsUTF8.argtypes = (ctypes.py_object,)\r\nPyUnicode_AsUTF8.restype = ctypes.c_char_p\r\n\r\nmy_string = \"World\\0truncated string\"\r\nprintf(b\"Hello %s\\n\", PyUnicode_AsUTF8(my_string))\r\n```\r\n\r\nOutput:\r\n\r\n```\r\nHello World\r\n```\r\n\r\nThe ``truncated string`` part is silently ignored!\r\n\r\n---\r\n\r\nMultiple functions were modified in the past to prevent this problem. Examples:\r\n\r\n* _dbm.open(): check *filename*\r\n* _gdbm.open(): check *filename*\r\n* `PyBytes_AsStringAndSize(str, NULL)`\r\n* grp.getgrnam(): check *name*\r\n* pwd.getpwnam(): check *name*\r\n* _locale.strxfrm(): check argument\r\n* path_converter() of the os module: basically any filename and path\r\n* PyUnicode_AsWideCharString()\r\n* os.putenv()\r\n* _posixsubprocess.fork_exec(): executable_list\r\n* _struct.Struct: check *format*\r\n* _tkinter SetVar() and varname_converter()\r\n* _winapi.CreateProcess() getenvironment()\r\n* PyUnicode_EncodeLocale()\r\n* PyUnicode_EncodeFSDefault()\r\n* unicode_decode_locale()\r\n* PyUnicode_FSConverter()\r\n* PyUnicode_DecodeLocale()\r\n* PyUnicode_DecodeLocaleAndSize()\r\n* PyUnicode_FSDecoder()\r\n* PyUnicode_AsUTF8() -- recently modified\r\n* _Py_stat(): check *path*\r\n* getargs.c: 's', 'y' and 'z' formats\r\n\r\nThere are exceptions which accept embedded null bytes/characters:\r\n\r\n* socket: AF_UNIX socket name","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2023-11-02T17:10:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/111656/cpython/issues/111656"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e0e7710c-3619-1cc5-67b0-6ca67ae701f3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D1DE:3366E4:4BD31B:642ABE:6A5BD0AD |
| html-safe-nonce | 884b3236293f71accb507e0e014817b1fd5eeb7938a31d2770b2ae2b4a0e4551 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMURFOjMzNjZFNDo0QkQzMUI6NjQyQUJFOjZBNUJEMEFEIiwidmlzaXRvcl9pZCI6Ijg2OTQ3Mzc0NzgwNDg1MzQ3MDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 388570d7d8d88156b393e08d005cd570e62405b7faf1a5bc0b97332ef95fda7a |
| hovercard-subject-tag | issue:1974695604 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/111656/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f621371cbbe51dae383f74aefc5f5d4cb7d3c6bf8999b25aa536d7a785ca706/python/cpython/issues/111656 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f621371cbbe51dae383f74aefc5f5d4cb7d3c6bf8999b25aa536d7a785ca706/python/cpython/issues/111656 |
| og:image:alt | I just modified PyUnicode_AsUTF8() of the C API to raise an exception if a string contains an embedded null character to reduce the risk of security vulnerabilities. PyUnicode_AsUTF8() caller expec... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width