菜鸟教程小白 发表于 2022-12-13 04:35:05

ios - OBJ-C 在取消之前删除 NSData 内容


                                            <p><p>出于安全原因,我们需要始终从内存中删除敏感数据。
通常这不是我在 IOS 中看到的,但对于需要扩展安全性的应用程序来说,这非常重要。</p>

<p>如果 NSData 和 NSString 对象通常需要删除的数据(指向 nil 不会删除数据,这是一个安全漏洞)</p>

<p>我已经设法用下面的代码删除了我的 NSStrings(当密码是 NSString 时):</p>

<pre><code>unsigned char *charPass;
if (password != nil) {
    charPass = (unsigned char*) CFStringGetCStringPtr((CFStringRef) password, CFStringGetSystemEncoding());
    memset(charPass, 0, );
    password = nil;
}
</code></pre>

<ul>
<li>关于此实现的重要说明:您必须在调用 charPass 之前检查 NULL,否则它可能会崩溃。不能保证 CFStringGetCStringPtr 会返回一个值!</li>
</ul>

<p>当密码为 NSData 时,它假设更加严格并且下面的代码假设可以工作:</p>

<pre><code>memset(, 0, );
</code></pre>

<p>但这给了我一个编译错误:</p>

<blockquote>
<p>No matching function for call to &#39;memset&#39;</p>
</blockquote>

<p>我找不到一种解决方法来指向密码地址并像我对字符串所做的那样删除那里的字节(字节方法应该让我按照我的理解做到这一点,但由于某种原因它无法编译我想不通)</p>

<p>有人对此有想法吗?</p>

<p>10 倍</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你的字符串释放器是<em>脆弱的</em>。你写:</p>

<blockquote>
<p>Big remark on this implementation: You HAVE to check for NULL before calling the charPass or it might crash. There is NO guarantee that CFStringGetCStringPtr will return a value!</p>
</blockquote>

<p>这是记录在案的行为,因为 <code>CFString</code>(因此 <code>NSString</code>)确实<strong>不</strong>保证您可以直接访问其内部缓冲区。你没有说你是如何处理这种情况的,但如果你不删除内存,你可能会遇到安全问题。</p>

<p>如果你确实得到了一个有效的指针,那么你使用了错误的字节数。调用 <code>[密码长度]</code> 返回:</p>

<blockquote>
<p>The number of UTF-16 code units in the receiver.</p>
</blockquote>

<p>这与字节数不同。然而 <code>CFStringGetCStringPtr</code> 返回:</p>

<blockquote>
<p>A pointer to a C string or NULL if the internal storage of theString does not allow this to be returned efficiently.</p>
</blockquote>

<p>如果你有一个 C 字符串,你可以使用 C 库函数 <code>strlen()</code> 来查找它的长度。</p>

<p>要解决 <code>CFStringGetCStringPtr</code> 返回 <code>NULL</code> 的情况,您可以自己创建字符串作为 <code>CFString</code> 并提供自定义 <code>CFAllocater</code>。您不需要自己编写完整的分配器,而是可以基于系统构建一个。您可以获得默认分配器 <code>CFAllocatorContext</code> ,它将返回系统使用的函数指针。然后,您可以基于 <code>CFAllocatorContext</code> 创建一个新的 <code>CFAllocator</code>,它是默认的副本,除非您更改了 <code>deallocate</code> 和 <code>reallocate</code> 指向您根据默认 <code>allocate</code>、<code>reallocate</code> 和 <code>deallocate</code> 实现的函数的指针,但也调用 <code> memset</code> 适本地清除内存。</p>

<p>一旦您完成了安全删除操作,就可以确保这些自定义创建的 <code>CFString</code> 对象(也称为 <code>NSString</code> 对象)在您的应用退出之前被释放。</p>

<p><code>CFAllocator</code>、<code>CFAllocatorContext</code>等可以在<a href="https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.html#//apple_ref/doc/uid/10000127-SW1" rel="noreferrer noopener nofollow">Memory Management Programming Guide for Core Foundation</a>中找到.</p>

<p>这给我们带来了您的实际问题,如何将 <code>NSData</code> 归零。幸运的是,<code>NSData</code> 对象是 <code>CFData</code> 对象,而 <code>CFData</code> 的 <code>CFDataGetBytePtr</code> 与 <code>CFStringGetCStringPtr</code>,保证返回一个指向实际字节的指针,直接来自文档:</p>

<blockquote>
<p>This function is guaranteed to return a pointer to a <code>CFData</code> object&#39;s internal bytes. <code>CFData</code>, unlike <code>CFString</code>, does not hide its internal storage.</p>
</blockquote>

<p>因此,遵循 <code>CFString</code> 模式的代码将在这里工作。请注意,使用 <code>NSData</code> 的 <code>bytes</code> 在文档中<strong>不</strong>保证调用 <code>CFDataGetBytePtr</code>,例如可以调用<code>CFDataGetBytes</code> 并返回字节的<em>副本</em>,使用 <code>CFData</code> 函数。</p>

<p>HTH</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - OBJ-C 在取消之前删除 NSData 内容,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51806557/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51806557/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - OBJ-C 在取消之前删除 NSData 内容