安全地自动清除析构函数上C ++ std :: string和std :: vector的内容 [英] Automatically clear contents securely of C++ std::string and std::vector on destructor

查看:126
本文介绍了安全地自动清除析构函数上C ++ std :: string和std :: vector的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从std :: string和std :: vector元素存储的内存中安全清除(在简单情况下,将内容设置为零)敏感信息。我的向量还可以包含基本类型(例如std :: vector),因此我猜想清除向量析构函数内的所有包含元素会更加方便,因为原始类型没有析构函数。我无法使用继承覆盖std :: string和std :: vector类,因为这些类型不是为此设计的(没有虚拟析构函数,等等)。还有其他办法吗?我发现一些带有std :: string自动清除功能的帖子,但是示例实现都不完整。目前,我通过在使用完对象后手动将内容设置为零来清除对象,但这非常繁琐。

I would like to securely clear (in simple case, setting contents to zeros) sensitive information from memory stored by std::string and std::vector elements. My vector can contain also primitive types (like std::vector) so I guess it would be more convenient to do clearing of all contained elements inside destructor of vector because primite types don't have destructor. I can't override std::string and std::vector classes with inheritance because those types are not designed for that (no virtual destructor, etc). Is there any other way? I found some posts with std::string automatic clearing but sample implementations there were all incomplete. Currently I clear my objects by manually setting contents to zeros when finished using object but that is very tedious.

编辑:同样,以这种方式清除内存并不安全,因为C ++容器可能在某些操作期间分配/取消分配内存。这就是为什么至少在不使用SSO(短字符串优化)的较长字符串中使用分配器(在类似问题中描述)的唯一方法。

Also clearing memory in this way is not secure as C++ containers could allocate/deallocate memory during some operations. That's why using allocators (described in similar question) could be only way to do that at least for longer strings where SSO (Short String Optimization) is not used.

推荐答案

不,具有讽刺意味的是,面向对象的设计应该恰好支持这种东西。实际上,它是行不通的。

No, the irony is that object-oriented design is supposed to support exactly that sort of thing. In reality it doesn't work.

但是您可以做的是重载new / delete运算符以调用mymalloc(),myshreddingfree(),myshreddingfree,以便擦除释放的块(因此,您需要mymalloc来标记其大小)。有点奇怪,但是C ++允许。

However what you can do is overload the new / delete operators to call mymalloc(), myshreddingfree(), myshreddingfree written so as to wipe the block freed (so you'll need mymalloc to tag it with a size). It's a bit fiddly, but something C++ allows.

这篇关于安全地自动清除析构函数上C ++ std :: string和std :: vector的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆