首页 \ 问答 \ 内存泄露在OpenSSL中?(Memory leak in OpenSSL?)

内存泄露在OpenSSL中?(Memory leak in OpenSSL?)

我使用以下代码检查签名。 为简单起见,公钥,消息和签名在main中初始化。

#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/evp.h>

int main(){
    const unsigned char key[] = "-----BEGIN PUBLIC KEY-----\n"
                "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALulMfYFyX1kSm7oUxZyCWWhrBBcWrRA\n"
                "V7LSz1PzMPxoxG3KS8H7PRKFkIk42yM8/vhobmcCmj7UM5572wWch50CAwEAAQ==\n"
                "-----END PUBLIC KEY-----\n";
    unsigned int key_len = sizeof(key);

    const unsigned char data[] = {0x6d,0x65,0x73,0x65,0x0a};
    unsigned int data_len = sizeof(data);

    const unsigned char sig[] = {0xa9,0x29,0x81,0x07,0x8c,0xeb,0xf0,0x1b,0x2a,0x31,0xe5,0x60,0x94,0x8a,0x47,0x94,0x3a,0x8f,0x6b,
                0x4e,0x85,0xb9,0xe7,0xe5,0x4a,0x6c,0x56,0x46,0xd1,0x80,0x15,0x57,0xce,0xcb,0x0a,0x3a,0x67,0x15,0xed,
                0x68,0x03,0x58,0x99,0xa4,0x73,0x61,0xe3,0x30,0x85,0xff,0x89,0x7e,0x32,0xef,0x16,0xec,0x23,0x7f,0x14,
                0xde,0xbf,0x53,0xe0,0x3a};
    unsigned int sig_len = sizeof(sig);


    EVP_PKEY* evp_pubkey = EVP_PKEY_new();
        RSA* rsa_pubkey = NULL;
    EVP_MD_CTX ctx;

    BIO* bufio = BIO_new_mem_buf((void*)key, key_len);
    if(bufio == NULL){  
        fprintf(stderr, "BIO not created.\n");
        EVP_PKEY_free(evp_pubkey);
        BIO_free(bufio);
        return 3;
    }

    evp_pubkey = PEM_read_bio_PUBKEY(bufio, NULL, NULL, NULL);
    if(evp_pubkey == NULL){ 
        fprintf(stderr, "evp_pubkey not created.\n");
        EVP_PKEY_free(evp_pubkey);
        BIO_free(bufio);
        return 4;
    }

    EVP_MD_CTX_init(&ctx);  

    if (!EVP_VerifyInit(&ctx, EVP_sha256())) {
            fprintf(stderr, "EVP_SignInit: failed.\n");
            EVP_PKEY_free(evp_pubkey);
            BIO_free(bufio);
            return 5;
        }

        if (!EVP_VerifyUpdate(&ctx, data, data_len)) {
                fprintf(stderr, "EVP_SignUpdate: failed.\n");
                EVP_PKEY_free(evp_pubkey);
                BIO_free(bufio);
                return 6;
        }

        if (!EVP_VerifyFinal(&ctx, sig, sig_len, evp_pubkey)) {
            fprintf(stderr, "EVP_VerifyFinal: failed.\n");
            EVP_PKEY_free(evp_pubkey);
            BIO_free(bufio);
            return 7;
        }

        EVP_PKEY_free(evp_pubkey);
        BIO_free(bufio);

        return 0;
}

在使用gcc evp.c -lssl -lcrypto编译并运行valgrind --tool=memcheck --leak-check=full ./a.out之后,我得到一些内存泄漏:

==7492== 56 bytes in 1 blocks are definitely lost in loss record 9 of 12
==7492==    at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7492==    by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x4F5D459: EVP_PKEY_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x400C4C: main (in a.out)
==7492== 
==7492== 120 bytes in 1 blocks are definitely lost in loss record 10 of 12
==7492==    at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7492==    by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x4F55678: EVP_DigestInit_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x400D6A: main (in a.out)

难道我做错了什么?


I use the following code to check a signature. For simplicity public key, message and signature are initialized in main.

#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/evp.h>

int main(){
    const unsigned char key[] = "-----BEGIN PUBLIC KEY-----\n"
                "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALulMfYFyX1kSm7oUxZyCWWhrBBcWrRA\n"
                "V7LSz1PzMPxoxG3KS8H7PRKFkIk42yM8/vhobmcCmj7UM5572wWch50CAwEAAQ==\n"
                "-----END PUBLIC KEY-----\n";
    unsigned int key_len = sizeof(key);

    const unsigned char data[] = {0x6d,0x65,0x73,0x65,0x0a};
    unsigned int data_len = sizeof(data);

    const unsigned char sig[] = {0xa9,0x29,0x81,0x07,0x8c,0xeb,0xf0,0x1b,0x2a,0x31,0xe5,0x60,0x94,0x8a,0x47,0x94,0x3a,0x8f,0x6b,
                0x4e,0x85,0xb9,0xe7,0xe5,0x4a,0x6c,0x56,0x46,0xd1,0x80,0x15,0x57,0xce,0xcb,0x0a,0x3a,0x67,0x15,0xed,
                0x68,0x03,0x58,0x99,0xa4,0x73,0x61,0xe3,0x30,0x85,0xff,0x89,0x7e,0x32,0xef,0x16,0xec,0x23,0x7f,0x14,
                0xde,0xbf,0x53,0xe0,0x3a};
    unsigned int sig_len = sizeof(sig);


    EVP_PKEY* evp_pubkey = EVP_PKEY_new();
        RSA* rsa_pubkey = NULL;
    EVP_MD_CTX ctx;

    BIO* bufio = BIO_new_mem_buf((void*)key, key_len);
    if(bufio == NULL){  
        fprintf(stderr, "BIO not created.\n");
        EVP_PKEY_free(evp_pubkey);
        BIO_free(bufio);
        return 3;
    }

    evp_pubkey = PEM_read_bio_PUBKEY(bufio, NULL, NULL, NULL);
    if(evp_pubkey == NULL){ 
        fprintf(stderr, "evp_pubkey not created.\n");
        EVP_PKEY_free(evp_pubkey);
        BIO_free(bufio);
        return 4;
    }

    EVP_MD_CTX_init(&ctx);  

    if (!EVP_VerifyInit(&ctx, EVP_sha256())) {
            fprintf(stderr, "EVP_SignInit: failed.\n");
            EVP_PKEY_free(evp_pubkey);
            BIO_free(bufio);
            return 5;
        }

        if (!EVP_VerifyUpdate(&ctx, data, data_len)) {
                fprintf(stderr, "EVP_SignUpdate: failed.\n");
                EVP_PKEY_free(evp_pubkey);
                BIO_free(bufio);
                return 6;
        }

        if (!EVP_VerifyFinal(&ctx, sig, sig_len, evp_pubkey)) {
            fprintf(stderr, "EVP_VerifyFinal: failed.\n");
            EVP_PKEY_free(evp_pubkey);
            BIO_free(bufio);
            return 7;
        }

        EVP_PKEY_free(evp_pubkey);
        BIO_free(bufio);

        return 0;
}

After compiling with gcc evp.c -lssl -lcrypto and running valgrind --tool=memcheck --leak-check=full ./a.out on it I get some memory leaks:

==7492== 56 bytes in 1 blocks are definitely lost in loss record 9 of 12
==7492==    at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7492==    by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x4F5D459: EVP_PKEY_new (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x400C4C: main (in a.out)
==7492== 
==7492== 120 bytes in 1 blocks are definitely lost in loss record 10 of 12
==7492==    at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7492==    by 0x4E9AD77: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x4F55678: EVP_DigestInit_ex (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==7492==    by 0x400D6A: main (in a.out)

Am I doing something wrong?


原文:https://stackoverflow.com/questions/34794272
更新时间:2022-04-18 16:04

最满意答案

编辑ProjectB的代码文件...

或者,转到构建设置,搜索“前缀”并编辑告诉项目应该使用的.pch文件所在的路径。


Edit the code file for ProjectB...

Alternatively, go to the build settings, search for 'prefix' and edit the path which tells the project where the .pch file that it should use is.

相关问答

更多
  • 从设备获取framework.jar并将其放入您的项目中。 看起来你想用隐藏的API编译你的项目。 adb pull /system/framework/framework.jar 以下是参考资料: http : //devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/ Get framework.jar from the device and put it in your project. It ...
  • 确保文件位于项目文件夹中。 编译器不使用添加到项目中的所有标题,而是使用标题搜索路径中可以找到的所有标题。 默认情况下,该路径被设置为项目文件夹。 因此,如果您的标题在搜索路径之外,编译器将无法看到它。 了解C编译器不仅仅能够将文件添加到Xcode项目中,而是有时需要使用文件结构(例如,用于头文件),这一点很重要。 一些很难找到的错误可能是由这个引起的 - 例如,当有两个相同名称的头文件时。 Make sure the file is located in the project folder. The c ...
  • 一旦我使用cocoapods安装所需的库,这个工作。 您可以在这里学习如何安装cocoapods。 我仍然坚持认为如果没有cocoapods我无法弄清楚如何安装这个并且我发现卸载一个添加了cocoapods的库更加痛苦而不是它的价值。 This worked once I used cocoapods to install the desired library. You can learn how to install cocoapods here. I'm still put-out that I co ...
  • 您可以尝试不同的解决方案: - 尝试其他版本的Hadoop库 - 删除用户文件夹中的完整.m2存储库并重新下载库 - 检查您的项目是否在您的IDE中配置为Maven项目 祝你好运! I tried all the possible options like removing old .m2 directory, update project, checked settings.xml etc but none of them seemed to help. So I finally installed th ...
  • 我有同样的问题。 最后我在我的subspec添加了subspec 。 它更像是一个为我工作的工作(我不称之为解决方案)。 根据文件: public_header_files A list of file patterns that should be used as public headers. These are the headers that will be exposed to the user’s project and from which documentation will b ...
  • 在子项目中使用main项目的.h文件有点奇怪。 这将导致两个项目不能再分开。 那不是为之设计的项目/子项目结构。 那么,为什么不将所有这些类从主项目移到子项目中,如果是这样,你仍然可以在主项目中使用它们? 无论如何,如果你坚持在主项目中保留这些类,你是否尝试将这些.h文件添加到子项目中? 您不需要将它们复制到您的子项目中,只需将它们添加为参考即可。 That's a little bit weird to use main project's .h file in a sub-project. It wil ...
  • 在桥接头中导入头文件名时,需要引用头文件名。 喜欢这个: #import "UIImageView+WebCache.h"; 这与在objc中导入头文件的语法完全相同。 You need to quote the header file name when importing it in the bridging header. Like this: #import "UIImageView+WebCache.h"; This is exactly the same syntax as importin ...
  • 编辑ProjectB的代码文件... 或者,转到构建设置,搜索“前缀”并编辑告诉项目应该使用的.pch文件所在的路径。 Edit the code file for ProjectB... Alternatively, go to the build settings, search for 'prefix' and edit the path which tells the project where the .pch file that it should use is.
  • 好的,那是我的错,我应该用 export PYTHONPATH=/Users/roger/python/base_crawler_py 代替 PYTHONPATH=/Users/roger/python/base_crawler_py OK, that is my fault, I should use export PYTHONPATH=/Users/roger/python/base_crawler_py instead of PYTHONPATH=/Users/roger/python/base ...
  • 好吧,我觉得现在真的很愚蠢,但我应该尝试过,而不只是在Stack Overflow上问它并期待答案。 并不是说我很懒,但我已经习惯于在Stack Overflow上提问,我的本能就是在遇到问题时立即在Stack上问一个问题,而不是先尝试自己找到解决方案。 回答: 是的有效! 您可以通过桥接头在Swift中使用Objective-C ++类。 现在我没有做任何复杂的事情,只是在Objective-C ++类中实现了一个方法,并将该文件包含在桥接头中并在ViewController.Swift中调用该方法,并且 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)