安全C和大学 - 训练缓冲区溢出 [英] Secure C and the universities - trained for buffer overflow

查看:118
本文介绍了安全C和大学 - 训练缓冲区溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近完成了C.大学课程,所以我当然缺乏经验。

I recently finished a university course in C. Therefore I lack experience, of course.

一些大学往往会教导学生安全编程,的或至少一些元素的。有甚至视频(取从这里 )。

Some universities tend to teach their students secure programming, or at least some elements. There's even a video (taken from here).

为C,复制字符串,需要 - 因为据我所知 - 的strcpy或string.h中的功能。如何在每一天的节目安全地使用它?
你有一些功能,处理分配,以prevent缓冲区溢出?还有的<一个href=\"https://www.securecoding.cert.org/confluence/display/sec$c$c/CERT+C+Secure+Coding+Standard;jsessionid=9542D1D823E37EE711407D3FEFF18548\"相对=nofollow>对于C CERT安全编码标准。它提供的例子和兼容解决方案:

Being in C, copying strings, requires - as far as I know - strcpy or string.h functions. How do you use it securely in every-day programming? Do you have some functions, which handle allocation to prevent buffer overflows? There's the CERT secure coding standard for C. It's offering examples and compliant solutions:

int main(int argc, char *argv[]) {
  /* ... */
  char prog_name[128];
  strcpy(prog_name, argv[0]);
  /* ... */
}

和他们的选择是:

int main(int argc, char *argv[]) {
  /* ... */
  char *prog_name = (char *)malloc(strlen(argv[0])+1);
  if (prog_name != NULL) {
    strcpy(prog_name, argv[0]);
  }
  else {
    /* Couldn't get the memory - recover */
  }
  /* ... */
}

从这里开始,<一个拍摄href=\"https://www.securecoding.cert.org/confluence/display/sec$c$c/STR31-C.+Guarantee+that+storage+for+strings+has+sufficient+space+for+character+data+and+the+null+terminator\"相对=nofollow>第二个示例。

不过,据我得到它,这只是更具挑战性,更code,更多的工作。为什么没有人改变库本身?或者至少,为什么没有人提供一个安全的替代库或函数,处理这个正确的方式?

But as far as I get it that's just more challenging, more code, more work. Why does no one change the library itself? Or at least why does no one provide a secure alternative library or functions, which handle this in the right way?

感谢您的阅读,
wishi

Thanks for reading, wishi

推荐答案

POSIX的功能,这个(几乎所有的系统上可用)为的strdup()的strcpy()使用,如果你不想分配新的内存,而且已经有您想要使用的缓冲区,但你更好地了解多大的缓冲区,如果串适合于它。如果你不知道该字符串适合,有函数strncpy(),仅仅拷贝给定数量的字符。所以,你可以复制的量限制为您的缓冲区大小。

The Posix function for this (available on nearly every system) is strdup(). strcpy() is used if you don't want to allocate new memory and already have a buffer you want to use, but then you better known how big that buffer is and if the string fits in it. If you don't know if the string fits, there is strncpy() that just copies a given number of characters. So you can limit the copied amount to your buffers size.

除此之外的是,有很多刺库以不同的方式处理字符串的大小。

And besides of that, there are lots of sting libraries that manage string sizes in different ways.

既然你标记它的C ++:有的std ::字符串做所有的内存管理,为您和不给你这些问题。

And since you tagged it C++: There is std::string that does all the memory management for you and doesn't give you these problems.

这篇关于安全C和大学 - 训练缓冲区溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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