-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use libarchive instead of calling out to tar
Use libarchive in order to make use of its security features and avoid calling out to tar via a shell. The TAR_COMMAND is still used in staging.c to complete the copy when a hardlink fails. Signed-off-by: Matthew Johnson <matthew.johnson@intel.com>
- Loading branch information
1 parent
ba9e0f2
commit 338034d
Showing
8 changed files
with
153 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Software Updater - client side | ||
* | ||
* Copyright © 2012-2016 Intel Corporation. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 2 or later of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Authors: | ||
* Matthew Johnson <mathew.johnson@intel.com> | ||
* | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <archive.h> | ||
#include <archive_entry.h> | ||
|
||
#include "swupd.h" | ||
|
||
static int copy_data(struct archive *ar, struct archive *aw) | ||
{ | ||
int r; | ||
const void *buffer; | ||
size_t size; | ||
la_int64_t offset; | ||
|
||
for(;;) { | ||
r = archive_read_data_block(ar, &buffer, &size, &offset); | ||
if (r == ARCHIVE_EOF) { | ||
return ARCHIVE_OK; | ||
} else if (r < ARCHIVE_OK) { | ||
return r; | ||
} | ||
|
||
r = archive_write_data_block(aw, buffer, size, offset); | ||
if (r < ARCHIVE_OK) { | ||
fprintf(stderr, "Error: %s\n", archive_error_string(aw)); | ||
return r; | ||
} | ||
} | ||
} | ||
|
||
int extract(const char *filename, const char *target) | ||
{ | ||
struct archive *a, *ext; | ||
struct archive_entry *entry; | ||
int flags, r; | ||
|
||
/* set which attributes we want to restore */ | ||
flags = ARCHIVE_EXTRACT_TIME; | ||
flags |= ARCHIVE_EXTRACT_PERM; | ||
flags |= ARCHIVE_EXTRACT_ACL; | ||
flags |= ARCHIVE_EXTRACT_FFLAGS; | ||
flags |= ARCHIVE_EXTRACT_XATTR; | ||
|
||
/* set security flags */ | ||
flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS; | ||
flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT; | ||
//flags |= ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS; | ||
|
||
a = archive_read_new(); | ||
archive_read_support_format_all(a); | ||
archive_read_support_filter_all(a); | ||
|
||
ext = archive_write_disk_new(); | ||
archive_write_disk_set_options(ext, flags); | ||
archive_write_disk_set_standard_lookup(ext); | ||
r = archive_read_open_filename(a, filename, 10240); | ||
if (r) { | ||
return r; | ||
} | ||
|
||
for (;;) { | ||
r = archive_read_next_header(a, &entry); | ||
if (r == ARCHIVE_EOF) { | ||
break; | ||
} | ||
|
||
if (r < ARCHIVE_OK) { | ||
fprintf(stderr, "%s\n", archive_error_string(ext)); | ||
} | ||
|
||
if (r < ARCHIVE_WARN) { | ||
return r; | ||
} | ||
|
||
// Default to state directory if target is NULL | ||
char *fullpath; | ||
if (target != NULL) { | ||
string_or_die(&fullpath, "%s/%s", target, archive_entry_pathname(entry)); | ||
} else { | ||
string_or_die(&fullpath, "%s/%s", state_dir, archive_entry_pathname(entry)); | ||
} | ||
|
||
archive_entry_set_pathname(entry, fullpath); | ||
free(fullpath); | ||
|
||
r = archive_write_header(ext, entry); | ||
if (r < ARCHIVE_OK) { | ||
fprintf(stderr, "%s\n", archive_error_string(ext)); | ||
} else if (archive_entry_size(entry) > 0) { | ||
r = copy_data(a, ext); | ||
if (r < ARCHIVE_OK) { | ||
fprintf(stderr, "%s\n", archive_error_string(ext)); | ||
return r; | ||
} | ||
|
||
if (r < ARCHIVE_WARN) { | ||
return r; | ||
} | ||
} | ||
|
||
r = archive_write_finish_entry(ext); | ||
if (r < ARCHIVE_OK) { | ||
fprintf(stderr, "%s\n", archive_error_string(ext)); | ||
return r; | ||
} | ||
|
||
if (r < ARCHIVE_WARN) { | ||
return r; | ||
} | ||
} | ||
|
||
archive_read_close(a); | ||
archive_read_free(a); | ||
archive_write_close(ext); | ||
archive_write_free(ext); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters