Ptrace_scope的作用及设置

echo "0"|sudo tee /proc/sys/kernel/yama/ptrace_scope

Short answer: no practical danger yet, but read on for a better way...


What's this ptrace thing anyway?

this is due to a bug in the Ubuntu kernel that prevents ptrace and WINE playing well together.

  • No, ptrace protection is a deliberate kernel security measure first introduced around Ubuntu 10.10. It's not a bug, and so isn't going to be "fixed".

  • In simple terms, the default ptrace_scope value of 1 blocks one process from examining and modifying another process unless the second process (child) was started by the first process (parent).

  • This can cause problems with some programs under Wine because of the way wineserver provides "Windows Services" to these programs.

What are the risks in setting ptrace_scope to 0?

  • This restores the old behavior where one process can "trace" another process, even if there is no parent-child relationship.

  • In theory, a piece of malware can use this to harm you/your computer; e.g. it can attach to Firefox and log all of your URLs/passwords, etc. In practice this is extremely unlikely unless you blindly install binary debs from random sites, etc.

  • As far as debugging goes, the 0 settings is in fact required for gdb, strace, etc. to attach to non-children unless you run them with elevated privileges (sudo).

What are the problems with the workaround?

  • The workaround is somewhat problematic because ptrace_scope is a global value, and while it's set to 0, all processes on your system are exempt from the non-child restriction.
  • If you use the workaround, put it in a simple bash script that enables it, runs your Windows program and then disables (sets to 1) on exit.
    • DO NOT make ptrace_scope world-writable (666) as the forum post recommends -- that is a huge security risk because now any process can change it at will!

Is there a better solution?

  • A better solution which is more secure and does not require repetitively modifying ptrace_scope is to grant Wineserver ptrace capabilities.

    • In a terminal:

      sudo apt-get install libcap2-bin 
      sudo setcap cap_sys_ptrace=eip /usr/bin/wineserver
      sudo setcap cap_sys_ptrace=eip /usr/bin/wine-preloader
      
    • This exempts the wineserver and wine-preloader binaries from the non-child ptrace restriction, and allows them to ptrace any process.

    • It only needs to be done once, and is safer because these binaries are usually from a trusted source - the official repositories or the official Wine PPA, so they aren't going to be malware.

If you're using Crossover

Install libcap2:

sudo apt-get install libcap2-bin;

Then, add an exception for Crossover:

sudo setcap cap_sys_ptrace=eip /opt/cxoffice/bin/wineserver;
sudo setcap cap_sys_ptrace=eip /opt/cxoffice/bin/wine-preloader;

Finally, add its libraries to ld.so.conf (or you will get "error while loading shared libraries: libwine.so.1: cannot open shared object file: No such file or directory"):

echo /opt/cxoffice/lib/ | sudo tee /etc/ld.so.conf.d/crossover.conf
sudo /sbin/ldconfig

转载于:https://www.cnblogs.com/cane/p/3909420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`PTRACE_PEEKUSER` 是一个 `ptrace` 系统调用,用于读取目标进程中的用户空间寄存器值。它的调用方式如下: ```c long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data); ``` 其中,`request` 参数需要设置为 `PTRACE_PEEKUSER`,`pid` 参数为目标进程的进程 ID,`addr` 参数表示要读取的寄存器相对于用户空间的偏移地址,`data` 参数是输出参数,表示读取到的寄存器值。 在 x86_64 架构下,可以使用 `PTRACE_PEEKUSER` 读取的寄存器包括: - `R15`、`R14`、`R13`、`R12`、`RBP`、`RBX`、`R11`、`R10`、`R9`、`R8`、`RAX`、`RCX`、`RDX`、`RSI`、`RDI`、`ORIG_RAX`、`RIP`、`CS`、`EFLAGS`、`RSP`、`SS`、`FS_BASE`、`GS_BASE`、`DS`、`ES`、`FS`、`GS`。 下面是一个使用 `PTRACE_PEEKUSER` 读取 `RIP` 寄存器值的示例代码: ```c #include <sys/ptrace.h> #include <sys/user.h> int main() { pid_t pid = 1234; // 目标进程的PID struct user_regs_struct regs; // 使用ptrace附加到目标进程 if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) { perror("ptrace attach"); return -1; } // 等待目标进程停止 wait(NULL); // 获取目标进程的RIP寄存器值 long rip = ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL); if (rip < 0) { perror("ptrace peekuser"); return -1; } // 打印RIP寄存器的值 printf("RIP = %lx\n", rip); // 使用ptrace从目标进程中分离 if (ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) { perror("ptrace detach"); return -1; } return 0; } ``` 需要注意的是,`addr` 参数表示的是相对于用户空间的偏移地址,而不是绝对地址,因此需要将寄存器的名称转换为偏移量,如 `RIP` 对应的偏移量是 `8 * RIP`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值