Skip to content

Commit

Permalink
auth: Switch to pdns::UniqueFilePtr
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Mar 18, 2024
1 parent 01807c6 commit 46c4985
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion modules/pipebackend/coprocess.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ UnixRemote::UnixRemote(const string& path)
if (connect(d_fd, (struct sockaddr*)&remote, sizeof(remote)) < 0)
unixDie("Unable to connect to remote '" + path + "' using UNIX domain socket");

d_fp = std::unique_ptr<FILE, int (*)(FILE*)>(fdopen(d_fd, "r"), fclose);
d_fp = pdns::UniqueFilePtr(fdopen(d_fd, "r"));
}

void UnixRemote::send(const string& line)
Expand Down
3 changes: 2 additions & 1 deletion modules/pipebackend/coprocess.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdio.h>
#include <string>

#include "pdns/misc.hh"
#include "pdns/namespaces.hh"

class CoRemote
Expand Down Expand Up @@ -67,6 +68,6 @@ public:

private:
int d_fd;
std::unique_ptr<FILE, int (*)(FILE*)> d_fp{nullptr, fclose};
pdns::UniqueFilePtr d_fp{nullptr};
};
bool isUnixSocket(const string& fname);
2 changes: 1 addition & 1 deletion modules/remotebackend/pipeconnector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void PipeConnector::launch()
setCloseOnExec(d_fd1[1]);
close(d_fd2[1]);
setCloseOnExec(d_fd2[0]);
if (!(d_fp = std::unique_ptr<FILE, int (*)(FILE*)>(fdopen(d_fd2[0], "r"), fclose))) {
if (!(d_fp = pdns::UniqueFilePtr(fdopen(d_fd2[0], "r")))) {
throw PDNSException("Unable to associate a file pointer with pipe: " + stringerror());
}
if (d_timeout != 0) {
Expand Down
2 changes: 1 addition & 1 deletion modules/remotebackend/remotebackend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private:
int d_fd1[2]{}, d_fd2[2]{};
int d_pid;
int d_timeout;
std::unique_ptr<FILE, int (*)(FILE*)> d_fp{nullptr, fclose};
pdns::UniqueFilePtr d_fp{nullptr};
};

class RemoteBackend : public DNSBackend
Expand Down
4 changes: 2 additions & 2 deletions pdns/dnspcap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "namespaces.hh"
PcapPacketReader::PcapPacketReader(const string& fname) : d_fname(fname)
{
d_fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname.c_str(), "r"), fclose);
d_fp = pdns::UniqueFilePtr(fopen(fname.c_str(), "r"));

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This argument to a file access function is derived from
user input (a command-line argument)
and then passed to fopen(__filename).
This argument to a file access function is derived from
user input (a command-line argument)
and then passed to fopen(__filename).
This argument to a file access function is derived from
user input (a command-line argument)
and then passed to fopen(__filename).
This argument to a file access function is derived from
user input (a command-line argument)
and then passed to fopen(__filename).
if (!d_fp) {
unixDie("Unable to open file " + fname);
}
Expand Down Expand Up @@ -235,7 +235,7 @@ PcapPacketWriter::PcapPacketWriter(const string& fname, const PcapPacketReader&

PcapPacketWriter::PcapPacketWriter(const string& fname) : d_fname(fname)
{
d_fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname.c_str(),"w"), fclose);
d_fp = pdns::UniqueFilePtr(fopen(fname.c_str(),"w"));

Check failure

Code scanning / CodeQL

File created without restricting permissions High

A file may be created here with mode 0666, which would make it world-writable.

if (!d_fp) {
unixDie("Unable to open file");
Expand Down
12 changes: 6 additions & 6 deletions pdns/dnspcap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public:
}
};

PcapPacketReader(const string& fname);
PcapPacketReader(const string& fname);

template<typename T>
void checkedFread(T* ptr)
Expand Down Expand Up @@ -118,24 +118,24 @@ public:
char *d_buffer;
size_t d_bufsize;
private:
std::unique_ptr<FILE, int(*)(FILE*)> d_fp{nullptr, fclose};
pdns::UniqueFilePtr d_fp{nullptr};
string d_fname;
unsigned int d_skipMediaHeader;
};

class PcapPacketWriter
{
public:
public:
PcapPacketWriter(const string& fname, const PcapPacketReader& ppr);
PcapPacketWriter(const string& fname);

void write();
void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; }

private:
string d_fname;
const PcapPacketReader* d_ppr{nullptr};

std::unique_ptr<FILE, int(*)(FILE*)> d_fp{nullptr, fclose};
pdns::UniqueFilePtr d_fp{nullptr};
bool d_first{true};
};
};
2 changes: 1 addition & 1 deletion pdns/dnspcap2protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ try {

PcapPacketReader pr(argv[1]);

auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(argv[2], "w"), fclose);
auto fp = pdns::UniqueFilePtr(fopen(argv[2], "w"));

Check warning on line 66 in pdns/dnspcap2protobuf.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, auth)

variable name 'fp' is too short, expected at least 3 characters (readability-identifier-length - Level=Warning)

Check warning on line 66 in pdns/dnspcap2protobuf.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, auth)

do not use pointer arithmetic (cppcoreguidelines-pro-bounds-pointer-arithmetic - Level=Warning)

Check failure

Code scanning / CodeQL

File created without restricting permissions High

A file may be created here with mode 0666, which would make it world-writable.

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This argument to a file access function is derived from
user input (a command-line argument)
and then passed to fopen(__filename).
if (!fp) {
cerr<<"Error opening output file "<<argv[2]<<": "<<stringerror()<<endl;
exit(EXIT_FAILURE);
Expand Down
2 changes: 1 addition & 1 deletion pdns/dnssecinfra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ using namespace boost::assign;
std::unique_ptr<DNSCryptoKeyEngine> DNSCryptoKeyEngine::makeFromISCFile(DNSKEYRecordContent& drc, const char* fname)
{
string sline, isc;
auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname, "r"), fclose);
auto fp = pdns::UniqueFilePtr(fopen(fname, "r"));

Check warning on line 56 in pdns/dnssecinfra.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, auth)

variable name 'fp' is too short, expected at least 3 characters (readability-identifier-length - Level=Warning)

Check warning on line 56 in pdns/dnssecinfra.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, rec)

variable name 'fp' is too short, expected at least 3 characters (readability-identifier-length - Level=Warning)
if(!fp) {
throw runtime_error("Unable to read file '"+string(fname)+"' for generating DNS Private Key");
}
Expand Down
4 changes: 2 additions & 2 deletions pdns/dnssecinfra.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DNSCryptoKeyEngine
void createFromPEMString(DNSKEYRecordContent& drc, const std::string& contents)
{
// NOLINTNEXTLINE(*-cast): POSIX APIs.
unique_ptr<std::FILE, decltype(&std::fclose)> inputFile{fmemopen(const_cast<char*>(contents.data()), contents.length(), "r"), &std::fclose};
pdns::UniqueFilePtr inputFile{fmemopen(const_cast<char*>(contents.data()), contents.length(), "r")};
createFromPEMFile(drc, *inputFile);
}

Expand All @@ -89,7 +89,7 @@ class DNSCryptoKeyEngine

std::string output{};
output.resize(buflen);
unique_ptr<std::FILE, decltype(&std::fclose)> outputFile{fmemopen(output.data(), output.length() - 1, "w"), &std::fclose};
pdns::UniqueFilePtr outputFile{fmemopen(output.data(), output.length() - 1, "w")};
convertToPEMFile(*outputFile);
std::fflush(outputFile.get());
output.resize(std::ftell(outputFile.get()));
Expand Down
6 changes: 3 additions & 3 deletions pdns/dnstcpbench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ try
std::vector<std::thread> workers;
workers.reserve(numworkers);

std::unique_ptr<FILE, int(*)(FILE*)> fp{nullptr, fclose};
pdns::UniqueFilePtr fp{nullptr};

Check warning on line 265 in pdns/dnstcpbench.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, auth)

variable name 'fp' is too short, expected at least 3 characters (readability-identifier-length - Level=Warning)
if (!g_vm.count("file")) {
fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(0, "r"), fclose);
fp = pdns::UniqueFilePtr(fdopen(0, "r"));
}
else {
fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(g_vm["file"].as<string>().c_str(), "r"), fclose);
fp = pdns::UniqueFilePtr(fopen(g_vm["file"].as<string>().c_str(), "r"));
if (!fp) {
unixDie("Unable to open "+g_vm["file"].as<string>()+" for input");
}
Expand Down
2 changes: 1 addition & 1 deletion pdns/ixfrutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void writeZoneToDisk(const records_t& records, const DNSName& zone, const std::s
/* ensure that the partial zone file will only be accessible by the current user, not even
by other users in the same group, and certainly not by other users. */
umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
auto filePtr = std::unique_ptr<FILE, int(*)(FILE*)>(fopen((fname+".partial").c_str(), "w"), fclose);
auto filePtr = pdns::UniqueFilePtr(fopen((fname+".partial").c_str(), "w"));

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This argument to a file access function is derived from
user input (a command-line argument)
and then passed to fopen(__filename).
if (!filePtr) {
throw runtime_error("Unable to open file '"+fname+".partial' for writing: "+stringerror());
}
Expand Down
2 changes: 1 addition & 1 deletion pdns/pdnsutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3527,7 +3527,7 @@ try
const auto algorithm = pdns::checked_stoi<unsigned int>(cmds.at(3));

errno = 0;
std::unique_ptr<std::FILE, decltype(&std::fclose)> fp{std::fopen(filename.c_str(), "r"), &std::fclose};
pdns::UniqueFilePtr fp{std::fopen(filename.c_str(), "r")};

Check warning on line 3530 in pdns/pdnsutil.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, auth)

variable name 'fp' is too short, expected at least 3 characters (readability-identifier-length - Level=Warning)
if (fp == nullptr) {
auto errMsg = pdns::getMessageFromErrno(errno);
throw runtime_error("Failed to open PEM file `" + filename + "`: " + errMsg);
Expand Down

0 comments on commit 46c4985

Please sign in to comment.