| 
									
										
										
										
											2018-02-23 15:02:05 +00:00
										 |  |  | // Package fuzz contains functions that enable fuzzing of plugins.
 | 
					
						
							| 
									
										
										
										
											2017-09-29 22:28:13 +01:00
										 |  |  | package fuzz
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 22:28:13 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-19 08:06:25 +00:00
										 |  |  | // Do will fuzz p - used by gofuzz. See Makefile.fuzz for comments and context.
 | 
					
						
							| 
									
										
										
										
											2019-08-24 18:13:47 +00:00
										 |  |  | func Do(p plugin.Handler, fn SetupFunc, data []byte) int {
 | 
					
						
							|  |  |  | 	if fn != nil {
 | 
					
						
							|  |  |  | 		if err := fn(); err != nil {
 | 
					
						
							|  |  |  | 			panic("fuzz: " + err.Error())
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 22:28:13 +01:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 	r := new(dns.Msg)
 | 
					
						
							|  |  |  | 	if err := r.Unpack(data); err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:49:22 +00:00
										 |  |  | 		return 0 // plugin will never be called when this happens.
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// If the data unpack into a dns msg, but does not have a proper question section discard it.
 | 
					
						
							|  |  |  | 	// The server parts make sure this is true before calling the plugins; mimic this behavior.
 | 
					
						
							|  |  |  | 	if len(r.Question) == 0 {
 | 
					
						
							|  |  |  | 		return 0
 | 
					
						
							| 
									
										
										
										
											2017-09-29 22:28:13 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if _, err := p.ServeDNS(ctx, &test.ResponseWriter{}, r); err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:49:22 +00:00
										 |  |  | 		return 1
 | 
					
						
							| 
									
										
										
										
											2017-09-29 22:28:13 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:49:22 +00:00
										 |  |  | 	return 0
 | 
					
						
							| 
									
										
										
										
											2017-09-29 22:28:13 +01:00
										 |  |  | }
 |