{"id":78,"date":"2020-08-29T02:17:44","date_gmt":"2020-08-29T07:17:44","guid":{"rendered":"https:\/\/sourceopen.com\/?p=78"},"modified":"2020-08-29T16:24:32","modified_gmt":"2020-08-29T21:24:32","slug":"ping-and-traceroute-unable-to-resolve-hostnames-while-other-programs-work-fine","status":"publish","type":"post","link":"https:\/\/sourceopen.com\/index.php\/ping-and-traceroute-unable-to-resolve-hostnames-while-other-programs-work-fine\/","title":{"rendered":"ping and traceroute Unable to Resolve Hostnames While Other Programs Work Fine"},"content":{"rendered":"\n<p>I was chatting with a fellow OpenBSD user on IRC. He was having some very strange problems with name resolution on his OpenBSD machine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ping www.openbsd.org\nping: no address associated with name<\/code><\/pre>\n\n\n\n<p>Name resolution as his non-root user worked fine with <em>host <\/em>or <em>nslookup<\/em>. Most programs would work without a hitch. However, he was unable to <em>ping <\/em>or <em>traceroute <\/em>any hosts on the internet. Both programs would return that there was no address associated with the name (any name).<\/p>\n\n\n\n<p>We went through and checked his resolv.conf file for accuracy. We checked his \/etc\/hosts file. He was using a local nameserver, which he could query directly with no problems using tools like <em>host <\/em>or <em>nslookup<\/em>. He could query 8.8.8.8 directly as well; so no firewall issues. <\/p>\n\n\n\n<p>However, if he put 8.8.8.8 in \/etc\/resolv.conf it would fail again.<\/p>\n\n\n\n<p>I had him try it as root to try to rule out permissions issues. It still failed as root. So you would think no permissions issues. The programs <em>ping <\/em>and <em>traceroute <\/em>are both setuid root anyway.<\/p>\n\n\n\n<p>Finally on a hunch I had him check the permissions on \/etc\/resolv.conf<\/p>\n\n\n\n<p>It was like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-rw-r-----  1 root  wheel  66 Aug 22 02:10 \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p>Hopefully you are screaming at the screen that you know the issue&#8230; That \/etc\/resolv.conf is only owner (root) and group (wheel) readable. No user outside the group wheel can read it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">So why would this make <em>host <\/em>and <em>nslookup <\/em>work fine and <em>ping <\/em>and <em>traceroute <\/em>fail? <\/h3>\n\n\n\n<p>Let&#8217;s think about that for a moment. If the gentleman I was chatting with on IRC&#8217;s local user was in group wheel (it was) then any program run as HIS user would work fine because \/etc\/resolv.conf was group wheel and was group readable. <\/p>\n\n\n\n<p>Then why wouldn&#8217;t <em>ping <\/em>and <em>traceroute <\/em>work? They are setuid root. So root should be able to read resolv.conf&#8230;<\/p>\n\n\n\n<p>They are setuid when they <em>start<\/em>, but the programmers are smart enough to change the uid after they get the raw socket needed for sending ICMP. (At least on OpenBSD ping they are.)<\/p>\n\n\n\n<p>How can we see this? Try to ping some website. Start ping in the background <em>as root<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ping www.OpenBSD.org &amp;<\/code><\/pre>\n\n\n\n<p>Now search the process list for ping to see what user is running ping:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> ps auxww | grep ping\n_ping    47674  0.0  0.0   668   596 p5  Sp      1:51AM    0:00.04 ping www.OpenBSD.org<\/code><\/pre>\n\n\n\n<p>WTF? How is ping now running as the user _ping? The ping program revokes its root privileges early by setting the user id to _ping. So even when run as root the ping program is only running as root for a short time to get the socket, and the changing over to _ping.<\/p>\n\n\n\n<p><em>Don&#8217;t forget to kill that ping process you started in the background.<\/em><\/p>\n\n\n\n<p>Well, the user _ping is not in the group wheel! So _ping cannot read \/etc\/resolv.conf ! And hence our program cannot resolve names. The program <em>traceroute <\/em>works in basically the same way. So all of this applies to it as well.<\/p>\n\n\n\n<p>His solution was simple (as root):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 644 \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p>Now \/etc\/resolv.conf is world readable (other readable) as it should be, and everything works fine because the _ping user can access it, and all the other users can too, like the should be able to.<\/p>\n\n\n\n<p>Problem solved.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What would have been another (possibly more efficient) approach to solving this?<\/h3>\n\n\n\n<p><em>ktrace <\/em>of course! If we trace our system calls with <em>ktrace <\/em>we can see the problem our program is facing. In this case we need to run it as root since <em>ping <\/em>is setuid root.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ktrace -f ping.out ping openbsd.org\nping: no address associated with name\n# kdump -f ping.out | grep -n1 resolv.conf\n365- 20730 ping     CALL  stat(0x1069c5f03a6d,0x7f7ffffcbef0)\n366: 20730 ping     NAMI  \"\/etc\/resolv.conf\"\n367- 20730 ping     STRU  struct stat { dev=0, ino=103983, mode=-rw-r----- , nlink=1, uid=0&lt;\"root\">, gid=0&lt;\"wheel\">, rdev=420253, atime=1598683868&lt;\"Aug 29 01:51:08 2020\">.621464705, mtime=1598080210&lt;\"Aug 22 02:10:10 2020\">.244384888, ctime=1598684275&lt;\"Aug 29 01:57:55 2020\">.564346572, size=66, blocks=4, blksize=16384, flags=0x0, gen=0x0 }\n--\n375- 20730 ping     CALL  open(0x1069c5f03a6d,0x10000&lt;O_RDONLY|O_CLOEXEC>)\n376: 20730 ping     NAMI  \"\/etc\/resolv.conf\"\n377- 20730 ping     RET   open -1 errno 13 Permission denied<\/code><\/pre>\n\n\n\n<p>The first occurrence of \/etc\/resolv.conf is just to stat() the file to make sure it exists and check permissions, etc. You can even see the bad permissions listed right there in the &#8220;STRU struct stat&#8221; line: <\/p>\n\n\n\n<pre id=\"block-89d39608-3ce7-41ad-a9ec-badd762294fd\" class=\"wp-block-code\"><code>mode=-rw-r-----<\/code><\/pre>\n\n\n\n<p>The second occurrence of \/etc\/resolv.conf is where <em>ping <\/em>tries to open it. Now running as the user _ping, this has the return (RET) from the open() call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RET   open -1 errno 13 Permission denied<\/code><\/pre>\n\n\n\n<p><em>It&#8217;s right there in black and white (green and black?)<\/em><\/p>\n\n\n\n<p>So we could have used <em>ktrace <\/em>to diagnose this (possibly) a little faster. (Took about 10 mins of trying and checking various things.) It&#8217;s a great troubleshooting tool!<\/p>\n\n\n\n<p>Hopefully you have learned a little something, or just figured out why <em>ping <\/em>or <em>traceroute <\/em>keep returning:<\/p>\n\n\n\n<pre id=\"block-60bf4421-5e4b-4b53-af7f-138436c12c3e\" class=\"wp-block-code\"><code>ping: no address associated with name\n\nor\n\ntraceroute: no address associated with name<\/code><\/pre>\n\n\n\n<p>while other programs work fine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was chatting with a fellow OpenBSD user on IRC. He was having some very strange problems with name resolution on his OpenBSD machine. Name resolution as his non-root user worked fine with host or nslookup. Most programs would work &hellip; <a href=\"https:\/\/sourceopen.com\/index.php\/ping-and-traceroute-unable-to-resolve-hostnames-while-other-programs-work-fine\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,4,52,50,44,23,2,38,48,51,53,49,24],"_links":{"self":[{"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":1,"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sourceopen.com\/index.php\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}