From 9c53a2fab3418baf84d5148e4ff39f5065eb64bb Mon Sep 17 00:00:00 2001 From: Sangjin Lee Date: Fri, 21 Jul 2017 13:45:32 +0900 Subject: [PATCH] dump: add function to check whether file is link or not According to security code guide, before open the file for write it should be check the file is symbolic link. Change-Id: I8248e58292e17d67f0381e50a56571902f4fb8a2 --- src/tbm_surface_internal.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/tbm_surface_internal.c b/src/tbm_surface_internal.c index 09c298b..e03570d 100644 --- a/src/tbm_surface_internal.c +++ b/src/tbm_surface_internal.c @@ -369,6 +369,29 @@ _tbm_surface_internal_destroy(tbm_surface_h surface) } } +static int +_tbm_surface_check_file_is_valid(const char* path, int del_link) +{ + char *real_path; + + if (!path) + return 0; + + real_path = realpath(path, NULL); + if (real_path && strncmp(path, real_path, strlen(path))) { + if (del_link) + unlink(path); + free(real_path); + + return 0; + } + + if (real_path) + free(real_path); + + return 1; +} + int tbm_surface_internal_is_valid(tbm_surface_h surface) { @@ -1502,10 +1525,15 @@ static void _tbm_surface_internal_dump_file_raw(const char *file, void *data1, int size1, void *data2, int size2, void *data3, int size3) { - FILE *fp = fopen(file, "w+"); - TBM_RETURN_IF_FAIL(fp != NULL); + FILE *fp; unsigned int *blocks; + if (!_tbm_surface_check_file_is_valid(file, 1)) + TBM_LOG_E("%s is symbolic link\n", file); + + fp = fopen(file, "w+"); + TBM_RETURN_IF_FAIL(fp != NULL); + blocks = (unsigned int *)data1; fwrite(blocks, 1, size1, fp); @@ -1526,12 +1554,17 @@ static void _tbm_surface_internal_dump_file_png(const char *file, const void *data, int width, int height) { unsigned int *blocks = (unsigned int *)data; - FILE *fp = fopen(file, "wb"); - TBM_RETURN_IF_FAIL(fp != NULL); + FILE *fp; const int pixel_size = 4; // RGBA png_bytep *row_pointers; int depth = 8, y; + if (!_tbm_surface_check_file_is_valid(file, 1)) + TBM_LOG_E("%s is symbolic link\n", file); + + fp = fopen(file, "wb"); + TBM_RETURN_IF_FAIL(fp != NULL); + png_structp pPngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!pPngStruct) { -- 2.7.4