Bug 35291

Summary: File Upload vulnerability in upload-cover-image.pl
Product: Koha Reporter: Ryan Henderson <ryan.henderson>
Component: ToolsAssignee: Bugs List <koha-bugs>
Status: CLOSED FIXED QA Contact: Aleisha Amohia <aleisha>
Severity: critical    
Priority: P5 - low CC: 1joynelson, aleisha, amitddng135, dcook, didier.gautheron, fridolin.somers, jonathan.druart, laurent.ducos, lucas, m.de.rooy, martin.renvoize, nick, tomascohen
Version: unspecified   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
23.11.00,21.11.26
Circulation function:
Attachments: Bug 35291: Prevent leaks from FS
Bug 35291: Prevent leaks from FS
Bug 35291: Don't allow symlinks for link files in cover image ZIP
Bug 35291: Validate filepaths in linking files
Bug 35291: Prevent leaks from FS
Bug 35291: Don't allow symlinks for link files in cover image ZIP
Bug 35291: Validate filepaths in linking files
Bug 35291: Prevent leaks from FS
Bug 35291: Don't allow symlinks for link files in cover image ZIP
Bug 35291: Validate filepaths in linking files
Bug 35291: (QA follow-up) Tidying script for QA test tools
Bug 35291: Prevent leaks from FS
Bug 35291: Don't allow symlinks for link files in cover image ZIP
Bug 35291: Validate filepaths in linking files
Bug 35291: (QA follow-up) Tidying script for QA test tools

Description Ryan Henderson 2023-11-08 15:10:13 UTC
Hello community, 

We've recently had a penetration test run on one of our servers and it returned a vulnerability within the upload-cover-image.pl file. 

This was originally found within 23.05.04.  You can see the detail regarding the CVE below:

https://nvd.nist.gov/vuln/detail/CVE-2023-44962

The following GitHub explains the PoC for this as well:

https://github.com/ggb0n/CVE-2023-44962
Comment 1 Jonathan Druart 2023-11-08 15:40:59 UTC Comment hidden (obsolete)
Comment 2 Jonathan Druart 2023-11-08 16:15:06 UTC
Created attachment 158664 [details] [review]
Bug 35291: Prevent leaks from FS
Comment 3 Jonathan Druart 2023-11-08 16:15:27 UTC
This should fix the problem, please test.
Comment 4 David Cook 2023-11-09 00:28:32 UTC
Created attachment 158684 [details] [review]
Bug 35291: Prevent leaks from FS

Signed-off-by: David Cook <dcook@prosentient.com.au>
Comment 5 David Cook 2023-11-09 00:28:35 UTC
Created attachment 158685 [details] [review]
Bug 35291: Don't allow symlinks for link files in cover image ZIP

There's no reason to allow symlinks for link files in cover image
ZIP files. Preventing their use prevents someone from uploading
a symlink pointing to an existing file on the Koha server.

Test plan:
0. Apply patch and restart/reload Koha
1. Create a PNG cover image
2. Create a datalink.txt file that contains something like the
following:
29,Untitled.PNG
3. Turn on "LocalCoverImages" system preference
4. Upload via http://localhost:8081/cgi-bin/koha/tools/upload-cover-image.pl
5. Go to http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=29
6. Note the cover image has been uploaded
Comment 6 David Cook 2023-11-09 00:35:39 UTC
(In reply to Jonathan Druart from comment #3)
> This should fix the problem, please test.

This seems to prevent the data leak, so I've signed off.

I've also added a patch to disallow symlinks, so that the Perl script can't be pointed at a file on the server via a symlink. (Note that it just silently fails at the moment. I'm not sure if that is preferable to a noisy fail.)

--

Of course, looking at the code, we're not validating the filename from the file. It shouldn't really cause a problem per se, but it's not good. I might add something on top of that just while we're looking at it...
Comment 7 David Cook 2023-11-09 00:50:49 UTC
Created attachment 158686 [details] [review]
Bug 35291: Validate filepaths in linking files

Validate that the files in linking files are contained within the ZIP.
Comment 8 David Cook 2023-11-09 00:57:41 UTC
Actually, I'm not confident that I've sewn up the symlink/relative filepath issue, as I'm probably handling it too far down in the code, and not testing every possible permutation...

We're allowing a strange nested structure in the ZIP file...
Comment 9 Nick Clemens (kidclamp) 2023-11-21 11:41:23 UTC
Created attachment 159151 [details] [review]
Bug 35291: Prevent leaks from FS

Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 10 Nick Clemens (kidclamp) 2023-11-21 11:41:25 UTC
Created attachment 159152 [details] [review]
Bug 35291: Don't allow symlinks for link files in cover image ZIP

There's no reason to allow symlinks for link files in cover image
ZIP files. Preventing their use prevents someone from uploading
a symlink pointing to an existing file on the Koha server.

Test plan:
0. Apply patch and restart/reload Koha
1. Create a PNG cover image
2. Create a datalink.txt file that contains something like the
following:
29,Untitled.PNG
3. Turn on "LocalCoverImages" system preference
4. Upload via http://localhost:8081/cgi-bin/koha/tools/upload-cover-image.pl
5. Go to http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=29
6. Note the cover image has been uploaded

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 11 Nick Clemens (kidclamp) 2023-11-21 11:41:28 UTC
Created attachment 159153 [details] [review]
Bug 35291: Validate filepaths in linking files

Validate that the files in linking files are contained within the ZIP.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Comment 12 Tomás Cohen Arazi (tcohen) 2023-11-23 18:02:51 UTC
This is weird. 3 QA team members involved, signing. Yet we don't allow ourselves to do the step of marking it PQA.

I'll try to reproduce the exploit and check how this solves it.

Other than that, the code looks correct, makes sense and the QA script is (of course because of those involved) happy.
Comment 13 Aleisha Amohia 2023-11-23 21:09:15 UTC
Created attachment 159215 [details] [review]
Bug 35291: Prevent leaks from FS

Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Comment 14 Aleisha Amohia 2023-11-23 21:09:18 UTC
Created attachment 159216 [details] [review]
Bug 35291: Don't allow symlinks for link files in cover image ZIP

There's no reason to allow symlinks for link files in cover image
ZIP files. Preventing their use prevents someone from uploading
a symlink pointing to an existing file on the Koha server.

Test plan:
0. Apply patch and restart/reload Koha
1. Create a PNG cover image
2. Create a datalink.txt file that contains something like the
following:
29,Untitled.PNG
3. Turn on "LocalCoverImages" system preference
4. Upload via http://localhost:8081/cgi-bin/koha/tools/upload-cover-image.pl
5. Go to http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=29
6. Note the cover image has been uploaded

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Comment 15 Aleisha Amohia 2023-11-23 21:09:21 UTC
Created attachment 159217 [details] [review]
Bug 35291: Validate filepaths in linking files

Validate that the files in linking files are contained within the ZIP.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Comment 16 Aleisha Amohia 2023-11-23 21:09:25 UTC
Created attachment 159218 [details] [review]
Bug 35291: (QA follow-up) Tidying script for QA test tools

Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Comment 17 Tomás Cohen Arazi (tcohen) 2023-11-23 23:48:19 UTC
(In reply to Aleisha Amohia from comment #16)
> Created attachment 159218 [details] [review] [review]
> Bug 35291: (QA follow-up) Tidying script for QA test tools
> 
> Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>

Thanks Aleisha! I forgot to upload!

This is interesting:

kohadev-koha@kohadevbox:koha(master)$ qa -c 3
testing 3 commit(s) (applied to b6f350e '759 Bug 35374: Remove ES6 template li')

Processing files before patches
|========================>| 1 / 1 (100.00%)
Processing files after patches
|========================>| 1 / 1 (100.00%)

 OK	tools/upload-cover-image.pl


Processing additional checks OK!
Comment 18 Tomás Cohen Arazi (tcohen) 2023-11-23 23:49:07 UTC
Created attachment 159220 [details] [review]
Bug 35291: Prevent leaks from FS

Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 19 Tomás Cohen Arazi (tcohen) 2023-11-23 23:49:11 UTC
Created attachment 159221 [details] [review]
Bug 35291: Don't allow symlinks for link files in cover image ZIP

There's no reason to allow symlinks for link files in cover image
ZIP files. Preventing their use prevents someone from uploading
a symlink pointing to an existing file on the Koha server.

Test plan:
0. Apply patch and restart/reload Koha
1. Create a PNG cover image
2. Create a datalink.txt file that contains something like the
following:
29,Untitled.PNG
3. Turn on "LocalCoverImages" system preference
4. Upload via http://localhost:8081/cgi-bin/koha/tools/upload-cover-image.pl
5. Go to http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=29
6. Note the cover image has been uploaded

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 20 Tomás Cohen Arazi (tcohen) 2023-11-23 23:49:14 UTC
Created attachment 159222 [details] [review]
Bug 35291: Validate filepaths in linking files

Validate that the files in linking files are contained within the ZIP.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 21 Tomás Cohen Arazi (tcohen) 2023-11-23 23:49:18 UTC
Created attachment 159223 [details] [review]
Bug 35291: (QA follow-up) Tidying script for QA test tools

Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 22 Tomás Cohen Arazi (tcohen) 2023-11-24 19:46:47 UTC
Pushed to master for 23.11.

Nice work everyone, thanks!
Comment 23 Jonathan Druart 2023-12-01 08:16:36 UTC
Pushed to 21.11.x for 21.11.26