linux C/C++编程,如何在沙盒中运行用户自己的动态库

39 views
Skip to first unread message

adream

unread,
Aug 3, 2016, 9:53:23 AM8/3/16
to shlug
举例说明一下:
在程序中,我和用户协商了这样的调用接口:
int user_foo(int x)
需要用户提供的动态库(.so)文件必须包含如上定义的uer_foo函数。
程序运行时,需要把用户的.so文件传入,当前我使用dlopen打开用户的.so文件,然后使用dlsym搜索 user_foo函数。
但是这种方法有个问题,如果用户的 user_foo函数崩溃了,会导致我的整个程序崩溃的。
所以,我们想问一下有没有更为安全的调用方法,在程序中营造一个沙盒环境,在沙盒中调用用户动态库?
谢谢

蒲肖肖

unread,
Aug 3, 2016, 11:05:45 AM8/3/16
to Shanghai Linux User Group
一个想法,fork 一个子进程去执行用户函数

Shell Xu

unread,
Aug 3, 2016, 12:15:16 PM8/3/16
to shlug
我不知道你说的安全是什么意思。
例如,用户的函数上来想修改你的函数(在内存中的代码),而且UAC准了。你会允许他修改么?
如果允许的话,对客户函数的一般沙盒都没什么作用了。因为即使你防御了客户代码的崩溃,也无法防御自己的崩溃。
而不允许的话,可能很多客户行为都要精确定义了,哪些可以,哪些不行。你需要的不是沙盒,而是对客户异常行为的识别。这个有点像杀毒了,行为比沙盒更复杂。
当然,一个办法是fork一个进程去执行。但是这对于环境副作用是不友好的。例如某些库(线程非安全的)使用了static存储了某些数据。每次执行的时候fork,先不论开销,其结果一定会有问题。
另一个办法是,你不用管被调用函数的行为是不是异常。你只要处理他的各种崩溃。类似于Windows下面的SEH机制。
我对Linux内核不是很熟,Linux下有类似于SEH的机制么?

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/

Yang Fan

unread,
Aug 3, 2016, 10:19:41 PM8/3/16
to sh...@googlegroups.com
Linux貌似没有SEH,只是发个SEGV、ABRT之类signal的就完事了。
Regards,
Fan Yang

adream

unread,
Aug 4, 2016, 8:57:31 AM8/4/16
to shlug
谢谢回复,我说的安全是用户函数崩溃不会连累我程序崩溃。
另外用户函数是完全独立的行为,不会调用或我程序的任何东西,我们只有按照约定格式的数据交互,如例子中说的user_foo函数,我向用户函数传递一个int,用户函数向我返回一个int。

在 2016年8月4日 上午10:19,Yang Fan <miss...@gmail.com>写道:
Linux貌似没有SEH,只是发个SEGV、ABRT之类signal的就完事了。

2016-08-04 0:14 GMT+08:00 Shell Xu <shell...@gmail.com>:
我不知道你说的安全是什么意思。
例如,用户的函数上来想修改你的函数(在内存中的代码),而且UAC准了。你会允许他修改么?
如果允许的话,对客户函数的一般沙盒都没什么作用了。因为即使你防御了客户代码的崩溃,也无法防御自己的崩溃。
而不允许的话,可能很多客户行为都要精确定义了,哪些可以,哪些不行。你需要的不是沙盒,而是对客户异常行为的识别。这个有点像杀毒了,行为比沙盒更复杂。
当然,一个办法是fork一个进程去执行。但是这对于环境副作用是不友好的。例如某些库(线程非安全的)使用了static存储了某些数据。每次执行的时候fork,先不论开销,其结果一定会有问题。
另一个办法是,你不用管被调用函数的行为是不是异常。你只要处理他的各种崩溃。类似于Windows下面的SEH机制。
我对Linux内核不是很熟,Linux下有类似于SEH的机制么?
2016-08-03 21:53 GMT+08:00 adream <adre...@gmail.com>:
举例说明一下:
在程序中,我和用户协商了这样的调用接口:
int user_foo(int x)
需要用户提供的动态库(.so)文件必须包含如上定义的uer_foo函数。
程序运行时,需要把用户的.so文件传入,当前我使用dlopen打开用户的.so文件,然后使用dlsym搜索 user_foo函数。
但是这种方法有个问题,如果用户的 user_foo函数崩溃了,会导致我的整个程序崩溃的。
所以,我们想问一下有没有更为安全的调用方法,在程序中营造一个沙盒环境,在沙盒中调用用户动态库?
谢谢

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+unsubscribe@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN

---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+unsubscribe@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN

---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
Regards,
Fan Yang

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+unsubscribe@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN

---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

adream

unread,
Aug 4, 2016, 8:59:17 AM8/4/16
to shlug
前面有人提到使用fork,这确实是个方法,但我想知道有没有更简单一点的。
想Firefox中的插件开发,插件运行崩溃,应该不会导致 Firefox崩溃。
谢谢。

Heiher

unread,
Aug 4, 2016, 9:01:26 AM8/4/16
to shlug
如果 call signature 是简单的,如不涉及指针传递的,封装个简单的 RPC 层,将客户共享库放到独立的进程中执行,通过 RPC
交互。说不定网上找找已经有现成的开源的 RPC 框架了。
>>>> shlug+un...@googlegroups.com. For more options, visit this group at
>>>> https://groups.google.com/d/forum/shlug?hl=zh-CN
>>>> ---
>>>> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
>>>> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
>>>> 要查看更多选项,请访问https://groups.google.com/d/optout
>>>
>>>
>>>
>>>
>>> --
>>> 彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
>>> blog: http://shell909090.org/
>>> twitter: @shell909090
>>> about.me: http://about.me/shell909090
>>>
>>> --
>>> -- You received this message because you are subscribed to the Google
>>> Groups Shanghai Linux User Group group. To post to this group, send email to
>>> sh...@googlegroups.com. To unsubscribe from this group, send email to
>>> shlug+un...@googlegroups.com. For more options, visit this group at
>>> https://groups.google.com/d/forum/shlug?hl=zh-CN
>>> ---
>>> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
>>> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
>>> 要查看更多选项,请访问https://groups.google.com/d/optout
>>
>>
>>
>>
>> --
>> Regards,
>> Fan Yang
>>
>> --
>> -- You received this message because you are subscribed to the Google
>> Groups Shanghai Linux User Group group. To post to this group, send email to
>> sh...@googlegroups.com. To unsubscribe from this group, send email to
>> shlug+un...@googlegroups.com. For more options, visit this group at
>> https://groups.google.com/d/forum/shlug?hl=zh-CN
>> ---
>> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
>> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
>> 要查看更多选项,请访问https://groups.google.com/d/optout
>
>
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout



--
Best regards!
Heiher
http://hev.cc

Shell Xu

unread,
Aug 4, 2016, 9:57:37 AM8/4/16
to shlug
ff的插件是js的,崩溃发生在js的rt层吧。。。

Shell Xu

unread,
Aug 4, 2016, 9:58:47 AM8/4/16
to shlug
我的问题是,在用户的函数中,哪些行为是合法的?还是全部行为都是合法的?如果全部行为都是合法的,那么越界访问数据也是。

2016-08-04 20:57 GMT+08:00 adream <adre...@gmail.com>:

依云

unread,
Aug 4, 2016, 10:17:43 AM8/4/16
to sh...@googlegroups.com
On Thu, Aug 04, 2016 at 08:59:13PM +0800, adream wrote:
> 前面有人提到使用fork,这确实是个方法,但我想知道有没有更简单一点的。
> 想Firefox中的插件开发,插件运行崩溃,应该不会导致 Firefox崩溃。
> 谢谢。

首先假设你指的是 NPAPI 插件。

如果跑在同一进程内的话,是会连累火狐一起崩溃的。所以推荐是插件跑在单独的
进程里,使用 RPC 和主进程通讯。

我以前的火狐就经常崩溃,后来才发现原来我没有让插件在单独的进程里跑。更改
了设置之后就变成 coredumpctl 里一堆 plugin-container 的 core 了……

--
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://blog.lilydjwg.me/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

Henry Chen

unread,
Aug 5, 2016, 4:31:52 AM8/5/16
to Shanghai Linux User Group
有一种方法是用machinectl,是创造一个容器,不知道是否适合这里的方案。

在 2016年8月3日星期三 UTC+8下午9:53:23,Adream写道:

依云

unread,
Aug 6, 2016, 2:15:18 AM8/6/16
to sh...@googlegroups.com
On Fri, Aug 05, 2016 at 01:31:51AM -0700, Henry Chen wrote:
> 有一种方法是用machinectl,是创造一个容器,不知道是否适合这里的方案。

这个更重了呢。
Reply all
Reply to author
Forward
0 new messages