Sử dụng mã gốc
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Danh mục OWASP: MASVS-CODE: Chất lượng mã
Tổng quan
Các ứng dụng Android có thể tận dụng mã gốc được viết bằng các ngôn ngữ như C và C++ cho các chức năng cụ thể. Tuy nhiên, khi một ứng dụng sử dụng Giao diện gốc Java (JNI) để tương tác với mã gốc này, ứng dụng đó có thể bị lộ các lỗ hổng như tràn bộ đệm và các vấn đề khác có thể xuất hiện trong quá trình triển khai mã gốc.
Tác động
Mặc dù có những tác động rất tích cực như tối ưu hoá hiệu suất và làm rối mã nguồn, nhưng việc sử dụng mã gốc trong các ứng dụng Android có thể gây ra những tác động tiêu cực đến bảo mật. Các ngôn ngữ mã gốc như C/C++ thiếu các tính năng an toàn về bộ nhớ của Java/Kotlin, khiến chúng dễ bị các lỗ hổng như tràn bộ đệm, lỗi use-after-free và các vấn đề hỏng bộ nhớ khác – dẫn đến sự cố hoặc thực thi mã tuỳ ý. Ngoài ra, nếu có lỗ hổng trong thành phần mã gốc, thì lỗ hổng đó có thể làm hỏng toàn bộ ứng dụng, ngay cả khi phần còn lại được viết một cách an toàn bằng Java.
Giải pháp giảm thiểu
Hướng dẫn phát triển và lập trình
- Nguyên tắc lập trình an toàn: Đối với các dự án C/C++, hãy tuân thủ các tiêu chuẩn lập trình an toàn đã thiết lập (ví dụ: CERT, OWASP) để giảm thiểu các lỗ hổng như tràn bộ đệm, tràn số nguyên và các cuộc tấn công chuỗi định dạng. Ưu tiên các thư viện như Abseil nổi tiếng về chất lượng và bảo mật. Bất cứ khi nào có thể, hãy cân nhắc việc sử dụng các ngôn ngữ an toàn đối với bộ nhớ như Rust. Ngôn ngữ này mang lại hiệu suất tương đương với C/C++.
- Xác thực dữ liệu đầu vào: Xác thực nghiêm ngặt tất cả dữ liệu đầu vào nhận được từ các nguồn bên ngoài, bao gồm cả dữ liệu đầu vào của người dùng, dữ liệu mạng và tệp để ngăn chặn các cuộc tấn công chèn và các lỗ hổng khác.
Tăng cường các lựa chọn biên dịch
Bạn có thể tăng cường bảo mật cho các thư viện gốc sử dụng định dạng ELF trước một loạt các lỗ hổng bằng cách kích hoạt các cơ chế bảo vệ như bảo vệ ngăn xếp (Canary), chỉ có thể đọc khi di chuyển (RELRO), ngăn chặn thực thi dữ liệu (NX) và tệp thực thi độc lập với vị trí (PIE). Rất thuận tiện khi các tuỳ chọn biên dịch Android NDK đã bật tất cả các biện pháp bảo vệ này theo mặc định.
Để xác minh việc triển khai các cơ chế bảo mật này trong tệp nhị phân, bạn có thể sử dụng các công cụ như hardening-check
hoặc pwntools
.
Bash
$ pwn checksec --file path/to/libnativecode.so
Arch: aarch64-64-little
RELRO: Full RELRO
Stack: Canary found
NX: NX enabled
PIE: PIE enabled
Xác minh thư viện bên thứ ba không bị lỗ hổng
Khi chọn thư viện bên thứ ba, hãy ưu tiên sử dụng những thư viện có uy tín vững chắc trong cộng đồng phát triển. Các tài nguyên như Chỉ mục SDK của Google Play có thể giúp bạn xác định các thư viện có uy tín và đáng tin cậy. Đảm bảo bạn luôn cập nhật thư viện lên phiên bản mới nhất và chủ động tìm kiếm mọi lỗ hổng đã biết liên quan đến thư viện bằng các tài nguyên như cơ sở dữ liệu của Exploit-DB. Việc tìm kiếm trên web bằng các từ khoá như [library_name] vulnerability
hoặc [library_name] CVE
có thể tiết lộ thông tin bảo mật quan trọng.
Tài nguyên
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],null,["# Use of native code\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\nOverview\n--------\n\nAndroid applications can take advantage of native code written in languages like\nC and C++ for specific functionalities. However, when an application utilizes\nthe Java Native Interface (JNI) to interact with this native code, it\npotentially exposes itself to vulnerabilities like buffer overflows and other\nissues that may be present in the native code implementation.\n\nImpact\n------\n\nDespite very positive impacts such as performance optimization and obfuscation,\nutilizing native code in Android applications can have negative security\nimpacts. Native code languages like C/C++ lack the memory safety features of\nJava/Kotlin, making them susceptible to vulnerabilities like buffer overflows,\nuse-after-free errors, and other memory corruption issues -- leading to crashes\nor arbitrary code execution. Additionally, if a vulnerability exists in the\nnative code component, it can potentially compromise the entire application,\neven if the rest is written securely in Java.\n\nMitigations\n-----------\n\n### Development and coding guidance\n\n- **Secure Coding Guidelines**: For C/C++ projects, adhere to established secure coding standards (e.g., CERT, OWASP) to mitigate vulnerabilities like buffer overflows, integer overflows, and format string attacks. Prioritize libraries like Abseil known for quality and security. Whenever possible, consider adopting memory-safe languages like Rust, which offer performance comparable to C/C++.\n- **Input Validation**: Rigorously validate all input data received from external sources, including user input, network data, and files, to prevent injection attacks and other vulnerabilities.\n\n### Harden the compilation options\n\nNative libraries utilizing the ELF format can be hardened against a range of\nvulnerabilities by activating protective mechanisms like stack protection\n(Canary), relocation read-only (RELRO), data execution prevention (NX), and\nposition-independent executables (PIE). Conveniently, the Android NDK\ncompilation options already enable all these protections by default.\n\nTo verify the implementation of these security mechanisms within a binary, you\ncan employ tools like `hardening-check` or `pwntools`. \n\n### Bash\n\n $ pwn checksec --file path/to/libnativecode.so\n Arch: aarch64-64-little\n RELRO: Full RELRO\n Stack: Canary found\n NX: NX enabled\n PIE: PIE enabled\n\n### Verify third-party libraries are not vulnerable\n\nWhen choosing third-party libraries, prioritize using those with a solid\nreputation in the development community. Resources like the [Google Play SDK\nIndex](https://play.google.com/sdks) can help you identify well-regarded and trustworthy libraries. Ensure\nyou keep the libraries updated to the latest versions and proactively search for\nany known vulnerabilities related to them using resources like the databases\nfrom [Exploit-DB](https://www.exploit-db.com/). A web search using keywords like\n`[library_name] vulnerability` or `[library_name] CVE` can reveal critical\nsecurity information.\n\nResources\n---------\n\n- [CWE-111: Direct Use of Unsafe JNI](https://cwe.mitre.org/data/definitions/111.html)\n- [Exploit database](https://www.exploit-db.com/)\n- [Check binaries for security hardening features](https://www.systutorials.com/docs/linux/man/1-hardening-check/)\n- [Check binary security settings with pwntools](https://docs.pwntools.com/en/stable/commandline.html#pwn-checksec)\n- [Linux binary security hardening](https://medium.com/@n80fr1n60/linux-binary-security-hardening-1434e89a2525)\n- [Hardening ELF binaries using Relocation Read-Only (RELRO)](https://www.redhat.com/fr/blog/hardening-elf-binaries-using-relocation-read-only-relro)\n- [OWASP binary protection mechanisms](https://mas.owasp.org/MASTG/Android/0x05i-Testing-Code-Quality-and-Build-Settings/#binary-protection-mechanisms)\n- [SEI CERT Coding Standards](https://wiki.sei.cmu.edu/confluence/display/seccode/SEI+CERT+Coding+Standards)\n- [OWASP Developer Guide](https://owasp.org/www-project-developer-guide/release/)\n- [Google Play SDK Index](https://play.google.com/sdks)\n- [Android NDK](/ndk)\n- [Android Rust introduction](https://source.android.com/docs/setup/build/rust/building-rust-modules/overview)\n- [Abseil (C++ Common Libraries)](https://github.com/abseil/abseil-cpp)\n- [PIE is enforced by the linker](https://cs.android.com/android/platform/superproject/main/+/main:bionic/linker/linker_main.cpp;l=425?q=linker_main&ss=android%2Fplatform%2Fsuperproject%2Fmain)"]]