diff mbox series

[FFmpeg-devel] mfenc: Use dlopen instead of LoadLibrary for loading mfplat.dll

Message ID 20220525213519.3730605-1-martin@martin.st
State New
Headers show
Series [FFmpeg-devel] mfenc: Use dlopen instead of LoadLibrary for loading mfplat.dll | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_armv7_RPi4 success Make finished
andriy/make_fate_armv7_RPi4 success Make fate finished

Commit Message

Martin Storsjö May 25, 2022, 9:35 p.m. UTC
The dlopen wrapper contains code to make loading libraries safer,
to avoid loading a potentially malicious DLL with the same name.

Signed-off-by: Martin Storsjö <martin@martin.st>
---
 libavcodec/mfenc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Timo Rothenpieler May 25, 2022, 9:42 p.m. UTC | #1
On 25/05/2022 23:35, Martin Storsjö wrote:
> The dlopen wrapper contains code to make loading libraries safer,
> to avoid loading a potentially malicious DLL with the same name.
> 
> Signed-off-by: Martin Storsjö <martin@martin.st>
> ---
>   libavcodec/mfenc.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c
> index 17d0ec60bd..ab0b6b4145 100644
> --- a/libavcodec/mfenc.c
> +++ b/libavcodec/mfenc.c
> @@ -29,6 +29,7 @@
>   #include "libavutil/time.h"
>   #include "codec_internal.h"
>   #include "internal.h"
> +#include "compat/w32dlfcn.h"
>   
>   typedef struct MFContext {
>       AVClass *av_class;
> @@ -1158,7 +1159,7 @@ static int mf_load_library(AVCodecContext *avctx)
>       MFContext *c = avctx->priv_data;
>   
>   #if !HAVE_UWP
> -    c->library = LoadLibraryA("mfplat.dll");
> +    c->library = dlopen("mfplat.dll", 0);
>   
>       if (!c->library) {
>           av_log(c, AV_LOG_ERROR, "DLL mfplat.dll failed to open\n");
> @@ -1191,7 +1192,7 @@ static int mf_close(AVCodecContext *avctx)
>       if (c->library)
>           ff_free_mf(&c->functions, &c->mft);
>   
> -    FreeLibrary(c->library);
> +    dlclose(c->library);
>       c->library = NULL;
>   #else
>       ff_free_mf(&c->functions, &c->mft);

Very much a nit, given this code only ever runs on Windows, and dlsym is 
just a literal define to GetProcAddress, but mixing 
dlopen()+GetProcddress() like that looks kinda weird :D
Martin Storsjö May 25, 2022, 10:01 p.m. UTC | #2
On Wed, 25 May 2022, Timo Rothenpieler wrote:

> On 25/05/2022 23:35, Martin Storsjö wrote:
>> The dlopen wrapper contains code to make loading libraries safer,
>> to avoid loading a potentially malicious DLL with the same name.
>> 
>> Signed-off-by: Martin Storsjö <martin@martin.st>
>> ---
>>   libavcodec/mfenc.c | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>> 
>> diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c
>> index 17d0ec60bd..ab0b6b4145 100644
>> --- a/libavcodec/mfenc.c
>> +++ b/libavcodec/mfenc.c
>> @@ -29,6 +29,7 @@
>>   #include "libavutil/time.h"
>>   #include "codec_internal.h"
>>   #include "internal.h"
>> +#include "compat/w32dlfcn.h"
>>     typedef struct MFContext {
>>       AVClass *av_class;
>> @@ -1158,7 +1159,7 @@ static int mf_load_library(AVCodecContext *avctx)
>>       MFContext *c = avctx->priv_data;
>>     #if !HAVE_UWP
>> -    c->library = LoadLibraryA("mfplat.dll");
>> +    c->library = dlopen("mfplat.dll", 0);
>>         if (!c->library) {
>>           av_log(c, AV_LOG_ERROR, "DLL mfplat.dll failed to open\n");
>> @@ -1191,7 +1192,7 @@ static int mf_close(AVCodecContext *avctx)
>>       if (c->library)
>>           ff_free_mf(&c->functions, &c->mft);
>>   -    FreeLibrary(c->library);
>> +    dlclose(c->library);
>>       c->library = NULL;
>>   #else
>>       ff_free_mf(&c->functions, &c->mft);
>
> Very much a nit, given this code only ever runs on Windows, and dlsym is 
> just a literal define to GetProcAddress, but mixing 
> dlopen()+GetProcddress() like that looks kinda weird :D

Ah, yes, it's indeed best to keep it consistent.

// Martin
diff mbox series

Patch

diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c
index 17d0ec60bd..ab0b6b4145 100644
--- a/libavcodec/mfenc.c
+++ b/libavcodec/mfenc.c
@@ -29,6 +29,7 @@ 
 #include "libavutil/time.h"
 #include "codec_internal.h"
 #include "internal.h"
+#include "compat/w32dlfcn.h"
 
 typedef struct MFContext {
     AVClass *av_class;
@@ -1158,7 +1159,7 @@  static int mf_load_library(AVCodecContext *avctx)
     MFContext *c = avctx->priv_data;
 
 #if !HAVE_UWP
-    c->library = LoadLibraryA("mfplat.dll");
+    c->library = dlopen("mfplat.dll", 0);
 
     if (!c->library) {
         av_log(c, AV_LOG_ERROR, "DLL mfplat.dll failed to open\n");
@@ -1191,7 +1192,7 @@  static int mf_close(AVCodecContext *avctx)
     if (c->library)
         ff_free_mf(&c->functions, &c->mft);
 
-    FreeLibrary(c->library);
+    dlclose(c->library);
     c->library = NULL;
 #else
     ff_free_mf(&c->functions, &c->mft);