【问题标题】:GDB problems inside dockerdocker内部的GDB问题
【发布时间】:2014-08-26 11:44:06
【问题描述】:

在 Ubuntu 13.04 上使用 docker 版本 Docker version 1.1.0, build 79812e3,并使用以下创建的 docker 容器:

# docker build -t gdb_problem_testing - < THIS_FILE
FROM ubuntu
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y build-essential gdb

这样做:

user@host $ sudo docker run --rm -it --user=root gdb_problem_testing su root -c bash
root@690396061e81:/# cat <<EOF > test.c && gcc -ggdb test.c -o test && gdb -ex run test
> #include <stdio.h>
>
> int main(int argc, char **argv) {
>     printf("Hello\n!");
> }
> EOF
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /test...done.
Starting program: /test
user@host $

不运行程序。 gdb 刚刚启动并退出。请注意最后一行,我什至从 docker 容器中启动并且没有返回到 bash 提示符 (!)

我无法在非 docker 环境中重现此问题(su &lt;some_user&gt; -c bash 等)。

如果我不使用su &lt;some_user&gt; -c bash 而只是使用bash,则不会出现此问题。由于各种原因,必须使用su,主要是因为这是我发现能够为 docker 容器中的特定用户强制执行 ulimit 的唯一方法。

为什么 gdb 在这种情况下不起作用??

编辑

copy-pastable 命令在 docker 容器中运行:

cat <<EOF > test.c && gcc -ggdb test.c -o test && gdb -ex run test
#include <stdio.h>

int main(int argc, char **argv) {
    printf("Hello\n!");
}
EOF

更新

只是为了表明是 docker 容器中的 su 命令搞砸了,下面是使用 bash 而不是 su root -c bash 执行相同操作的输出:

user@host $ sudo docker run --rm -it --user=root gdb_problem_testing bash
root@ce1581184f7a:/# cat <<EOF > test.c && gcc -ggdb test.c -o test && gdb -ex run test
> #include <stdio.h>
>
> int main(int argc, char **argv) {
>     printf("Hello\n!");
> }
> EOF
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /test...done.
Starting program: /test
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Hello
![Inferior 1 (process 17) exited with code 07]
(gdb)

注意程序的实际运行方式(打印“Hello”),我留在 gdb 和 docker 容器中。

【问题讨论】:

  • 出于拼命尝试随机的事情,我碰巧找到了解决这个问题的方法。做sudo -u root su root -c &lt;cmd&gt;。我不知道为什么会有所不同,但确实如此。 gdb 将运行,所有 ulimits 仍然适用

标签: c gdb docker su


【解决方案1】:

这是由于apparmor。我有一个解决方案,但需要在每次启动后应用。

诀窍是告诉 apparmor “投诉”安全违规行为,而不是阻止它们。这不是最安全的解决方法,我真的很想找到一种更好的方法来处理它(比如只允许 ptrace 和 GDB 需要的任何其他内容)。

要告诉 apparmor 投诉,您需要将 /etc/apparmor.d/docker 中的行从:

profile docker-default flags=(attach_disconnected,mediate_deleted) {

到:

profile docker-default flags=(attach_disconnected,mediate_deleted,complain) {

【讨论】:

  • Steven Van Acker [这里][1] 提出了一个解决方案,该解决方案可以在启动后继续存在。简而言之,如果您使用的是 Ubuntu,请在主机上执行以下操作: 1. 使用:sudo apt-get install apparmor-utils 安装 apparmor-utils 2. 为 docker apparmor 配置启用抱怨模式。 sudo aa-complain /etc/apparmor.d/docker 3. 使上述命令在重新启动后保持不变:sudo echo "aa-complain /etc/apparmor.d/docker" &gt;&gt; /etc/rc.local [1]:github.com/docker/docker/issues/7276#issuecomment-50436671
猜你喜欢
  • 2017-10-10
  • 2014-07-13
  • 2021-04-02
  • 2020-06-22
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多